UPDATE form to MySQL help

I have a form that sends info to a table called osd_report. I have a page that pulls the info submitted and another user (employee) can update the submitters post. I have error checks throughout and and when the information is updated and saved, it says that it was saved, however, nothing is changed in the MySQL table. Can someone help me figure out what I did wrong?

Here is the code for the page that pulls the info and allows for updating:

[php]

<?php if ($_SERVER['HTTPS'] != "on") { $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; header("Location: $url"); exit; } include 'dbc.php'; //where all db info is stored page_protect(); //used by a login script to protect page $db_selected = mysql_select_db(DB_NAME, $link); if (!db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); } $id = $_GET['id']; //this line pulls from another page where the link for this specific submission directs //here and where all submissions are stored if($_POST['doSave'] == 'Save') { // Filter POST data for harmful code (sanitize) foreach($_POST as $key => $value) { $data[$key] = filter($value); } mysql_query("UPDATE osd_report SET `first_name` = '$data[first_name]', `last_name` = '$data[last_name]', `company_name` = '$data[company_name]', //many more fields that have// //been removed for this psot// WHERE id='$id' ") or die(mysql_error()); //header("Location: contact_osd_results.php?msg=Profile Sucessfully saved"); $msg[] = "Report Information Saved"; } $rs_osd = mysql_query("select * from osd_report where id='$id'"); ?>

[/php]

Here is a snippet from the form:

[php]

<?php while ($row_osd = mysql_fetch_array($rs_osd)) {?>
How to reach the company:

First Name*:

//much more cut out//

      <p>Date Updated:</p>
      <p><input class="forms" type="text" name="date_updated" value="<? echo $row_osd['date_updated']; ?>" maxlength="" size="36"><br /><br /></p>             
  </div>  
  <div class="forms_all form_full">
    <p align="center"> 
      <input name="doSave" type="submit" id="doSave" value="Save">
    </p>      
  </div>   
<?php } ?> [/php]

I need the form to update the information in a specific row by the “id” field.

I hope I have given enough information and code. If not, please advise and I will do my best to supply what is needed.

Thanks,

Clint

I have tested your code and modified it, it should work now:
[php]

<?php //php398 - script now working, put a record in osd_report with id of 1, and go to script.php?id=1 //php398 - insert server, username, password mysql_connect("server", "username", "password") or die(mysql_error()); echo "Connected to MySQL
";

$db_selected = mysql_select_db(“userdbph”);
//php398 - used $db_selected instead of db_selected
if (!$db_selected) {
die('Can’t use userdbph: ’ . mysql_error());
}

$id = $_GET[‘id’]; //this line pulls from another page where the link for this specific submission directs
//here and where all submissions are stored

if (!empty($_POST)){
if($_POST[‘doSave’] == ‘Save’)
{
// Filter POST data for harmful code (sanitize)
foreach($_POST as $key => $value) {
$data[$key] = $value;
}

//php398 - corrected apostrophe use, no apostrophes should appear around field names
mysql_query("UPDATE osd_report SET first_name = '$data[first_name]', last_name = '$data[last_name]', company_name = '$data[company_name]'	WHERE id='$id'") or die(mysql_error());

 //header("Location: contact_osd_results.php?msg=Profile Sucessfully saved");
$msg[] = "Report Information Saved";
 }

}

$rs_osd = mysql_query(“select * from osd_report where id=’$id’”);

//php398 - you need to make sure that the form posts to a page where id is set in the query string otherwise $_GET[‘id’] will be undefined
function curPageName() {
$page1 = explode(’/’,$_SERVER[‘REQUEST_URI’]);
$page2 = end($page1);
return $page2;
}
?>

<?php while ($row_osd = mysql_fetch_array($rs_osd)) { //php398 - if you have more than one form displayed only data in the form you have clicked "save" for will be updated ?>
How to reach the company:

First Name*:

//much more cut out//


Date Updated:










<?php } mysql_close(); ?> [/php]

awesome thanks! It updates now with the exception of two fields. I have two textarea fields that do not pull the data from the server. It will however allow me to type text and update it, and it does upload the data to the server, but when I refresh the update form, it will not pull the recently added text down to the page. any ideas?

And your form action script now keeps the upload message on the same page. thank you for that added fix!

Thanks,

Clint

Resolved. I had my php tags inside the testarea element instead of between opening and closing tags. :stuck_out_tongue:

Thanks for your help!!!

Clint

Sponsor our Newsletter | Privacy Policy | Terms of Service