Click to See Complete Forum and Search --> : php msql update not working


kproc
08-14-2006, 07:54 PM
Hi below is code that I use to update information in a table. the first section of code is used to create the form and display the table values

the second part is suppposed to update the information in the table.
----------------
The first part is exicuted if matching data is found in the table. If there is matching data nothing needs to be updated and a message is displayed with instructions.

the second part is supposed to update the table and display a message when done.

The first part works excellent, the problem is with the second part when a change is made and there is no match the message displayes but the values in the table is not updated.

any help is great
thank you

<?

include 'db.php';

$child_id = $_REQUEST['id'];

$result = mysql_query("SELECT * FROM children WHERE children.child_id = '$child_id'") or die(mysql_error());

while($row = mysql_fetch_array($result)){


$first = $row[childfirstname];
$last = $row[childlastname];
$dob = $row[childdob];
$child_id = $row[child_id];


}
?>

<form id="updateinfo" name="updateinfo" method="post" action= "<? echo "CodeUpdatechild.php?id=$child_id";?>">

<table>

<tr>
<td></td>
<td>Child's First Name:</td>
<td><input id="childfirstname" name="childfirstname" type="text" value="<?php echo $first; ?>"></td>
</tr>

<tr>
<td></td>
<td>Child's Last Name:</td>
<td><input id="childlastname" name="childlastname" type="text" value="<?php echo $last; ?>"></td>
</tr>

<tr>
<td></td>
<td>Child's DOB:</td>
<td><input id="childdob" name="childdob" type="text" value="<?php echo $dob; ?>"></td>
</tr>

<tr>
<td></td>
<td>Sex:</td>
<td><select name="childsex" id="childsex">
<option></option>
<option>Male</option>
<option selected>Female</option>

</select></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="Submit" value= Update></td>
</tr>
</form>
</table>



code to update table

<? session_start(); // Start Session?>

<title>code update</title>
<?

include 'db.php';

$child_id = $_REQUEST['id'];
$firstname= $_POST['childfirstname'];
$lastname = $_POST['childlastname'];
$dob = $_POST['childdob'];
$sex = $_POST['childsex'];


/// check to make sure update does not create douplicate value
$sql_child_check = mysql_query("SELECT * FROM children WHERE childlastname = '$lastname' AND
childfirstname = '$firstname' AND
childdob = '$dob'")or die(mysql_error());

$child_check = mysql_num_rows($sql_child_check);


$row = mysql_fetch_assoc($sql_child_check);

$child_id = $row['child_id'];

if(($child_check == 1)){

$msg .= '<div style="width:325px" id= "formmessage">';
$msg .= "The change you made resulted in finding <b>".$firstname. ' '. $lastname. "</b> in the database<br> <br> <a href='deleteParent.php?id=$child_id&user_id=$user_id'>Delete</a> current person and use <a href='addchild.php'>Add Parent</a> to locate person and create link:<br />";
$msg .= '</div>';

include 'getchildren.php';
exit ();
}


if(($child_check == 0)){

mysql_query("UPDATE children SET childfirstname = '$firstname', childlastname = '$lastname', childdob = '$dob' WHERE child_id = '$child_id'")or die(mysql_error());

$msg .='<div style="width:325px" id= "formmessage">';
$msg .= $firstname. ' '. $lastname. " has been updated.";
$msg .= '</div>';

include 'getchildren.php';

}
?>

kproc
08-15-2006, 08:37 AM
I thought that I would add

If I remove the if statements and use just the code that does the update and displayes the message the table will update correctly and display th message

kproc
08-16-2006, 06:05 PM
Hi I have been searching trying to figure out why the below code is not working correctly. I have not had any luck.

any tips or a recommended alternative approach would be greatly appreciated