1

I have the following code in a php file on my server, and it's supposed to take simple POST input from a HTML form and store it in a database.

$var_name = $_POST[post_name];
$var_email = $_POST[post_email];
$var_id = intval($_POST[post_id]);
echo "Name=".$var_name.", email=".$var_email.", id=".$var_id;

$status = $db->query("insert into names (name, email, id) values ('$var_name', '$var_email', $var_id)");
var_dump($status);

All of the POST variables are correct, they all store the data I entered into the form, and db is definitely set up correctly, as running the following code works as expected, printing out each line in the database:

$results = $db->query('SELECT * FROM names');
while ($name = $results->fetchArray()) 
    echo $name[0]."\n";

What happens is, var_dump($status) proves that $status is false. How can it be false? What am I doing wrong? Inserting the query line into the sqlite3 CLI works perfectly, so I can't see what's wrong. Thanks

4
  • put your query in a variable and var_dump that. $sql = "insert into names (name, email, id) values ('$var_name', '$var_email', $var_id)". use the same variable in the query method. This way you can make sure the query is correct Commented Aug 24, 2013 at 19:32
  • What does your database's error report say? If you're using PDO, then you can access it with var_dump($query->ErrorInfo()) Commented Aug 24, 2013 at 19:33
  • 3
    Never write such dangerous code. Use prepared statements from day one and never look back: php.net/manual/en/pdo.prepared-statements.php Commented Aug 24, 2013 at 19:34
  • 1
    The query is: << insert into names (name, email, id) values ('a', 'b', 1) >>.. There's nothing wrong with this, but it's still telling me that $status is false... Commented Aug 24, 2013 at 19:50

2 Answers 2

1

There is probably a permissions problem with your database, since the code looks like it should work. Check that you have read/write permissions on the database file.

Also, you should make sure that the folder containing the database file also has read/write permissions as well, because both the folder and the file must be writable in order for an INSERT to work

Sign up to request clarification or add additional context in comments.

Comments

0

I see that you have forget some quotes in your code.. begin to try this:

$var_name = $_POST['post_name'];
$var_email = $_POST['post_email'];
$var_id = intval($_POST['post_id']);
echo "Name=".$var_name.", email=".$var_email.", id=".$var_id;

$status = $db->query("insert into names (name, email, id) values ('$var_name', '$var_email', $var_id)");
var_dump($status);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.