Jump to content

[SOLVED] $_POST data and PHP code


Delaran

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.