Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/182-phpmysql-update-error/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/182-phpmysql-update-error/#findComment-537
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182-phpmysql-update-error/#findComment-544
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.