wlpywd Posted March 3, 2007 Share Posted March 3, 2007 OK, i guess i need to post code in my question? couldn't quite figure out the other way we were trying, so now trying to have an update page for entries in our database. Putting together bits and pieces of knowledge and sample code, we are left with something that was working and as we've evolved it, its not working. Can anyone help and see somthing thats wrong? Thank you, $id=$_GET['id']; $con = mysql_connect("********", "*****", "*****"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("*****", $con); $query=" SELECT * FROM usa WHERE id='$id'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $first=mysql_result($result,$i,"date"); $last=mysql_result($result,$i,"name"); $fax=mysql_result($result,$i,"color"); $email=mysql_result($result,$i,"discription"); <form method="post"> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> Date: <input type="text" name="ud_date" value="<? echo $date; ?>"><br> Customer: <input type="text" name="ud_name" value="<? echo $name; ?>"><br> Color: <input type="text" name="ud_color" value="<? echo $color; ?>"><br> Discription: <input type="text" name="ud_discription" value="<? echo $discription; ?>"><br> <input type="Submit" value="Update"> </form> $ud_id=$_POST['ud_id']; $ud_date=$_POST['ud_date']; $ud_name=$_POST['ud_name']; $ud_color=$_POST['ud_color']; $ud_discription=$_POST['ud_discription']; $query="UPDATE contacts SET date='$ud_date', name='$ud_name', color='$ud_color', discription='$ud_discription' WHERE id='$ud_id'"; mysql_query($query); echo "Record Updated"; mysql_close(); ++$i; } Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/ Share on other sites More sharing options...
JBS103 Posted March 3, 2007 Share Posted March 3, 2007 Is this the full code? I'm sort of confused because I see a block of HTML in the middle of PHP with no open or closing tags. I suggest putting a die() on the end of the query so you can get some errors and see what is going on. mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198361 Share on other sites More sharing options...
Archadian Posted March 3, 2007 Share Posted March 3, 2007 if you have the PHP in the same script as the form you need to have the script rerun when the submit button is clicked you also need an "action" in your <form> tag. <form method="post" action="<php? $_SERVER['PHP_SELF']; ?>"> Also, you need an if statement so the PHP part of the code that gets the info from the form doesn't run until its supposed to Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198363 Share on other sites More sharing options...
wlpywd Posted March 3, 2007 Author Share Posted March 3, 2007 I've taken out the posting action to a separate file, update5.php: $ud_id=$_POST['ud_id']; $ud_date=$_POST['ud_date']; $ud_name=$_POST['ud_name']; $ud_color=$_POST['ud_color']; $ud_discription=$_POST['ud_discription']; $query="UPDATE contacts SET date='$ud_date', name='$ud_name', color='$ud_color', discription='$ud_discription' WHERE id='$ud_id'"; mysql_query($query); echo "Record Updated"; mysql_close(); and then the update.php file now looks like this with the html tags out of the middle of code, i think. Is this what it should look like? cuz it is still not working for me... i am getting an error "Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /*************/update.php on line 16" <? $id=$_GET['id']; $con = mysql_connect("********", "*****", "*****"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("*****", $con); $query=" SELECT * FROM usa WHERE id='$id'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $first=mysql_result($result,$i,"date"); $last=mysql_result($result,$i,"name"); $fax=mysql_result($result,$i,"color"); $email=mysql_result($result,$i,"discription"); ?> <form action="update5.php" method="post"> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> Date: <input type="text" name="ud_date" value="<? echo $date; ?>"><br> Customer: <input type="text" name="ud_name" value="<? echo $name; ?>"><br> Color: <input type="text" name="ud_color" value="<? echo $color; ?>"><br> Discription: <input type="text" name="ud_discription" value="<? echo $discription; ?>"><br> <input type="Submit" value="Update"> </form> <? ++$i; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198380 Share on other sites More sharing options...
Archadian Posted March 3, 2007 Share Posted March 3, 2007 its mysql_num_rows() not mysql_numrows() Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198384 Share on other sites More sharing options...
wlpywd Posted March 3, 2007 Author Share Posted March 3, 2007 fixed it and getting this error message: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /*************/update.php on line 16 Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198385 Share on other sites More sharing options...
Archadian Posted March 3, 2007 Share Posted March 3, 2007 i've had problems with * after SELECT try putting in a column from your usa table where the * is to see if that gets rid of the error, if it does then its the * and make sure something in the id column matches $id Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198387 Share on other sites More sharing options...
wlpywd Posted March 3, 2007 Author Share Posted March 3, 2007 i am looking over this code and i am confused... I can sorta follow what is suppose to happen, but will this pull each record from the table?? or is there nothing to update from this code? We eventually want, and tried to do at first, the 'update' button to appear in a separate column on the display table page, a page where we display the data from the database, but couldn't get it to work. This code was saved from our first attempt at doing that. As i look at it, i think the * was used so it would pull for all the entries, i'm not sure how it would work stand alone. Am i looking at this too long and confusing myself for no reason?? also, tried to replace * and id with an entry and still get the same line 16 error! Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198390 Share on other sites More sharing options...
Archadian Posted March 3, 2007 Share Posted March 3, 2007 if you are trying to "update" a row...you need to use the UPDATE query instead of SELECT mysql_query("UPDATE table SET column1 = '$variable', column2 = '$variable' WHERE id = '$id'"); Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198393 Share on other sites More sharing options...
wlpywd Posted March 3, 2007 Author Share Posted March 3, 2007 isn't that what the <form action="update5.php" method="post"> is for? with update5.php posted above: $ud_id=$_POST['ud_id']; $ud_date=$_POST['ud_date']; $ud_name=$_POST['ud_name']; $ud_color=$_POST['ud_color']; $ud_discription=$_POST['ud_discription']; $query="UPDATE contacts SET date='$ud_date', name='$ud_name', color='$ud_color', discription='$ud_discription' WHERE id='$ud_id'"; mysql_query($query); echo "Record Updated"; mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/40968-update-form-code-something-very-wrong/#findComment-198396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.