suttercain Posted April 9, 2007 Share Posted April 9, 2007 Hi everyone, I have been working on this since 11:00AM and have not yanked my hair out... yet. I tried doing this with a bigger form but could not get it to work. So I went back to square one and decided to to go basic and build from there. I have a form that is being populated from MySQL. When I change any of the values in the form and select submit I would like it to update the record in MySQL. I cannot get this. I have managed to get INSERT and DELETE going okay... but not UPDATE <?php require ('get_connected.php'); $reload= true; if (isset($_POST['title'])) { $title= mysql_real_escape_string($_POST['title']); // Assuming only one row is in the table, otherwise WHERE clause is needed $res= mysql_query("UPDATE TABLE news SET title='$title' WHERE story_id='1'"); $reload= false; } else { $title= ''; } if ($reload) { // Populate the from $hent = mysql_query("SELECT title FROM news WHERE story_id='1'"); while ($vis = mysql_fetch_array($hent)){ $title = $vis['title']; } } ?> <form method='post' action="edit_news.php"> <textarea rows="1" cols="40" name="title"><?php echo $title ?></textarea> <input type="hidden" name="story_id" value="<?php echo $story_id; ?>"> <input type='submit' value='submit'> </form> Can anyone make any suggestions or lend some adivse? Thank you in advance for your help. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 9, 2007 Share Posted April 9, 2007 Write your queries like this, and you'll get helpful error messages: $sql = "UPDATE TABLE news SET title='$title' WHERE story_id='1'"; $res= mysql_query($sql) or die("Error in $sql\n" . mysql_error()); It takes more time to type, but it will save you MUCH more time in debugging. And you can just copy and paste. The other option is to make a small function that does error checking for all your queries, and use that wrapper function instead of mysql_query() Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 Thank you Btherl. I have implemented that and will force the habit. After making the changes you advised I got the following error: Error in UPDATE TABLE news SET title='HellO!!!!' WHERE story_id='1' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE news SET title='HellO!!!!' WHERE story_id='1'' at line 1 Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 Doh... I got it. Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 But now I did this: I manually changed the WHERE in both statements to be story_id'3' instead of one to see if it worked on another record, but it's not populating the form with the title from that record it is instead repopulating the form with what ever I last entered. It still updates, but it isn't populating from the database now. I tried unset($_POST['title']); but that didn't work. How do I get it to populate from MySQL instead of the last text I entered? Thanks guys. Quote Link to comment Share on other sites More sharing options...
suttercain Posted April 9, 2007 Author Share Posted April 9, 2007 I'm still here... Can anyone tell me if the code look messed up to them or point me to a basic tutorial or premade script? I have 3 books, done a google, yahoo, phpfreaks search and no luck. Quote Link to comment 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.