PDA

View Full Version : Problem with last part of php - updating data


Karkar
06-06-2007, 08:23 AM
OkOk. When I update data, it does work but it also adds another field. How can I change this? Here's the code: tyvm for help


<?php
require_once("connections/connection.php"); // 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'];

///////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
///////////////////////////////////////////////
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 set field2='$data2', field3='$data3', field4='$data4' where field1_id = '$update_id'");
$result = @mysql_query($query);
////////////
}
////////////
if ($del){
/////////////////////////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());
////////////////////////////////////////////
}
if ($submit && $data2 && $data3 && $data4){
/////////////////////////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());
////////////////////////////////////////////
}elseif($submit){
echo "One of your fields is empty !";
}
///////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////////
/* do {
echo $row['field3']."<br>";
}while ($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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body><br /><br />
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="461" border="1">
<tr>
<td width="144">data2</td>
<td width="144">data3</td>
<td width="190">data4</td>
</tr>
<tr>
<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>
</tr>
<tr>
<td colspan="3">
<label>
<input type="reset" name="Submit4" value="Reset" />
</label>
<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']?>" />
</td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<table width="491" border="1">
<tr>
<td width="54">&nbsp;</td>
<td width="54" height="34">ID</td>
<td width="99">data2</td>
<td width="96">data3</td>
<td width="96">data4</td>
<td width="52">&nbsp;</td>
</tr>

<?php do { ?>

<tr>
<td><label>
<input type="button" name="Submit3" value="Update" onClick="document.location.href='dwc.php?upd=<?php echo $row['field1_id']?>'" />
</label></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='dwc.php?del=<?php echo $row['field1_id']?>'" /> </td>
</tr>

<?php }while ($row = mysql_fetch_array($result)); ?>
</table>
</body>
</html>

davidj
06-06-2007, 09:14 AM
you have 2 insert statements

one checks the update variable where the second does not

its the second one that is causing you the headache

////////

Can i just say that this is a very simple coding error and that if you look through your code carefully before posting then you should have seen this. Its part of the learning aspect of coding. Debugging is just as important a skill as writing the code in the first place.

Dont get me wrong as this is not a bollocking. I too will ask a colleague to read through my code when i have a syntax error which i cant find and its usually an unclosed condition or an unterminated line.

double check then tripple check your syntax all the time

Karkar
06-06-2007, 12:15 PM
okedokes. thanks very much for help

Karkar
06-06-2007, 12:33 PM
i'm trying to figure out that little riddle at the end... not having much luck

davidj
06-06-2007, 02:08 PM
what are you tryg to figure out

Karkar
06-06-2007, 02:24 PM
getting the text fields to display the latest update without refreshing

davidj
06-06-2007, 03:08 PM
post the code you have after you have put right the problem i pointed out

Karkar
06-06-2007, 03:09 PM
<?php
require_once("connections/connection.php"); // 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'];

///////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
///////////////////////////////////////////////
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 set field2='$data2', field3='$data3', field4='$data4' where field1_id = '$update_id'");
$result = @mysql_query($query);
////////////
}
////////////
if ($del){
/////////////////////////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());
////////////////////////////////////////////
}
if ($submit && $data2 && $data3 && $data4){

}elseif($submit){
echo "One of your fields is empty !";
}
///////////////////////////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
///////////////////////////////////////////////
/* do {
echo $row['field3']."<br>";
}while ($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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body><br /><br />
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table width="461" border="1">
<tr>
<td width="144">data2</td>
<td width="144">data3</td>
<td width="190">data4</td>
</tr>
<tr>
<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>
</tr>
<tr>
<td colspan="3">
<label>
<input type="reset" name="Submit4" value="Reset" />
</label>
<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']?>" />
</td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<table width="491" border="1">
<tr>
<td width="54">&nbsp;</td>
<td width="54" height="34">ID</td>
<td width="99">data2</td>
<td width="96">data3</td>
<td width="96">data4</td>
<td width="52">&nbsp;</td>
</tr>

<?php do { ?>

<tr>
<td><label>
<input type="button" name="Submit3" value="Update" onClick="document.location.href='dwc.php?upd=<?php echo $row['field1_id']?>'" />
</label></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='dwc.php?del=<?php echo $row['field1_id']?>'" /> </td>
</tr>

<?php }while ($row = mysql_fetch_array($result)); ?>
</table>
</body>
</html>

davidj
06-06-2007, 04:28 PM
ok whats wrong with it

Karkar
06-07-2007, 07:46 AM
i dunno........ It looks fine to me. Does new code need to be inserted or does exisisting code need to be changed???? Does it have anythingt o do with the javascript?

davidj
06-07-2007, 07:50 AM
ok we have some wires crossed

your original post read...

When I update data, it does work but it also adds another field.
i spotted that you had 2 insert statements and you removed the one causing the issue.

Whats happening now?

Karkar
06-07-2007, 08:04 AM
I fixed that problem. Now i am trying to figure out how to make it so when i type stuff in the text fields and submit, to make the new data appear in the text fields instead of having to refresh the page. You talk about it at the very end of of the last beginner php tutorial.

davidj
06-07-2007, 10:27 AM
oh right

your the second person to ask this

check out this thread

http://www.dreamweaverclub.com/forum/showthread.php?t=25457&page=2&highlight=medal

start at post 14 and see if you can solve it

Karkar
06-07-2007, 12:03 PM
I can't believe that's all i had to do :-). That's great. Thanks heaps, david. Diddo to everything tux said! I dunno what i'd to without you, dj~!

Kar