Jump to content

php mysql update


proctk

Recommended Posts

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
[code=php:0]
<?

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]

code to update table
[code=php:0]
<? 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';

}
?>
[/code]
Link to comment
Share on other sites

Your code is very messy.

[code]<?

include 'db.php';

$child_id = $_REQUEST['id'];

$result = mysql_query("SELECT childfirstname, childlastname, childdob, child_id FROM children WHERE children.child_id = '$child_id'") or die(mysql_error());

list($first, $last, $dob, $child_id) = mysql_fetch_assoc($result);

?>
<form id="updateinfo" name="updateinfo" method="post" action= "<? echo "CodeUpdatechild.php?id=$child_id";?>">
<table>
<tr>
<td>&nbsp;</td>
<td>Child's First Name:</td>
<td><input id="childfirstname" name="childfirstname" type="text" value="<?php echo $first; ?>"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Child's Last Name:</td>
<td><input id="childlastname" name="childlastname" type="text" value="<?php echo $last; ?>"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Child's DOB:</td>
<td><input id="childdob" name="childdob" type="text" value="<?php echo $dob; ?>"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Sex:</td>
<td>
<select name="childsex" id="childsex">
<option></option>
<option>Male</option>
<option selected>Female</option>
</select>
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="Submit" value= Update></td>
</tr>

</table>
</form>



<? 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';
}
?>[/code]

Echo out your query, then execute it in phpMyAdmin or MySQL query browser and make sure that it is executing correctly.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.