Jump to content


php/mysql update error

  • Please log in to reply
4 replies to this topic

#1 HossMan

  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 February 2003 - 03:28 PM

Please bear with me, as I am very new to PHP. I am attempting to use a form to update records in a mysql database. I have a basic html form that enables the user to select which table they want to update, then after submitting the form, the php script will show all of the fields in that table. Once you select a record to edit, there are several text boxes that should automatically have the information filled in. You should then be able to edit the info in these text boxes and submit the changes, thus updating the record. The problem I am having is instead of receiving the text automatically filled into the text fields, I am getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

I am using a variable in the script to represent the table. The variable is $select_table. However, when I replace the variable with the actual table name instead, the script works without error. I only run into the problem when using variables. I would appreciate any and all help.

Here is a look at the php script.:




$db = mysql_connect(\"localhost\", \"root\");


if ($item) {

if ($submit) {

$sql = \"UPDATE $select_table SET
WHERE item=$item\";

$result = mysql_query($sql);

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

} else {

// query the DB

$sql = \"SELECT * FROM $select_table WHERE item=$item\";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);


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

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

manufacturer:<input type=\"Text\" name=\"manufacturer\" value=\"<?php echo $myrow[\"manufacturer\"] ?>\"><br>

product_name:<input type=\"Text\" name=\"product_name\" value=\"<?php echo $myrow[\"product_name\"] ?>\"><br>

year:<input type=\"Text\" name=\"year\" value=\"<?php echo $myrow[\"year\"] ?>\"><br>

scale:<input type=\"Text\" name=\"scale\" value=\"<?php echo $myrow[\"scale\"] ?>\"><br>

number:<input type=\"Text\" name=\"number\" value=\"<?php echo $myrow[\"number\"] ?>\"><br>

driver:<input type=\"Text\" name=\"driver\" value=\"<?php echo $myrow[\"driver\"] ?>\"><br>

sponsor:<input type=\"Text\" name=\"sponsor\" value=\"<?php echo $myrow[\"sponsor\"] ?>\"><br>

car_make:<input type=\"Text\" name=\" car_make\" value=\"<?php echo $myrow[\" car_make\"] ?>\"><br>

produced:<input type=\"Text\" name=\"produced\" value=\"<?php echo $myrow[\"produced\"] ?>\"><br>

serial:<input type=\"Text\" name=\"serial\" value=\"<?php echo $myrow[\"serial\"] ?>\"><br>

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

notes:<input type=\"Text\" name=\"notes\" value=\"<?php echo $myrow[\"notes\"] ?>\"><br>

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


} else {

// display list

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

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

printf(\"<a href=\"%s?item=%s\"><br><b>Manufacturer: </b>%s<br><b>Year:
</b>%s<br><b>Scale: </b>%s<br><b>Product Name: </b>%s<br><b>Car Number: </b>%s<br>
</a><br>n\", $PHP_SELF,
$myrow[\"manufacturer\"], $myrow[\"year\"],$myrow[\"scale\"],$myrow[\"product_name\"], $myrow[\"number\"] );




#2 shivabharat

  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 25 February 2003 - 11:26 PM

$result = SELECT * FROM $select_table WHERE item=$item

<?printf(\"Item: %s<br>n\", mysql_result($result,0,\"item\"));
<?printf(\"Man: %s<br>n\", mysql_result($result,0,\"manufacturer\"));
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 HossMan

  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 February 2003 - 12:36 PM

Forgive me, but I am new to php. I\'m not quite sure what you mean by your last post. Can you explain it a little? Thanks for your help!

#4 HossMan

  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 February 2003 - 02:27 PM

Also, I just changed the following line from:

$result = mysql_query($sql);


$result = mysql_query($sql) or die(\"Error : query failed.<br>MySQL said : <i>\".mysql_error().\"</i><br>\");

and this is the new error that I\'m getting:

Error : query failed.
MySQL said : You have an error in your SQL syntax near \'WHERE item=1\' at line 1

Any suggestions?

#5 pallevillesen

  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 28 February 2003 - 09:32 AM

ALWAYS do the following when debugging:

$sql = "UPDATE $select_table SET


WHERE item=$item";

echo "<pre>$sql</pre>";

$result = mysql_query($sql); 

You\'ll probably notice the error that way. Most likely a problem with missing space, wrong use of \' or something like that....

If you\'re stuck then post the echo result from above...

Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users