Forum Moderators: coopster

Message Too Old, No Replies

Help with PHP / mysql database UPDATE SCRIPT

         

eric11

2:14 pm on Sep 21, 2011 (gmt 0)

10+ Year Member



Hi,
Im just in the middle of creating an update script for my mysql database but don't know why it's not working. p.s. I'm a little new to PHP, but know quite a bit, it's probably something really small.. *facepalm*

Here's the script:

the form (update.php)
<?
// Connect to the database
$link = mysql_connect('###', '###', '###');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('###', $link);

$id = $_GET['id'];

// Ask the database for the information from the links table
$query="SELECT * FROM orders WHERE id='$id'";
$result = mysql_query("SELECT * FROM orders");
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"Name");
$location=mysql_result($result,$i,"Location");
$fault=mysql_result($result,$i,"Fault");

?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo "$id";?>">
Name: <input type="text" name="ud_name" value="<? echo "$name"?>"><br>
Location: <input type="text" name="ud_location" value="<? echo "$location"?>"><br>
Fault: <input type="text" name="ud_fault" value="<? echo "$fault"?>"><br>

<input type="Submit" value="Update">
</form>

<?
++$i;
}
?>

------------------------------------------------------
(processor) updated.php

<?php
// Connect to the database
$link = mysql_connect('###', '###', '###');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('###', $link);

$query="UPDATE orders SET Name='" . $_POST['ud_name'] . "', Location='" . $_POST['ud_location'] . "', Fault='" . $_POST['ud_fault'] . "' WHERE $id='" . $_POST['ud_id'] . "'";
echo $query;
$checkresult = mysql_query($query);
if ($checkresult) echo '<p>update query succeeded';
else echo '<p>update query failed';

mysql_close();
?>


------------------------------------------------------

Every time I want to update, it comes up with:

UPDATE orders SET Name='TEST', Location='TEST', fault='jbjh' WHERE =''
update query failed

Any help would be appreciated. :)

penders

3:21 pm on Sep 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$query="UPDATE orders SET Name='" . $_POST['ud_name'] . "', Location='" . $_POST['ud_location'] . "', Fault='" . $_POST['ud_fault'] . "' WHERE $id='" . $_POST['ud_id'] . "'";


You are not setting $id to anything in your updated.php script, so no column name is being output in the resulting SQL - hence the error. This should also be triggering a warning, along the lines of "undefined variable...". If you are not seeing this then your error_reporting might be set too low (for developing you need to see all errors, warnings and notices)...

error_reporting(E_ALL);