equality4xy Posted February 23, 2009 Share Posted February 23, 2009 For some reason I can't get this to update the DB. I can't even get it to display an error why. Any ideas? ?php // Connects to your Database mysql_connect("XXX.XXX.XXX", "XXXX", "XXXX") or die(mysql_error()); mysql_select_db("XXXX") or die(mysql_error()); //If cmd has not been initialized if(!isset($cmd)) { //display all the events $result = mysql_query("select * from jos_jtimer order by id"); //run the while loop that grabs all the countdowns while($r=mysql_fetch_array($result)) { //grab the id, eventName, Year, Month, Day, Hour and Minute and seconds of the events $id=$r["id"];//take out the id $eventName=$r["eventName"];//take out the eventName $targetYear=$r["targetYear"];//take out the targetYear $targetMonth=$r["targetMonth"];//take out the targetMonth $targetDay=$r["targetDay"];//take out the targetDay $targetHour=$r["targetHour"];//take out the targetHour $targetMinute=$r["targetMinute"];//take out the targetMinute $targetSecond=$r["targetSecond"];//take out the targetSecond //make the eventName a link echo "<a href='edit.php?cmd=edit&id=$id'>$eventName</a>, $targetMonth-$targetDay-$targetYear $targetHour:$targetMinute:$targetSecond"; echo "<br>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["submit"])) { $id = $_GET["id"]; $sql = "SELECT * FROM jos_jtimer WHERE id=$id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); // specify the target date here ?> <form action="edit.php" method="post"> <input type=hidden name="id" value="<?php echo $myrow["id"] ?>"> Event:<INPUT TYPE="TEXT" NAME="eventName" VALUE="<?php echo $myrow["eventName"] ?>" SIZE=30><br> Year:<INPUT TYPE="TEXT" NAME="targetYear" VALUE="<?php echo $myrow["targetYear"] ?>" SIZE=4>(4 digits i.e. 2009)<br> Month:<INPUT TYPE="TEXT" NAME="targetMonth" VALUE="<?php echo $myrow["targetMonth"] ?>" SIZE=2>(2 digits)<br> Day:<INPUT TYPE="TEXT" NAME="targetDay" VALUE="<?php echo $myrow["targetDay"] ?>" SIZE=2>(2 digits)<br> Hour:<INPUT TYPE="TEXT" NAME="targetHour" VALUE="<?php echo $myrow["targetHour"] ?>" SIZE=2>(2 digits)<br> Minute:<INPUT TYPE="TEXT" NAME="targetMinute" VALUE="<?php echo $myrow["targetMinute"] ?>" SIZE=2>(2 digits)<br> Seconds:<INPUT TYPE="TEXT" NAME="targetSecond" VALUE="<?php echo $myrow["targetSecond"] ?>" SIZE=2>(2 digits)<br> <input type="hidden" name="cmd" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } ?> <? if ($_POST["$submit"]) { $eventName = $_POST["eventName"]; $targetYear = $_POST["targetYear"]; $targetMonth = $_POST["targetMonth"]; $targetDay = $_POST["targetDay"]; $targetHour = $_POST["targetHour"]; $targetMinute = $_POST["targetMinute"]; $targetSecond = $_POST["targetSecond"]; $sql = "UPDATE jos_jtimer SET eventName='$eventName',targetYear='$targetYear',targetMonth='$targetMonth',targetDay='$targetDay',targetHour='$targetHour',targetMinute='$targetMinute',targetSecond='$targetSecond' WHERE id=$id"; //replace jos_jtimer with your table name above $result = mysql_query($sql) or die (mysql_error()); echo "Thank you! Information updated."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146531-solved-phpmysql-not-updating-db/ Share on other sites More sharing options...
JonnoTheDev Posted February 23, 2009 Share Posted February 23, 2009 This is incorrect if ($_POST["$submit"]) Use if ($_POST['submit']) Quote Link to comment https://forums.phpfreaks.com/topic/146531-solved-phpmysql-not-updating-db/#findComment-769286 Share on other sites More sharing options...
equality4xy Posted February 23, 2009 Author Share Posted February 23, 2009 Thank you, that did it. I used the code from here... http://www.spoono.com/php/tutorials/tutorial.php?id=23 So, I was really thinking it was not the functions so much as my syntax, even though I had read on a forum almost the same solution, I thought, that can't be right. Quote Link to comment https://forums.phpfreaks.com/topic/146531-solved-phpmysql-not-updating-db/#findComment-769318 Share on other sites More sharing options...
JonnoTheDev Posted February 23, 2009 Share Posted February 23, 2009 If I were you I would tidy that code up also. It is best practice to reload the page after any update/insert query. This prevents a user hitting refresh on their browser and causing multiple updates or inserts into your database. The code that inserts/updates the database is best placed at the very top of your file (before any HTML or data is printed to the screen). That way you can use the header() function to reload the page i.e. if($_POST['submit']) { // update database mysql_query("UPDATE table SET name='".mysql_real_escape_string($_POST['name'])."' WHERE id='".mysql_real_escape_string($_POST['id'])."'"); // reload page to prevent user refresh header("Location:xyz.php?id=".$_POST['id']."&update=success"); exit(); } // display a message if update was ok if($_GET['update'] && $_GET['update'] == 'success') { echo "Thank you. Your record was updated successfully"; } Quote Link to comment https://forums.phpfreaks.com/topic/146531-solved-phpmysql-not-updating-db/#findComment-769321 Share on other sites More sharing options...
equality4xy Posted February 24, 2009 Author Share Posted February 24, 2009 once again neil.johnson , thank you. The page not refreshing was irksome and something I had a mind to remedy, but did not know how to go about it yet, so you read my mind. Quote Link to comment https://forums.phpfreaks.com/topic/146531-solved-phpmysql-not-updating-db/#findComment-769749 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.