Jump to content


Photo

php/mysql update error


  • Please log in to reply
4 replies to this topic

#1 HossMan

HossMan
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 February 2003 - 03:28 PM

Hi,
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.:

<html>

<body>

<?php



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

mysql_select_db(\"nascar\",$db);

if ($item) {

if ($submit) {

$sql = \"UPDATE $select_table SET
manufacturer=\'$manufacturer\',product_name=\'$product_name\',year=\'$year\',scale=\'$scale\',number=\'$number\',driver=\'$driver\',spons$
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\">

</form>

} 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[\"item\"],
$myrow[\"manufacturer\"], $myrow[\"year\"],$myrow[\"scale\"],$myrow[\"product_name\"], $myrow[\"number\"] );

}

}

?>
</body>
</html>

#2 shivabharat

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

HossMan
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 27 February 2003 - 12:36 PM

Hi,
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

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);

to

$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?
Thanks!

#5 pallevillesen

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

manufacturer=\'$manufacturer\',product_name=\'$product_name\',year=\'$year\',scale=\'$scale\',number=\'$number\',driver=\'$driver\',spons$

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...

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users