vietfancy |
11-08-2009 10:02 AM |
Edit database with a php file
Could anyone please help me to edit the code so that it allowed me to edit the database as well!!!
I got something like this but it doesn't work.
PHP Code:
<? //connect to mysql include("./database.php");
//If action has not been initialized if(!isset($action)) { //display all the quotes $result = mysql_query("select * from quotes ORDER BY id LIMIT 0, 10"); //run the while loop that grabs all the quotess scripts while($r=mysql_fetch_array($result)) { //grab the quote and the ID of the data $quote=$r["quote"];//take out the quote $author=$r["author"];//take out the author $id=$r["id"];//take out the id //make the quote a link echo ""."$quote"." - "."$author"." - "; echo "<a href='edit.php?action=edit&id=$id'>Edit</a>"; echo "<br>"; } } ?> <? if($_REQUEST['action'] == "edit") { if (!isset($_POST["submit"])) { $id = $_REQUEST["id"]; $sql = "SELECT * FROM quotes 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"] ?>"> Quote:<TEXTAREA NAME="quote" ROWS=10 COLS=30><? echo $myrow["quote"] ?></TEXTAREA><br> Author:<INPUT TYPE="TEXT" NAME="author" VALUE="<?php echo $myrow["author"] ?>" SIZE=30><br> <input type="hidden" name="action" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } if ($_POST["$submit"]) { $quote = $_POST["quote"]; $author = $_POST["author"]; $sql = "UPDATE quotes SET quote=$quote author=$author WHERE id=$id"; //replace news with your table name above $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?>
|