PDA

View Full Version : Stuck on Part 11 Updating data with php


3ppps
05-29-2008, 06:40 PM
Hi

Im Having a problem updating the data on video 11, can someone look at my code and see where im going wrong

Thanks in Advance



<?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'whe re field_id ='$update_id'");
$result= @mysql_query ($query);
/////////////////////////////////////////////////////////////////
}
///delete record
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);
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
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 !";
}

/*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=utf-8" />
<title>DWC php</title>
</head>
<body>
<br /><br/>
<form id="form1" name="form1" method="post" action="<?php $server['PHP_SELF']; ?>">
<table width="300" border="1">
<tr>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
<tr>
<td><input type="text" name="data2" id="data2" value="<?php echo $rowupdate['field2'];?>"/></td>
<td><input type="text" name="data3" id="data3" value="<?php echo $rowupdate['field3'];?>"/></td>
<td><input type="text" name="data4" id="data4" value="<?php echo $rowupdate['field4'];?>"/></td>
</tr>
<tr>
<td colspan="3"><label>

<input type="reset" name="submit4" value="Reset" id="submit4" />
<input type="submit" name="submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input type="hidden" name="update_id" id="update_id" value="<?php echo $roeupdate['field1_id']; ?>" />
</label></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<table width="300" border="1">
<tr>
<td>&nbsp;</td>
<td>id</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td><input type="button" name="submit3" id="submit3" value="update" onclick="document.location.href='dwc.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" id="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
05-29-2008, 09:56 PM
where you have...


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



You need to echo the $query variable straight after the $result and then peform the update. Your SQL should echo to the browser allowing you to see if the field_id has a value

3ppps
05-31-2008, 05:07 PM
Hi David

Thanks for your reply, But Im sorry I don`t get it

As in the video I dont see the echo syntax at the update part of the code, can u expand on this by showng the correct code

P

davidj
05-31-2008, 07:49 PM
the echo is not part of the tutorial

its a way to debug and print out variables and strings to see if they contain values or that the values are correct

you need to echo the following like this...


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

echo $query; /// <<<< here is the echo

exit; /// <<<< the exit will stop any further processng


This will allow you to analyse the UPDATE Statement and to see if its correctly set and the vars look correct

3ppps
05-31-2008, 09:14 PM
Hi D

I Copied and pasted the code as shown and it still does the same, when i preview to the browser the table of data shows ok, but when I click the update button it does not show the data in the top form

It didn`t print out any var or strings
where am i going wrong

P

davidj
05-31-2008, 10:00 PM
post ya code

with the echos

3ppps
05-31-2008, 10:25 PM
code as requested

<?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'whe re field_id ='$update_id'");
$result = @mysql_query ($query);
echo $query; /// <<<< here is the echo ///////////////////////////////// This is where I posted the code
exit; /// <<<< the exit will stop any further processng /////////////////////////////////
/////////////////////////////////////////////////////////////////
}
///delete record
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);
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
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 !";
}

/*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=utf-8" />
<title>DWC php</title>
</head>
<body>
<br /><br/>
<form id="form1" name="form1" method="post" action="<?php $server['PHP_SELF']; ?>">
<table width="300" border="1">
<tr>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
<tr>
<td><input type="text" name="data2" id="data2" value="<?php echo $rowupdate['field2'];?>"/></td>
<td><input type="text" name="data3" id="data3" value="<?php echo $rowupdate['field3'];?>"/></td>
<td><input type="text" name="data4" id="data4" value="<?php echo $rowupdate['field4'];?>"/></td>
</tr>
<tr>
<td colspan="3"><label>

<input type="reset" name="submit4" value="Reset" id="submit4" />
<input type="submit" name="submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input type="hidden" name="update_id" id="update_id" value="<?php echo $roeupdate['field1_id']; ?>" />
</label></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<table width="300" border="1">
<tr>
<td>&nbsp;</td>
<td>id</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td><input type="button" name="submit3" id="submit3" value="update" onclick="document.location.href='dwc.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" id="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>

3ppps
05-31-2008, 10:39 PM
Hi
I have just noticed in the code I just posted, after $data4 (whe re) has a space in it, in dremweaver it shows as ok, dont know why


$query = sprintf("UPDATE table1 set field2='$data2',field3='$data3',field4='$data4'whe re field_id ='$update_id'");
$result = @mysql_query ($query);
echo $query; /// <<<< here is the echo ///////////////////////////////// This is where I posted the code
exit; /// <<<< the exit will stop any further processng

3ppps
05-31-2008, 10:57 PM
Hi
sorry also just noticed I need to spell better apart from ( dremweaver )
in my code below ( $roeupdate )

<input type="hidden" name="update_id" id="update_id" value="<?php echo $roeupdate['field1_id']; ?>" />

davidj
05-31-2008, 11:41 PM
is this the issue

is it working

3ppps
06-01-2008, 09:09 PM
No not yet

davidj
06-01-2008, 10:21 PM
did you echo the sql

davidj
06-01-2008, 10:28 PM
i noticed in your update statement you have a db field called


field_id


is this not supposed to be called


field1_id

3ppps
06-02-2008, 10:13 PM
Hi
Changed that to field1_id, and I still can`t update
when you say ( did you echo the sql ) sorry but what exactly do you mean

can you send me the code from the update video and I can check through it line by line

davidj
06-02-2008, 10:18 PM
i dont have it any more

your making basic mistakes (you pointed out a few already)

if your making this many then every key you press could lead to a bug or a syntax error

i would start again and follow the tuts to the letter. Be careful and try to understand what your writing then the mistakes tend to jump out at you

davidj
06-02-2008, 10:21 PM
post ya code again

please use the forum code tags when formating code in your posts

3ppps
06-02-2008, 11:08 PM
Thanks D
Here is my code



<?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'whe re field1_id ='$update_id'");
$result = @mysql_query ($query);
echo $query; /// <<<< here is the echo ///////////////////////////////// This is where I posted the code
exit; /// <<<< the exit will stop any further processng /////////////////////////////////
/////////////////////////////////////////////////////////////////
}
///delete record
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);
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
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 !";
}

/*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=utf-8" />
<title>DWC php</title>
</head>
<body>
<br /><br/>
<form id="form1" name="form1" method="post" action="<?php $server['PHP_SELF']; ?>">
<table width="300" border="1">
<tr>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
<tr>
<td><input type="text" name="data2" id="data2" value="<?php echo $rowupdate['field2'];?>"/></td>
<td><input type="text" name="data3" id="data3" value="<?php echo $rowupdate['field3'];?>"/></td>
<td><input type="text" name="data4" id="data4" value="<?php echo $rowupdate['field4'];?>"/></td>
</tr>
<tr>
<td colspan="3"><label>

<input type="reset" name="submit4" value="Reset" id="submit4" />
<input type="submit" name="submit" value="Submit" />
<input name="submit" type="hidden" id="submit" value="1" />
<input type="hidden" name="update_id" id="update_id" value="<?php echo $rowupdate['field1_id']; ?>" />
</label></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<table width="300" border="1">
<tr>
<td>&nbsp;</td>
<td>id</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
<td>&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td><input type="button" name="submit3" id="submit3" value="update" onclick="document.location.href='dwc.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" id="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>


format your posts with a code tag please - dj

davidj
06-03-2008, 07:00 AM
in your form tag you have


$server['PHP_SELF'];


it should be


$_SERVER['PHP_SELF'];


if your not following instruction and inventing your own syntax then your going run into problems often.

3ppps
06-05-2008, 01:17 PM
Hi David

Done as you suggested went back to the start and it works ok this time

Thanks again

P