guidance51x Posted October 19, 2009 Share Posted October 19, 2009 Hello Everyone This is a basic script for posting to a database. I'm not sure why it doesn't work. Can anyone tell me why? <?php require('config.php'); if(isset($_GET['id']) == TRUE && isset($_GET['td'])==TRUE){ if(is_numeric($_GET['id'])==FALSE || is_numeric($_GET['td'])==FALSE) { $error = 1; } if($error == 1){ header("Location: " . $config_basedir); } else{ $validentry = $_GET['id']; $validtd = $_GET['td']; } } else{ $validentry = 0; } if($_POST['submit']) { $db = mysql_connect($dbhost,$dbuser,$dbpassword); mysql_select_db($dbdatabase, $db); if(!$db){ die('FAILED TO OPEN DATABASE:' . mysql_error()); } $sql = "INSERT INTO todo(id, topic, date, comment) VALUES (" . $validentry . ", '" . $_POST['topic'] . "', '" . $_POST['date'] . "', '" . $_POST['comment'] . "');"; $result=mysql_query($sql); if(!$result){ die('FAILED TO INPUT INTO DATABASE:' . mysql_error()); } header("Location: index.php?id=" . $validentry); } ?> <center> **All dates must be inputted in the YYYY-MM-DD format** <br> <br> <form action="<?php echo $SCRIPT_NAME . "?id=" . $validentry; ?>" method="post"> <table> <tr> <td>Topic</td> <td><input type="text" name="topic"></td> </tr> <tr> <td>Date</td> <td><input type="text" name="lname"></td> </tr> <tr> <td>Comment</td> <td><input type="text" name="address"></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Submit Note"></td> </tr> </table> </form> </center> Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/ Share on other sites More sharing options...
jmr3460 Posted October 19, 2009 Share Posted October 19, 2009 This has worked for me: <?php $topic = $_POST['topic']; $date = $_POST['date']; $comment = $_POST['comment']; sql = "INSERT INTO todo(id, topic, date, comment) VALUES ('$validentry', '$topic', '$date', '$comment')"; ?> This way the double quotes don't get confused with the single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-939473 Share on other sites More sharing options...
guidance51x Posted October 19, 2009 Author Share Posted October 19, 2009 thanks for the reply. unfortunately it seems that isnt the problem Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-939480 Share on other sites More sharing options...
Coreye Posted October 19, 2009 Share Posted October 19, 2009 What's not inserting and do you get any errors? Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-939490 Share on other sites More sharing options...
guidance51x Posted October 19, 2009 Author Share Posted October 19, 2009 When i run the script as a page and try to submit the fields, they dont get inserted into the dbase. No errors. Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-939825 Share on other sites More sharing options...
mrMarcus Posted October 19, 2009 Share Posted October 19, 2009 change: $result=mysql_query($sql); to: $result=mysql_query($sql) or die ('Error:' . mysql_error()); see if you get any error there. Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-939849 Share on other sites More sharing options...
guidance51x Posted October 23, 2009 Author Share Posted October 23, 2009 nope. no error Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-942988 Share on other sites More sharing options...
mrMarcus Posted October 23, 2009 Share Posted October 23, 2009 you should really have error_reporting on. would have picked up the extra ; at the end of your query: $sql = "INSERT INTO todo(id, topic, date, comment) VALUES (" . $validentry . ", '" . $_POST['topic'] . "', '" . $_POST['date'] . "', '" . $_POST['comment'] . "');"; //;"; is no good; should be: $sql = "INSERT INTO todo(id, topic, date, comment) VALUES (" . $validentry . ", '" . $_POST['topic'] . "', '" . $_POST['date'] . "', '" . $_POST['comment'] . "')"; Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943015 Share on other sites More sharing options...
guidance51x Posted October 23, 2009 Author Share Posted October 23, 2009 still nothing Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943044 Share on other sites More sharing options...
mrMarcus Posted October 23, 2009 Share Posted October 23, 2009 no idea, man .. what is $SCRIPT_NAME? where is it being defined? Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943095 Share on other sites More sharing options...
guidance51x Posted October 24, 2009 Author Share Posted October 24, 2009 $SCRIPT_NAME refers to the script itself. the bottom half is the html of the form while the top half is the part processing the data Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943371 Share on other sites More sharing options...
PFMaBiSmAd Posted October 24, 2009 Share Posted October 24, 2009 What does happen in the browser when you submit the form? Does it just redisplay the form or does it redirect to index.php? For debugging purposes, please add the following two lines of code immediately after your first opening <?php tag - ini_set("display_errors", "1"); error_reporting(E_ALL); FYI, the extra semicolon ; on the end of the query statement would normally be used when you execute the query directly against the mysql server, but it is optional when executing a query through php. It is not a php or an sql error. Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943373 Share on other sites More sharing options...
PFMaBiSmAd Posted October 24, 2009 Share Posted October 24, 2009 Once you start debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON, you will find that your date and comment fields in your form don't have the correct name="..." attributes. Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943375 Share on other sites More sharing options...
guidance51x Posted October 24, 2009 Author Share Posted October 24, 2009 When i submit the code it redirects to index.php and looks like nothing ever changed. The first thing i saw was this message on top of my script: Notice: Undefined index: submit in C:\xampp\htdocs\sites\smart\addtodo.php on line 20 when i try to submit something, the page 404's with this in the URL box: http://localhost/sites/web/%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20SCRIPT_NAME%20in%20%3Cb%3EC:%5Cxampp%5Chtdocs%5Csites%5Csmart%5Caddtodo.php%3C/b%3E%20on%20line%20%3Cb%3E43%3C/b%3E%3Cbr%20/%3E?id=1 I'm guessing it has a problem with $SCRIPT_NAME or ?id=1... Any help wit clarification? Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943556 Share on other sites More sharing options...
DavidAM Posted October 24, 2009 Share Posted October 24, 2009 1) When you first load the page $_POST['submit'] is not defined, so change that IF to if (isset($_POST['submit'])) { 2) $SCRIPT_NAME is undefined. You are not assigning it anywhere in the code you posted. Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943588 Share on other sites More sharing options...
PFMaBiSmAd Posted October 24, 2009 Share Posted October 24, 2009 Seven years ago $SCRIPT_NAME would have been valid. Either use $_SERVER['SCRIPT_NAME'] or you can just leave off the script/file name in the action="..." attribute and use - action="?id=<?php echo $validentry; ?>" Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943592 Share on other sites More sharing options...
guidance51x Posted October 24, 2009 Author Share Posted October 24, 2009 OK! Everything fixed. Thanks a lot to everyone. props to PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/178180-solved-script-not-running-properly/#findComment-943719 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.