Delaran Posted April 23, 2007 Share Posted April 23, 2007 Hey guys, I'm a PHP noob. I am using SQLite and PHP 5.0.5 on my server. Now that y'all know my background, here's my problem... I'm coding a page with text fields using $_POST data. I have pageone.php which has the text fields, a submit button, etc. and then I have pagetwo.php which is my "action" page. I am attempting to insert the data into my database from pageone's $_POST data, but have run into some problems. I have checked the forums here as well as googled my problem, but it seems most people are using MySQL on this site. I hope I'm not out of line to ask this question and if I am -- I'll understand if this thread is locked/deleted. My code is as follows (shortened to the problem area): // set path of database file $db = $_SERVER['DOCUMENT_ROOT']."/../wantdb.sqlite2"; // open database file $handle = sqlite_open($db) or die("Could not open database"); // check to see if the form was submitted with a new record if (isset($_POST['submit'])) { // make sure all fields are present if (!empty($_POST['name']) && !empty($_POST['item']) && !empty($_POST['phone']) && !empty($_POST['email'])) { // generate and execute query $name = sqlite_escape_string($_POST['name']); $item = sqlite_escape_string($_POST['item']); $phone = sqlite_escape_string($_POST['phone']); $email = sqlite_escape_string($_POST['email']); $query = "INSERT INTO wantdb (name, item, phone, email) VALUES ('".$name."', '".$item."', '".$phone."', '".$email."')"; $result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle))); echo "<i>Record successfully inserted with ID ".sqlite_last_insert_rowid($handle)."!</i><p />"; } else { // missing data // display error message echo "<i>Incomplete form input. Record not inserted!</i><p />"; --------------------------------------- As you can see, I have four text fields I want inserted into the db with $_POST data. I get no error messages so I assume that the database has received the information, but when I use "select * from wantdb" it retrieves only the two I manually entered beforehand. Nothing from the web form shows up. So my question(s) are: 1) Should I place this code onto pageone.php, or leave it on pagetwo.php? 2) Is this code correct in the syntax, or have I added/omitted something necessary? I greatly appreciate anyone's help or if you could link another thread where this has been solved. I looked but could not find anything related to an SQLite topic; most of the threads revolved around MySQL. ~delaran Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/ Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 in your insert statement you do not need the periods...try this line.... $query = "INSERT INTO wantdb (name, item, phone, email) VALUES ('$name', '$item', '$phone', '$email')"; Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236344 Share on other sites More sharing options...
Delaran Posted April 23, 2007 Author Share Posted April 23, 2007 Tried that just now -- still nothing. Gives me no errors and when I check the database it still has nothing in it. Could there be something else? ??? Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236346 Share on other sites More sharing options...
bobleny Posted April 23, 2007 Share Posted April 23, 2007 Try this: $query = "INSERT INTO wantdb (name, item, phone, email) VALUES ('".$name."', '".$item."', '".$phone."', '".$email."')"; echo $query; die(); Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236348 Share on other sites More sharing options...
Delaran Posted April 23, 2007 Author Share Posted April 23, 2007 Still nothing, no echo of the query either. I appreciate all the help, thanks guys. Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236350 Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 $query = "INSERT INTO wantdb (name, item, phone, email) VALUES (\''.$name.'\'', \''.$item.'\', \''.$phone.'\', \''.$email.'\')"; Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236351 Share on other sites More sharing options...
Delaran Posted April 23, 2007 Author Share Posted April 23, 2007 Copied and pasted in, used scp to transfer my file from my computer to the server, tested it via the webpage, then queried the database with select * from wantdb, and still nothing... This one is a tough one, I think I tried some of those code snippets before and got nothing.. Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236356 Share on other sites More sharing options...
bobleny Posted April 23, 2007 Share Posted April 23, 2007 If you used this: $query = "INSERT INTO wantdb (name, item, phone, email) VALUES ('".$name."', '".$item."', '".$phone."', '".$email."')"; echo $query; die(); And it didn't echo the query, I think you have another issue at play. That should have echoed the query... Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236360 Share on other sites More sharing options...
Delaran Posted April 23, 2007 Author Share Posted April 23, 2007 Alright.. I inserted that code again and got no echo. Below is the -entire- code of the page. I hope I'm not breaking any rules. Now, should I pull anything out of this code or add something in? Maybe a username/pw? <html> <head> <title>Web-based customer requests</title> </head> <center> <br> <br> Thank you <?php echo htmlspecialchars($_POST['name']); ?>, we look forward to helping you find the item you are looking for. <br> Upon finding the item, we will contact you at <?php echo (int)($_POST['phone']); ?>, or by email at <?php echo htmlspecialchars($_POST['email']); ?>. </center> <?php // set path of database file $db = $_SERVER['DOCUMENT_ROOT']."/../wantdb.sqlite2"; // open database file $handle = sqlite_open($db) or die("Could not open database"); // check to see if the form was submitted with a new record if (isset($_POST['submit'])) { // make sure all fields are present if (!empty($_POST['name']) && !empty($_POST['item']) && !empty($_POST['phone']) && !empty($_POST['email'])) { // generate and execute query $name = sqlite_escape_string($_POST['name']); $item = sqlite_escape_string($_POST['item']); $phone = sqlite_escape_string($_POST['phone']); $email = sqlite_escape_string($_POST['email']); $query = "INSERT INTO wantdb (name, item, phone, email) VALUES (\''.$name.'\'', \''.$item.'\', \''.$phone.'\', \''.$email.'\')"; echo $query; die(); $result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle))); echo "<i>Record successfully inserted with ID ".sqlite_last_insert_rowid($handle)."!</i><p />"; } else { // missing data // display error message echo "<i>Incomplete form input. Record not inserted!</i><p />"; } } // all done // close database file sqlite_close($handle); ?> Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236364 Share on other sites More sharing options...
mpharo Posted April 23, 2007 Share Posted April 23, 2007 You are using die() before the query is executed...remove that outta there and try it....die should be used for error handling...or forcibly terminating a script... Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236382 Share on other sites More sharing options...
Delaran Posted April 23, 2007 Author Share Posted April 23, 2007 Alright, so this thread can be "SOLVED", here is what was wrong with my code, I modified it so just take a look at the above code and match it to the one below to see what I removed (it appears to be the code that was supposed to check if the text fields were submitted with something in them, or blank fields): <html> <head> <title>Web-based customer requests</title> </head> <center> <br> <br> Thank you <?php echo htmlspecialchars($_POST['name']); ?>, we look forward to helping you find the item you are looking for. <br> Upon finding the item, we will contact you at <?php echo (int)($_POST['phone']); ?>, or by email at <?php echo htmlspecialchars($_POST['email']); ?>. </center> </html> <?php // set path of database file $db = $_SERVER['DOCUMENT_ROOT']."/../wantdb.sqlite2"; // open database file $handle = sqlite_open($db) or die("Could not open database"); { // generate and execute query $name = sqlite_escape_string($_POST['name']); $item = sqlite_escape_string($_POST['item']); $phone = sqlite_escape_string($_POST['phone']); $email = sqlite_escape_string($_POST['email']); $query = "INSERT INTO wantdb (name, item, phone, email) VALUES ('".$name."', '".$item."', '".$phone."', '".$email."')"; $result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle))); echo "<i>Record successfully inserted with ID ".sqlite_last_insert_rowid($handle)."!</i><p />"; } // all done // close database file sqlite_close($handle); ?> Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236405 Share on other sites More sharing options...
bobleny Posted April 24, 2007 Share Posted April 24, 2007 You are using die() before the query is executed...remove that outta there and try it....die should be used for error handling...or forcibly terminating a script... To clarify, that was to test the $query line. I find it helpful to kill it after such a thing to prevent the rest of the scrip from interfering. We thought the problem was the $query, so if you check to see what exactly is being queryed, you might discover a problem. He was not getting anything echoed, which means there is another problem. Because of the "die();", you know the problem is above the "die();". Just a small little check I leaned from Wildteen I think... Link to comment https://forums.phpfreaks.com/topic/48342-solved-_post-data-and-php-code/#findComment-236559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.