Hi,

I have this code. It works fine using PHP 4 and MySQL 5.0. but it doesn't work at all on my webhost (php4 mySQL 4) and I cannot for the life of me figure out why.

Here's my code:

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

//if "Submit" button is clicked
if($_POST){

//Get parameters from form
$id=$_GET;
$name=$_POST;
$email=$_POST;
$tel=$_POST;

//Update database record
mysql_query("update phonebook set name='$name', email='$email', tel='$tel' where id='$id'");

//Redirect to record view page
header("location:select.php");
exit;
}
// ************* End update part *************


//Get record id to display data for record to be updated
$id=$_GET;

//Put all the results from the query into $result
$result=mysql_query("select * from phonebook where id='$id'");

//Split result into rows and put in $row
$row=mysql_fetch_assoc($result);

?>

<!-- END PHP. This is the HTML form for updating the record -->

<html>
<body>
<!-- set this form to POST method and target 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; ?>"/>
<br />
Email :
<!-- name of this text field is "email" -->
<input name="email" type="text" id="email" value="<? echo $row; ?>"/>
<br />
Tel :
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="tel" value="<? echo $row; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

---end update.php----

For some reason this fails to update the db, but doesn't give an error. The exact same code works perfectly on my development system (php4 and mysql 5.0).

When I try to do the following in phpmyadmin on my webhost it works fine:

UPDATE phonebook SET name='test', email='test', tel='123' WHERE id='2';

But somehow it doesn't work when i use the update.php script.

The weirdest thing is that I can do a lookup query just fine, for example the code below works just fine and it also uses the same $id variable.

$result=mysql_query("select * from phonebook where id='$id'");
$row=mysql_fetch_assoc($result);

I'm at my wits end and don't know what else to try. Do you have any suggestions?

I don't see anywhere in your form the value of 'id'
Place a hidden field in your form to hold this value so the SQL statement will read the value and update.

Hmm, I tried that. I added a hidden field with the called record with the value of id and then tried to pick it up at the beginning of the if statement instead of $id=$_GET[id] i now have $id = $_POST[record]. But still I don't get my record updated. I even tried to see if my variables even receive the $_POST data and they do...I can echo the data out...just can't update it to the db somehow.

try this:

<?php

include("connectdb.php");

if(isset($_POST['Submit'])){

$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];

$sql = "UPDATE phonebook SET name = '" . $name . "', email = '" . $email . "', tel = '" . $tel . "' WHERE id = '" . $id . "'";
$query = mysql_query($sql);

if ($query) {
header("location:select.php");
exit;
}
}

$id = $_REQUEST['id'];

$result=mysql_query("select * from phonebook where id='$id'");

$row=mysql_fetch_assoc($result);

?>

why do you put ' ' to assign numbers to the variables. it usually gives error.

where id=$id")

This article has been dead for over six months. Start a new discussion instead.