2

I have been trying to make a project where I need to upload information to a sqlite3 database. For that I'm using simple PHP scripts.

I succeeded already uploading information from a PHP script to a database with something like this:

    <?php

    try
    {
    $db = new PDO('sqlite:mydatabase.db');

    $db->exec("INSERT INTO temps (zone,temperature) VALUES ('maia',77)");
    echo "Row Inserted\n";
    }
    catch(PDOException $e)
    {
            print $e->getMessage();
    }
    ?>

Now I am struggling to do the same with a script lie this:

<?php

        $data = htmlspecialchars($_GET["temp1"]);
        $file = "temps.txt";
        $current = file_get_contents($file);
        $current .= $data;
        file_put_contents($file, $current);

try
{
        $db = new PDO('sqlite:teste1.db');
        $db->exec('BEING;');
        $db->exec('INSERT INTO temps (temperature) VALUES ($temp1)');
        $db->exec('COMMIT;');
}
catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
}
?>

My table "temps" has a schema like this:

CREATE TABLE temps (temperature NUMERIC);

Is it because of the var type in the PHP since I declared it as numeric in the database? If so how can I solve that?

Appreciate all your ideas.

Thank you

7
  • $db->exec('BEING;'); should probably be $db->exec('BEGIN;'); Also $db->exec('INSERT INTO temps (temperature) VALUES ($temp1)'); should be $db->exec("INSERT INTO temps (temperature) VALUES ($temp1)");. Using a single quote is a literal (so you can't use variables inside it). What errors are you receiving? Commented Aug 26, 2015 at 23:31
  • No errors. I will change that right now Commented Aug 26, 2015 at 23:32
  • Make sure you also add in the edit I just did Commented Aug 26, 2015 at 23:32
  • Edited everything like you said. No errors and still no value in the database. I have an empty entry in the database tho Commented Aug 26, 2015 at 23:36
  • $temp1 is a variable I get with GET method. Its a numeric value that represents a temperature Commented Aug 27, 2015 at 8:01

1 Answer 1

1

You might be interested in prepapred statements and (named|positional) parameters:

<?php
$temp1 = '1234';
try
{
    $db = new PDO('sqlite::memory:');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec('CREATE TABLE temps (temperature NUMERIC)');

    $stmt = $db->prepare('INSERT INTO temps (temperature) VALUES (?)');
    $stmt->execute( array($temp1) );
}
catch (PDOException $e) {
    echo $e->getMessage();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for your answer, but I think I don't get what you're saying. Why are you creating the table again?
Oh sorry, it's a self-contained example (i.e. you can copy&paste the script and run it without anything else). The database is kept only in memory (sqlite::memory:), therefore the table has to be created each time.
I understand. Didn't knew I could do that (but there are many things I dont know too). But it's not going to solve my problem. Let me show you the big picture: This is a part of a project where I'm using an arduino to measure temperatures from some pipes, send them through the wifi shield to a PHP script and store the information in a database.
The relevant part are only the two lines containing prepare and execute, the remaining script is boilerplate. Read the linked page about prepared statements.

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.