Updating mySQL data using PHP forms. – PHP

  php

Q(Question):

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.


<?php
$author=$_POST['author'];
$date=$_POST['date'];
$shortstory=$_POST['story'];
mysql_connect ("dbserver", "user", "pass") or die ('Error: ' .mysql_error());
mysql_select_db ("dbname");
$query = "INSERT INTO blog (author, date, shortstory) VALUES ('".$author."', '".$date."', '".$shortstory."')";
mysql_query($query) or die ('Error updating database');
echo "Database Updated! <br />
Data Inserted : <br />
";
?>

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…

A(Answer):

Did you try that with MySQL Update query?

A(Answer):

…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 w3schools.com tutorials and you’ll get there.

Ronald

A(Answer):

Did you try that with MySQL Update query?

Yeah, I know that, but how could I edit through a PHP form?

A(Answer):

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.

A(Answer):

Hi,
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.

Regards,
RP

A(Answer):

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…

LEAVE A COMMENT