How to create a simple PHP edit page for SQL

A

Anonymous

Guest
First you should review your MySQL book to see how you would make your changes using only MySQL. Then it becomes simple in PHP. All you have to do is create a simple HTML form using POST as the method like so...

Code:
<HTML>
<HEADER>
</HEADER>
<BODY>
<FORM Method="post" action="submit.php">
<H1>
Input something
</H1>
<input type="text" name="something" size=30 maxlength=40>
<input type="submit" name="submit" value="add something">
</BODY>
</HTML>

Then make a second file called "submit.php" with a php script like this
Code:
<?php

//connect to mysql
        $conn = mysql_connect('localhost', 'root', '');
        if (!$conn) {
        die('Could not connect: ' . mysql_error());
 	}
//select the database
mysql_select_db("testDB",$conn);
// create the SQL statement note that the first '' in the () are for an autoincrementing id in testTable
$sql = "INSERT INTO testTable values ('', '$_POST[something]')";
// execute the SQL statement
if (mysql_query($sql, $conn)) {
	echo "record added!";
} else {
	echo "something went wrong";
}
?>

I didn't test that, but it looks right. It's close anyways. Give it a try!

Cheers!

Justin
 
Well kennedyr4, you can see from fishermanjuice's example how to insert the data into the database.
The secret now is in the SQL part.

So like fishermanjuice showed, you have your HTML form (using post method).
First, you must retrieve the data from the database and print/output it inside some form field value, so you have it ready to edit.
Example:
Code:
...
<input name="first_name" value="<?php print $data['name']; ?>" alt="" />
...
Now you just need to use SQL's UPDATE (instead of INSERT query)... to change the data in table.
 
Back
Top