I copied this code from some forms that work and changed some information to apply to the new situation but something is wrong. The form "editreg.php" seems to be working fine. It populates the fields correctly. When it gets redirected to "editregsubmit.php" I get the message that the record was updated but when I check the table or refresh the form the record was NOT updated. What am I doing wrong? Eventually I want to only show the fields Camps, Grades, Dates, and Cost and only update the Number field. I do not want the id field to show at all. I am just including these as they are so I can troubleshoot, but this obviously isn't working. Can someone explain why?

editreg.php:

<?php
$con = mysql_connect("","pleasant_pbc","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pleasant_pbc", $con);

mysql_query($sql,$con);
$data = 'SELECT * FROM reg WHERE id = "1"'; 
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
  $data2 = mysql_fetch_array($query); 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
      <title></title> 
</head> 
<body> 
<!-- form to display record from database --> 
<form name="form" method="POST" action="editregsubmit.php"> 
<input type="text" name="number" size="2" value="<?php echo $data2[id]?>" />
Camp:&nbsp;<input type="text" name="camp" value="<?php echo $data2[camp]?>" />
Grades:&nbsp;<input type="text" name="grades" value="<?php echo $data2[grades]?>" />
Dates:&nbsp;<input type="text" name="dates" value="<?php echo $data2[dates]?>" />
Cost:&nbsp;<input type="text" name="cost" value="<?php echo $data2[cost]?>" />
Number Registered: &nbsp;<input type="text" name="number" value="<?php echo $data2[number]?>" size="5"/>
<input type="submit" value="submit" />
</form>
</body> 
</html>

As I said, this form populates correctly.

editregsubmit.php:

<?php 

$con = mysql_connect("","pleasant_pbc","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pleasant_pbc", $con);

$id=$_POST['id']; 
$camp=$_POST['camp']; 
$grades=$_POST['grades']; 
$dates=$_POST['dates']; 
$cost=$_POST['cost']; 
$number=$_POST['number'];

$data = "UPDATE reg SET id='$id', camp='$camp', grades='$grades', dates='$dates', cost='$cost', number='$number' WHERE id=".'"'.$id.'"'; 
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
echo "record update complete";
?>

Try echoing the update query and running it in mysql. Also, I'm not sure where you're getting $_POST from since I can't find it in the form

//name == number not id
<input type="text" name="number" size="2" value="<?php echo $data2[id]?>" />

I'm not sure what you mean by echoing the update query and running it in mysql.

Line 24 should read like this (I will correct it after I post this): <input type="text" name="id" size="2" value="<?php echo $data2[id]?>" />
so that is where I am getting the $_POST[id] from. I changed it in my online form but that didn't make any difference. It still didn't post even though it said it did.

id is the Primary Key of the table.

ps. I guess I can't edit the opening post but hopefully everyone will catch the change that should be made.

I am very SORRY!!!

I edited the wrong form. Now that the correct name for id is in the correct form it does work.

cool, glad you got your code working. what I meant by echoing the update query was to actually echo the query like

echo $data;

That way you can copy the outputted string and paste it in your query browser or phpadmin to see if the query runs correctly in mysql. It's a great trouble shooting method for finding out where the problem lies in your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.