PDA

View Full Version : Double Submit and Update problems


gandalfthegrey
04-22-2007, 05:13 PM
Hello again,
Once again, i've seemed to have overlooked things in my script,
PROBLEM 1: somehow once i submit an entry, the results appear twice
PROBLEM 2: instead of updating a result, another result is posted

here is my code;

<?php
require_once("connectionscript.php");
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$delete = $_GET['delete'];
$update = $_GET['update'];
$update_id = $_POST['update_id'];

$query = sprintf("SELECT * FROM table1 where field1_id = '$update'");
$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($delete){
$query = sprintf("DELETE FROM table1 where field1_id = '$delete'");
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 "please complete all the field";
}

$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>
<form id="form5" name="form5" method="post" action="">
<table width="495" border="1">
<tr>
<td width="129">data2</td>
<td width="121">data3</td>
<td width="223">data4</td>
</tr>
<tr>
<td><?php $_SERVER['PHP_SELF']; ?>
<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"><input type="submit" name="Submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input type="reset" name="Submit4" value="Reset" />
<input name="update_id" type="hidden" id="update_id" value= "<?php echo $rowupdate ['field1_id'];?>" /></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<table width="566" border="1">
<tr>
<td width="66">&nbsp;</td>
<td width="66">ID </td>
<td width="65">data2</td>
<td width="76">data3</td>
<td width="118">data4</td>
<td width="135">&nbsp;</td>
</tr>

<?php do{ ?>
<tr>
<td><input type="button" name="Submit3" value="Update"
onClick="document.location.href='dwc.php?update=<?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><form id="form1" name="form1" method="post" action="">
<input type="button" name="Submit2" value="Delete"
onClick="document.location.href='dwc.php?delete=<?php echo $row['field1_id'] ?>'" />
</form> </td>
</tr>

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



Well you did say ask if i needed help :-D

davidj
04-22-2007, 05:52 PM
PROBLEM 1: somehow once i submit an entry, the results appear twice


does this mean your inserting data twice

gandalfthegrey
04-22-2007, 06:00 PM
well i dont doulble click the submit which must mean somehow the code i've used duplicates entries.

davidj
04-22-2007, 06:05 PM
you have 2 INSERT statements

you need to go over your script and understand it

You cant code professionaly if you just replicate some one else.

once you understand what your doing then these errors will jump out at you

gandalfthegrey
04-22-2007, 06:10 PM
Ok thanks for the advice, i've also thought that i'll understand the code better if i start to comment.

What about the update problem then?

davidj
04-22-2007, 06:22 PM
the update problem could be a result of 2 INSERT's

gandalfthegrey
04-22-2007, 06:24 PM
I've made the change, there is no duplication now, but the results dont update when new values are submitted, they remain unchanged.

davidj
04-22-2007, 06:26 PM
post the code you have now

gandalfthegrey
04-22-2007, 06:29 PM
<?php

require_once("connectionscript.php");

$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$delete = $_GET['delete'];
$update = $_GET['update'];
$update_id = $_POST['update_id'];


$query = sprintf("SELECT * FROM table1 where field1_id = '$update'");
$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($delete){

$query = sprintf("DELETE FROM table1 where field1_id = '$delete'");
mysql_query($query)or die(mysql_error());
}

if ($submit && $data2 && $data3 && $data4) {




}elseif($submit){
echo "please complete all the field";
}



$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>
<form id="form5" name="form5" method="post" action="">
<table width="495" border="1">
<tr>
<td width="129">data2</td>
<td width="121">data3</td>
<td width="223">data4</td>
</tr>
<tr>
<td><?php $_SERVER['PHP_SELF']; ?>
<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"><input type="submit" name="Submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input type="reset" name="Submit4" value="Reset" />
<input name="update_id" type="hidden" id="update_id" value= "<?php echo $rowupdate ['field1_id'];?>" /></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<table width="566" border="1">
<tr>
<td width="66">&nbsp;</td>
<td width="66">ID </td>
<td width="65">data2</td>
<td width="76">data3</td>
<td width="118">data4</td>
<td width="135">&nbsp;</td>
</tr>

<?php do{ ?>
<tr>
<td><input type="button" name="Submit3" value="Update"
onClick="document.location.href='dwc.php?update=<?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><form id="form1" name="form1" method="post" action="">
<input type="button" name="Submit2" value="Delete"
onClick="document.location.href='dwc.php?delete=<?php echo $row['field1_id'] ?>'" />
</form> </td>
</tr>

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

davidj
04-22-2007, 06:39 PM
just before your update statement in the IF...

try this..


}elseif($submit && $update_id) {

echo "TEST"; //<< for debugging

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



This is a good debugging method

once you have done the above run the script and try an update. If you see the echo'd message (TEST) then you know your IF statement is working so we can look at your SQL if you dont see the message then your IF is at fault

gandalfthegrey
04-22-2007, 06:50 PM
Yes, TEST was echoed, what now?

davidj
04-22-2007, 06:53 PM
this tells you that your Logic is working so it must be the SQL statement

now lets see what your SQL is doing...

Try this below then post what you get back

add an echo...


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

gandalfthegrey
04-22-2007, 06:54 PM
Yes the TEST was echoed what now?

davidj
04-22-2007, 06:56 PM
what?

i told you to echo the SQL as described

gandalfthegrey
04-22-2007, 06:59 PM
Where do i place that code?

gandalfthegrey
04-22-2007, 07:01 PM
TESTUPDATE table1 set field2='3333', field3='pears', field4='2007-04-11 where field1_id='40'

davidj
04-22-2007, 07:02 PM
you dont

you just add the echo to the UPDATE statement

gandalfthegrey
04-22-2007, 07:04 PM
Ok this is what i see:

TESTUPDATE table1 set field2='3333', field3='pears', field4='2007-04-11 where field1_id='40'

davidj
04-22-2007, 07:05 PM
that looks ok

in your database do you have a record with the ID of '40'

gandalfthegrey
04-22-2007, 07:07 PM
No, it goes up to 39

gandalfthegrey
04-22-2007, 07:09 PM
NO actually i have one now.

davidj
04-22-2007, 07:09 PM
so how can it update a record with an ID of 40 when you only have ID's upto 39?

gandalfthegrey
04-22-2007, 07:11 PM
what do you you mean, the string 'pears' has an ID of 40.

davidj
04-22-2007, 07:13 PM
and is it still not working

davidj
04-22-2007, 07:14 PM
if this still doesnt work your going to have to revisit the tutorials and start over

Take your time and make sure you understand every aspect

gandalfthegrey
04-22-2007, 07:20 PM
Its still not updating.

gandalfthegrey
04-22-2007, 07:21 PM
Ok thanks for your help, maybe i'll wake up with a fresh mind and get down to it tommorrow.

Thank you