By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,888 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,888 IT Pros & Developers. It's quick & easy.

Updating mySQL data using PHP forms.

P: 2
I've done a simple PHP-thing which adds data into my mySQL database, using that "PHP $_POST" thing, now I want to do editing that data through PHP form again, but I don't know how to do that. I've tried once, but it failed.

Expand|Select|Wrap|Line Numbers
  1. <?php
  3. $author=$_POST['author'];
  4. $date=$_POST['date'];
  5. $shortstory=$_POST['story'];
  7. mysql_connect ("dbserver", "user", "pass") or die ('Error: ' .mysql_error());
  8. mysql_select_db ("dbname");
  10. $query = "INSERT INTO blog (author, date, shortstory) VALUES ('".$author."', '".$date."', '".$shortstory."')";
  12. mysql_query($query) or die ('Error updating database');
  14. echo "Database Updated! <br />
  15. Data Inserted : <br />
  16. ";
  17. ?>
Now, I wanted to do is if I add a "news" then, I'll be able to edit that, just like in blogs or news cms.

I'm just a newbie in PHP, so I don't know much yet in PHP and mySQL...
May 3 '08 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 1,654
Did you try that with MySQL Update query?
May 3 '08 #2

Expert 2.5K+
P: 4,258
...using that "PHP $_POST" thing...

I'm just a newbie in PHP, so I don't know much yet in PHP and mySQL...
When this is the level you want to discuss a PHP problem, you'd better think twice and follow some PHP tutorials before you continue any coding. Start with the tutorials and you'll get there.

May 4 '08 #3

P: 2
Did you try that with MySQL Update query?
Yeah, I know that, but how could I edit through a PHP form?
May 4 '08 #4

P: 5
Pass the edited info from the form to another page and then use a update quiery to store the info and then use the header function to relocate the browser to what ever page.
May 4 '08 #5

P: 188
It is similar to what you've done so far. Instead of INSERT query you need to use UPDATE query in your code which you've shown as an example. The steps are similar to what you are doing just replace INSERT with PROPER UPDATE statement.

May 5 '08 #6

Expert 100+
P: 1,654
First fetch the same data from database by SELECT query. Then fill that data in the value attribute of the inputs in your form, and then after user clicks submit, run the UPDATE query.[php]$sql = mysql_query("SELECT * FROM.......") or die(mysql_error());
$info = mysql_fetch_array($sql); //if your data is only on one row
echo "<input type=\"text\" name=\"data\" value=\"".$info['data']."\" />";
//--- and in the updating portion
$sql = mysql_query("UPDATE ........") or die(mysql_error());[/php]

Hope it helps...
May 5 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.