
<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","password");
//select which database you want to edit
mysql_select_db("spoono_news");
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from news order by id");
//run the while loop that grabs all the news scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$title=$r["title"];//take out the title
$id=$r["id"];//take out the id
//make the title a link
echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit</a>";
echo "<br>";
}
}
?>
The second part of the tutorial involves displaying the information from the mySQL row which you can edit. Take a look at the coding:
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM news WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
Title:<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
Message:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["message"] ?></TEXTAREA><br>
Who:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["who"] ?>" SIZE=30><br>
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
<? } ?>
Alright, that looks confusing too, but again its simpler than it looks. The second part works right after you click the Title in the first part. The $cmd variable has been set to "edit" and no sign of a $submit variable exists. So, we select the post from the news that matches the $id sent from part one. We fetch that row and display the Title, the Message, and Who submitted it. We finally display a hidden variable that defines $cmd to "edit" again and this time create a submit button and $submit variable.
<?
if ($_POST["$submit"])
{
$title = $_POST["title"];
$message = $_POST["message"];
$who = $_POST["who"];
$sql = "UPDATE news SET title='$title',message='$message',who='$who' WHERE id=$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}
?>
The third part is the easiest but the most important. $cmd="edit" and $submit is true, so that means we have the edited data and all we have to do is send it back. We update the 'news' table with all the new information, where title, message, and who are rows in the mySQL table. Finally, we echo out that the information has been updated. A lot of people ask if all this code can all be placed on a single page, and the answer is yes, thats the way its supposed to work :)Copyright © 2000-2010 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.