mikew2 Posted August 1, 2006 Share Posted August 1, 2006 Hi guys, Im trying to edit a row in mysql using php. This is my code:[quote]<? //connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","root","admin"); //select which database you want to editmysql_select_db("ict"); //If cmd has not been initializedif(!isset($cmd)) { //display all the news $result = mysql_query("select * from contact 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>"; }}?><?if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit"){ if (!isset($_POST["submit"])) { $id = $_GET["id"]; $sql = "SELECT * FROM contact 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> Content:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA><br> Date:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["date"] ?>" SIZE=30><br> <input type="hidden" name="cmd" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } ?><? if ($_POST["$submit"]) { $title = $_POST["title"]; $content = $_POST["content"]; $date = $_POST["date"]; $sql = "UPDATE contact SET title='$title',content='$content',date='$date' WHERE id=$id"; $result = mysql_query($sql); echo "Thank you! Information updated."; }}?>[/quote]It works fine until i hit submit and then it just goes back to the edit.php page. And doesnt update the row. It also doesnt display the "Thank you! Information updated." However there are no mysql errors.Any help would be much appreciated ThanksMike Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 Try changing[code] if ($_POST["$submit"])[/code]to[code] if ($_POST["submit"])[/code] Quote Link to comment Share on other sites More sharing options...
mikew2 Posted August 1, 2006 Author Share Posted August 1, 2006 ah thanks. But ive still got a problem. Now it says "Information has been updated" however it hasnt actually updated the row. any clues? Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 1, 2006 Share Posted August 1, 2006 No clue yet, but you'd better inspect the $result of the mysql_query and display the mysql error messge if not correct.Ronald ;D Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 Hmm I'm unsure about this[code]if(!isset($cmd))[/code]What is your $cmd ? Quote Link to comment Share on other sites More sharing options...
mikew2 Posted August 1, 2006 Author Share Posted August 1, 2006 hmm im not quite sure what you mean. sorry im quite new at this.and ronald there isnt any mysql errors. Which is odd. Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 You have wrote this "//If cmd has not been initialized"But does it actually means ? What does that cmd do ? Quote Link to comment Share on other sites More sharing options...
mikew2 Posted August 1, 2006 Author Share Posted August 1, 2006 it means if the edit query has been started. Then display the titles. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted August 1, 2006 Share Posted August 1, 2006 What is your primary key? You need to update it as well. I am assuming it is "id", if it is, this should work:[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","root","admin"); //select which database you want to editmysql_select_db("ict");//If cmd has not been initializedif(!isset($cmd)){ //display all the news $result = mysql_query("select * from contact 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[/url]"; echo ""; }}?><?if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit"){ if (!isset($_POST["submit"])) { $id = $_GET["id"]; $sql = "SELECT * FROM contact 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> Content:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA> Date:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["date"] ?>" SIZE=30> <input type="hidden" name="cmd" value="edit"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <input type="submit" name="submit" value="submit"> </form> <? } ?><? if ($_POST["$submit"]) { $title = $_POST["title"]; $content = $_POST["content"]; $date = $_POST["date"]; $id = $_POST["id"]; $sql = "UPDATE contact SET title='$title',content='$content',date='$date',id='$id' WHERE id=$id"; $result = mysql_query($sql); echo "Thank you! Information updated."; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 Try change[code]<form action="edit.php" method="post">[/code]to[code]<form action="<?php echo $_SERVER['PHP_SELF']; ?>?cmd=edit&id=$id" method="post">[/code] Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 1, 2006 Share Posted August 1, 2006 Uh, How could I change this for my use?? Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 [quote author=techiefreak05 link=topic=102548.msg407114#msg407114 date=1154427316]Uh, How could I change this for my use??[/quote]Create a new thread and ask for help :P Quote Link to comment Share on other sites More sharing options...
techiefreak05 Posted August 1, 2006 Share Posted August 1, 2006 I was about to, but then I saw this and thought it was easier just to post in here.. but i didnt know if I could start a new one, but now I know, thanks. Quote Link to comment Share on other sites More sharing options...
mikew2 Posted August 1, 2006 Author Share Posted August 1, 2006 hmm none of that changed anything. however when i added mysql_error to the query. i got this:[quote]Line 66:MySQL Error:Incorrect date value: '' for column 'date' at row 1[/quote]I dont understand tho because the date is in the right format Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 Hmm.. Try chaning the date table to "text". Quote Link to comment Share on other sites More sharing options...
mikew2 Posted August 1, 2006 Author Share Posted August 1, 2006 that works hacker, but its not really a solution. Quote Link to comment Share on other sites More sharing options...
mikew2 Posted August 1, 2006 Author Share Posted August 1, 2006 lol i just realised the answer rofl. Im kicking myself. The name of my date input element was called "who" and not "date" OMFOGMGOMFOI MFOM OMROFL OROFL LOL :pthanks for the help guysmike[b]EDITED BY WILDTEEN88: REMOVED LANGUAGE. PLEASE DO NOT SWEAR IN POSTS. THERE ARE MINORS THAT BROWSE AROUND THIS FORUM[/B] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.