I have a form with only one input. When I click the button, an AJAX request is made and the data is sent to the response.php, which uses the pdo library to insert the data to db. Here it is okay, but in the page, there is a success function which loads the data into the form - when the ajax request completes, the result is blank...I need to refresh(F5) the page to see the data ..
Here is this code:
<?php
include_once("config.php");
if (!empty($_POST)) {
try{
$statement = $conn->prepare("INSERT INTO DIAGNOSTICO (id_paciente, id_doctor, hconsulta) VALUES (?, ?, ?)");
if ($statement->execute(array($_POST['id_paciente'], $_POST['id_doctor'], $_POST['hconsulta'])));
$dbSuccess = true;
} catch (Exception $e) {
$return['databaseException'] = $e->getMessage();
}
{
echo $conn->lastInsertId();
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
}
$dbh = null;
}
?>
It is working, but when in the page, the result returns from the ajax request and shows the id but not the data...I need the data result...can you help me?
best regards!
After using the lastinsertid you have to select data by using
$sth = $dbh->prepare("SELECT * FROM DIAGNOSTICO WHERE id_diagnostico= ".$conn->lastInsertId());
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
Read http://php.net/manual/en/pdostatement.fetch.php
Related
Im busy creating a webpage that issues drawings to a client and creates a receipt for sending to a client. The part that I'm having trouble with is to select the drawings that I want to add to the receipt and to change the revision.
The information for the drawings are split over two tables. The first contains the drawing number, title, drawn by etc and the second contains only the revision history. The tables are linked by the drawing id.
To issue a drawings the user must tick the checkboxes and change the revision of the drawings he wants to issue.
I want to input this into the database, but I'm not sure how to create the array. Currently my controller takes gets all the dropdown inputs and only the ticked checkboxes. I want only the dropdown inputs associated with the selected checkboxes to be updated by the database.
This is my view.
<h1>Issue drawing</h1>
<br>
<div id="body">
<div class="row">
<div class="form-group-sm"><lable class="col-sm-2 control-label">Project number:</lable>
<?php
$js = 'onchange="this.form.submit()" class="form-control" id="focusInput"';
echo form_open('dwg_issue/issue_dwg');
echo "<div class=\"col-xs-2\">" . form_dropdown('project_no',$proj_num, $this->input->post('project_no'), $js)."</div>";
echo form_error('project_no', '<div class="col-xs-4"><div class="alert alert-danger fade in">×','</div></div>');
?>
</div>
</div>
<?php
echo "<noscript>".form_submit('submit','Submit')."</noscript>";
echo form_close();
?>
<form action="<?php echo base_url() . 'index.php/dwg_issue/issue'; ?>" method="post" accept-charset="utf-8" id="issue">
<table title="Client information" class="table table-hover">
<caption><b>List of drawings</b></caption>
<thead>
<tr><th>Project number</th><th>Drawing number</th><th>Client drawing number</th>
<th>Title</th><th>Drawn by</th><th>Revision</th><th>Drawn Date</th><th>Select</th>
</thead>
<tbody>
<?php
// var_dump($client_info);
if(!empty($result))
{
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row->project_no . "</td>";
echo "<td>" . $row->sws_dwg_no . "</td>";
echo "<td>" . $row->client_dwg_no . "</td>";
echo "<td>" . $row->dwg_title . "</td>";
echo "<td>" . $row->dwg_by . "</td>";
$rev = array('0' => $row->dwg_rev);
$rev_change = array(
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E'
);
$dropdown = array_merge($rev,$rev_change);
echo "<td>" . form_dropdown('dwg_rev['.$row->dwg_id.']',$dropdown) . "</td>";
echo "<td>" . date('Y/m/d', strtotime($row->dwg_date)) . "</td>";
echo "<td>" . form_checkbox('select['.$row->dwg_id.']',$row->dwg_id) . "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
<div class="row">
<div class="col-xs-12 col-md-10">Please select client distribution here with other options</div>
<?php
echo "<div class=\"col-xs-6 col-md-2\"><button class=\"btn btn-md btn-primary btn-block\" type=\"submit\" form=\"issue\">Issue drawings</button></div>";
?>
</div>
<br>
<?php
echo form_close();
?>
</div>
My controller
public function issue_dwg()
{
$data['proj_num'] = $this->model_proj->proj_num_all();
if ($this->input->post('project_no') != '0')
{
$data['result'] = $this->model_issue->list_dwg($this->input->post('project_no'));
}
$data['main_content'] = 'issue_view';
$this->load->view('includes/template.php', $data);
}
//This function takes the selected drawings and create and issue slip
public function issue()
{
$rows = array();
$num_results = count($this->input->post('select'));
for($i = 0; $i < $num_results; $i++)
{
$rows[$i]['select'] = $_POST['select'][$i];
$rows[$i]['dwg_rev'] = $_POST['dwg_rev'][$i];
}
}
The issue_dwg() function gets populates the dropdown with the drawing numbers and populates the view with the db results.
The issue() function needs to process the posted information and convert it into a array to pass to the db.
(I wanted to add an image of the view page, but I don't have enough reputation to do it)
When you parse your form, you need to build your inputs so that, when you submit the form, you end up with associative arrays:
currently you have the following:
//...
echo form_dropdown('dwg_rev['.$row->dwg_id.']',$dropdown);
echo form_checkbox('select['.$row->dwg_id.']',$row->dwg_id);
//...
when submitted, it will send the following post array:
"select" (array)[
0 => "some select value",
1 => "some select value"
],
"dwg_rev" (array)[
0 => "some dwg_rev value",
1 => "some dwg_rev value"
]
SOLUTION:
structure your form like this:
//...
echo form_dropdown('result['.$row->dwg_id.'][dwg_rev]',$dropdown);
echo form_checkbox('result['.$row->dwg_id.'][select]',$row->dwg_id);
//...
this way, youll end up with an array as such:
"result"[
0 => [
"select" => "some select value",
"dwg_rev" => "some dwg_rev value"
]
1 => [
"select" => "some select value",
"dwg_rev" => "some dwg_rev value"
]
]
Now you can iterate with a foreach in your controller with ease.
I am trying to load articles from 1 to 5 on the homepage of this website.
When loading the homepage, only results from ID 2 to 5 vs. 1 to 5 are getting displayed and I'm not sure why. It seems there probably is a problem with my while loop but I can't seem to figure it out.
I have added a link to a screenshot of the database to show that there is in fact an article with the ID 1 and I've also added a link to the website itself.
Database in question
Website in question
<div class="bodymainwrap">
<div class="contentwrap">
<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
$page = "between " . (($_GET['id']*5)-5) . " and " . ($_GET['id']*5);
} else {
$page = "between 1 and 5";
}
require_once '/db.connect';
$query = "SELECT * FROM Articles where id " . $page;
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result)
?>
<div class="sidebar" align="center">
<a href="/nothing/index.php?id=2" >Page 2</a>
<?php echo $query ?>
</div>
<?php
while ($row = mysqli_fetch_array($result)){
echo '<div class="entry">';
echo '<img src="/nothing/images/julie.jpg">';
echo'<H2>';
echo $row['Title'];
echo'</h2>';
echo '<p>';
echo $row['Article'];
echo '</p>';
echo '</div>';
}
?>
</div>
Your issue is here I believe, with the lone mysqli_fetch_array call after you get $result
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result) //delete this
Here you have already fetched the first row. Remove that, and just keep it within the loop. That's the underlying reason for how the while loop works. It keeps advancing while mysqli_fetch_array doesn't return null.
i have this code in iclude for connect to database
i have in this file mysql connection function too but i decide change mysql to pdo
-include.php:
try{
$conn = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
return $conn;
}
catch(PDOException $e)
{
echo 'can not connect to database';
exit();
}
i have mysql query in index.php but i want change this code to pdo
<?php
$sql=mysql_query("SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18");
if(mysql_num_rows($sql)) {
$j=0;
while($result = mysql_fetch_object($sql)) {
$j++;
if($result->id == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
}
}
?>
how can i change mysql query in this code to pdo?
Read some tutorial (there are plenty even here on SO)
Run some simple queries to make yourself familiar with PDO
Start with rewriting.
You should read some doc about PDO and do some tutorial. But to help you, here is a really simple sample to begin with PDO :
<?php
$connexion = new PDO("mysql:host=$PARAM_hote;dbname=$PARAM_db_name", $PARAM_user, $PARAM_pwd); // DB connexion
$results=$connection->query("SELECT member FROM member ORDER BY member ASC"); // retrieving all entries from member table
$results->setFetchMode(PDO::FETCH_OBJ); // retrieving results as objects
while( $lines= $results->fetch() ) // retrieving list of member
{
echo 'user: '.$lines->member.'<br />'; // displaying members
}
$results->closeCursor(); // close results cursor
?>
And here a more sophisticated one with params (prepared query) :
<?php
// connection opening ...
$prepared_query=$connecion->prepare("SELECT id FROM member WHERE member_id= :id"); // query prepare
$prepared_query->execute(array( 'id' => 1 ));
$lines=$prepared_query->fetch(PDO::FETCH_OBJ);
echo $lines->id.'<br />';
?>
It's an old sample of mine I hope will help you.
Something like this:
<?php
$sql = "SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18 ";
$sth = $conn->query($sql);
$sth->setFetchMode(PDO::FETCH_ASSOC);
if ($count = $sth->rowCount() > 0){
$j=0;
while ($row = $sth->fetch()) {
$j++;
if($row['id'] == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $row['id'] ?>&title=<?php echo title($row['col1']);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $row['col1'] ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $row['id'] ?>&title=<?php echo title($row['col1']);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $row['col1'] ?></div>
</div>
</a>
<?php
}
}
}
?>
I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work
Here is my code:
<html>
<?php
DEFINE('DATABASE_USER', 'sfasdfasd');
DEFINE('DATABASE_PASSWORD', 'asdfasdfasdf');
DEFINE('DATABASE_HOST', 'sdfasdfasd');
DEFINE('DATABASE_NAME', 'dsafsdfasd');
$connect = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME) or
die ("Hey loser, check your server connection.");
mysql_select_db("minedb");
$query = "SELECT * FROM ideastable ORDER BY datee DESC";
$quey1="select * from ideastable";
$result=mysql_query($query) or die(mysql_error());
?>
<table border=1 style="background-color:#000000;" >
<caption><EM>Ideas List</EM></caption>
<tr>
<th>IDEAS</th>
<th>Thumbs Ups</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo "</td><td>";
echo $row['idea'];
echo "</td><td>";
echo $row['thumbsup'];
$i = $row['id'];
echo $i;
echo 'Thumbs Up!';
echo "</td></tr>";
}
echo "</table>";
?>
<SCRIPT type="text/javascript" src="jquery.min.js"></SCRIPT>
<SCRIPT type="text/javascript">
function doSomething() {
var myVar = "<?php echo $i; ?>";
alert(myVar);
$.load('uts.php?i=myVar');
return false;
}
</SCRIPT>
</html>
My question is how would I be able to make it so that when I click the Thumbs Up it recognizes the row the link was in? I am making a site where you can rate some of the objects in a database and that is the start up of it.
How about passing id to javascript function?
echo 'Thumbs Up!';