View Full Version : Part 11 - Updating data with PHP question

03-25-2007, 10:38 PM
Ok I've copied everything word for word and there are two things that wont work.

1. When I delete data it never deletes the id number. example: id number = 2 I delete id number 2 and enter new data so the new data should be 2 but has an id number of 3.

2. When I click update the information is sent into the top form and when I change it and submit it nothing happens.

I've looked over this for many hours now and I'm stumped there are only two reasons I think this isn't working and am wanting a second opinion.

1. My host uses MySQL 4.1.21
2. My host uses PHP version 4.4.4

Would that cause it not to work?

03-26-2007, 06:25 AM
Would that cause it not to work? You forgot to show the code.

03-26-2007, 02:51 PM

/////////////////////////////////////database connection

$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$del = $_GET['del'];
$upd = $_GET['upd'];
$update_id = $_POST['update_id'];

/////////////////////////////////////update data
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);

/////////////////////////////////////adding the information to the database
if ($submit && $data2 && data3 && data4 && ! $update_id) {

$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());

}elseif($submit && $update_id) {

$query = sprintf("UPDATE table1 field2='$data2', field3='data3', field4='$data4', where field1_id = '$update_id'");
$result =@mysql_query($query);

/////////////////////////////////////delete data
if ($del){
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());

$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="423" border="1">
<td><input name="data2" type="text" id="data2" value="<?php echo $rowUpdate['field2'];?>" /></td>
<td><input name="data3" type="text" id="data3" value="<?php echo $rowUpdate['field3'];?>" /></td>
<td><input name="data4" type="text" id="data4" value="<?php echo $rowUpdate['field4'];?>" /></td>
<td colspan="3"><div align="center">
<input type="reset" name="Submit4" value="Reset" />
<input type="submit" name="Submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input name="update_id" type="hidden" id="update_id" value="<?php echo $rowUpdate['field1_id'];?>" />
<table width="432" height="58" border="1">
<td width="56">&nbsp;</td>
<td width="56">ID1</td>
<td width="76">data2</td>
<td width="76">data3</td>
<td width="76">data4</td>
<td width="52">&nbsp;</td>

<?php do { ?>
<td><input type="button" name="Submit3" value="Update" onclick="document.location.href='sqltest.php?upd=<?php echo $row['field1_id']?>'" /></td>
<td><?php echo $row['field1_id'];?></td>
<td><?php echo $row['field2'];?></td>
<td><?php echo $row['field3'];?></td>
<td><?php echo $row['field4'];?></td>
<td><input type="button" name="Submit2" value="Delete" onClick="document.location.href='sqltest.php?del=<?php echo $row['field1_id'] ?>'" /></td>
<?php }while ($row = mysql_fetch_array($result)); ?>

03-27-2007, 01:45 AM
Since the ID field is auto increment it wont reuse an ID.

03-27-2007, 02:03 AM
but if you delete id 2 then the number is no longer there and the database should see id 1 and then increase to id 2

but im more worried about my second problem. If anyone has any ideas on that please share.