PDA

View Full Version : Update problem?



wispas
February 20th, 2007, 10:59 PM
I got this code from the tutorial and it does not update the record. Any help appreciated. Thank you in advance.

I have already started retrieving the data and am able to delete records but cannot update for some reason. It gets to the update page and when submit is clicked nothing happens and the data has not been updated.

<?
// START PHP CODES. THIS PART MUST ON THE TOP OF THIS PAGE.

// Connect database.
include("connectdb.php");

// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){

// Get parameters from form.
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];

// Do update statement.
mysql_query("update phonebook set name='$name', email='$email', tel='$tel' where id='$id'");

// Re-direct this page to select.php.
header("location:select.php");
exit;
}
// ************* End update part *************

// *** Select data to show on text fields in form. ***

// Get id parameter (GET method) from select.php
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from phonebook where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);

// Close database connection.
mysql_close();
?>

<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<body>
<!-- set this form to POST method and target this form to itself ($PHP_SELF;)-->
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name :
<!-- name of this text field is "name" -->
<input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/>
<br />
Email :
<!-- name of this text field is "email" -->
<input name="email" type="text" id="email" value="<? echo $row['email']; ?>"/>
<br />
Tel :
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="tel" value="<? echo $row['tel']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

borrob
February 21st, 2007, 03:52 AM
there is no id on your page so it will never be posted!

bwh2
February 21st, 2007, 10:50 AM
^yes there is, he uses $_GET

borrob
February 21st, 2007, 10:57 AM
no there is not!!!!

<?
// START PHP CODES. THIS PART MUST ON THE TOP OF THIS PAGE.

// Connect database.
include("connectdb.php");

// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){

// Get parameters from form.
$id=$_POST['id']; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];

// Do update statement.
mysql_query("update phonebook set name='$name', email='$email', tel='$tel' where id='$id'");

// Re-direct this page to select.php.
header("location:select.php");
exit;
}
!!!!!!!!!asks for a posted id but there is no id on your submitted form:!!!!!!!!!!
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name :
<!-- name of this text field is "name" -->
<input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/>
<br />
Email :
<!-- name of this text field is "email" -->
<input name="email" type="text" id="email" value="<? echo $row['email']; ?>"/>
<br />
Tel :
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="tel" value="<? echo $row['tel']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>

use a hidden input to post it with your form

<input type="hidden" name="id" value="<?php echo $row['id'] ;?>">

skrolikowski
February 21st, 2007, 02:23 PM
wispas, you should run the script and 'echo' out all your values to see if everything is being passed.

imvain2
February 21st, 2007, 02:54 PM
borrob is correct, the form isn't submitting the ID to the update script.

The only addition I have to borrob is a common sense one. Make sure to add his hidden field somewhere between the <form and the </form>:beam:

bwh2
February 21st, 2007, 03:07 PM
whoops. i wasn't looking at the form itself. in addition to the id not being there, i have a few other comments.
1) please use [noparse]
the code tags for your php,
for html.
2) this method is susceptible to SQL injections. you should [d-php]add-slashes[/d-php] the vars going into your query.
3) skrolikowski hit the nail on the head. when you're troubleshooting, you should echo out your vars to see what unexpected results you get.