Jump to content

A Much More Basic Update MySQL Form - Still can't get it to work.


suttercain

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
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.