Forum Moderators: mack

Message Too Old, No Replies

php update database

problem updating the database wth php form

         

CriamP

9:26 am on Mar 30, 2004 (gmt 0)

10+ Year Member



Hi all, I am using the following tutorial script to try and show data in fields and then edit back to the database. I am posting $staffid to the page from a dropdown menu, the problem I am having is that there is no result when submitted, the script jumps to the end and just displays the employees.
Any help would be great. cheers.

<html>

<body>

<?php
//from post
$staffid = $_POST['staffmember'];
if($staffid == "0")
{
echo ("error");
}

$db = mysql_connect("localhost", "james", "d3st");
mysql_select_db("test",$db);

if ($staffid) {

if ($submit) {

$sql = "UPDATE employees SET firstname='$first',surname='$last',email='$address',title='$position' WHERE ID=$staffid";

$result = mysql_query($sql);

echo "Thank you! Information updated.\n";

} else {

// query the DB

$sql = "SELECT * FROM employees WHERE ID=$staffid";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

?>

<form method="post" action="<?php echo $PHP_SELF?>">

<input type=hidden name="ID" value="<?php echo $myrow["ID"]?>">

First name:<input type="Text" name="first" value="<?php echo $myrow["firstname"]?>"><br>

Last name:<input type="Text" name="last" value="<?php echo $myrow["surname"]?>"><br>

Address:<input type="Text" name="address" value="<?php echo $myrow["email"]?>"><br>

Position:<input type="Text" name="position" value="<?php echo $myrow["title"]?>"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php

}

} else {

// display list of employees

$result = mysql_query("SELECT * FROM employees",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["ID"], $myrow["firstname"], $myrow["surname"]);

}

}

?>

</body>
</html>

aus_dave

10:00 pm on Mar 30, 2004 (gmt 0)

10+ Year Member



After your 'thank you' line, try adding something like this:

echo "<p>First name: $first</p>";

and so on for all your form variables. That will help you to see if you are passing any values to the MySQL query and narrow down the source of the error. Try echoing some text inside the if statements as well, to show where your script is up to.

killswitch

10:21 am on Mar 31, 2004 (gmt 0)

10+ Year Member



Just looking over your script and noticed the if statement coming before the database update query checks a variable ($submit) that is not being set anywhere in the script (thus always returning false and never executing the update query).

Another thing you might run into problems with later is that $staffid is not surrounded by quotes as are your other variables in the update query.

Oh yeah, one other thing in the form action I have always seen PHP_SELF being accessed through the $_SERVER global variable such as $_SERVER['PHP_SELF'], which I guess doesn't matter if you have register_globals enabled. Although, php recommends setting register_globals to off for newer versions of php.

lol I've edited this thing like 3 times already...

One other suggestion that may help you later for mysql is to put an or die("This is the message that prints if the operation fails."); function after the mysql operations.

So you could do something like:
$update = mysql_query($query) or die("Query failed to run.");

CriamP

1:04 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



cheers for your help and suggestions, I have managed to get the script working, and as said above I was not actually posting to the db. I am using the error checking which is helping a lot. thanks again.