-1

I've viewed a couple of questions related to this and most seem to be answered by simple syntax errors. I don't think my problem is syntax however.

I am connecting to my db successfully, but I cannot seem to see my entries in phpmyadmin (where I am viewing MySQL). I can echo my entries on another page as a variable, but I believe my input isn't going into the database.

Here is my html code:

 <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>student info</title>
    </head>
    <body>
        <br>
        Enter your first name and last name in the corresponding boxes.
        <br>
        <form  action="submit.php" method="POST">
            First: <input type="text" name="firstname"/>
        <br>
            Last: <input type="text" name="lastname"/>
        <br>
        <input type="submit">
        </form>


    </body>
</html>

My php for the database connection:

<?php
echo 'here';
    $dsn = 'mysql:host=localhost;dbname=practice_students';


    try {
        $db = new PDO($dsn);
        echo 'db connection success';
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        include('database_error.php');
        exit();
    }

?>

And my php for the submission page:

<?php
echo 'here ';
    $dsn = 'mysql:host=localhost;dbname=practice_students';


    try {
        $db = new PDO($dsn);
        echo 'db connection success';
        $firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING, 
                FILTER_SANITIZE_SPECIAL_CHARS);
        $lastname = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING,
                FILTER_SANITIZE_SPECIAL_CHARS);
        echo "Now we know your name! Hi," . " " . $firstname . " " . $lastname;
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        include('database_error.php');
        exit();
    }
?>

All of which prompt the response successfully in my local host

here db connection successNow we know your name! Hi, Maggie Bowen

However, MySQL shows no entries when I try to CHECK or SELECT *.

enter image description here

enter image description here

How can I see my entries? I know some of my sanitizing etc. can be improved, but I would really just like to know how to see my entries and ensure they are entered into the table. Thank you!

1 Answers1

3

You have the data $firstname and $lastname. Now you have to insert them into the database submitting a query using PDO::query().

Something like this:

$q = "INSERT INTO people (column1, column2) VALUES ('$firstname', '$lastname')";

$db->query($q);

EDIT Use prepared statements to avoid SQL Injection Attacks

Wikipedia says

Prepared statements are resilient against SQL injection, because parameter values, which are transmitted later using a different protocol, need not be correctly escaped. If the original statement template is not derived from external input, SQL injection cannot occur.

So the reviewed code

$stmt = $db->prepare("INSERT INTO people (column1, column2) VALUES (:firstname, :lastname)";

$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);

$stmt->execute();

Thanks to the guys from the comments!

Jacopo
  • 130
  • 1
  • 1
  • 9
  • 1
    `VALUES ($firstname, $lastname)` - I doubt those are integers here. Strings require to be quoted. Otherwise, what you posted will throw syntax errors. This for future readers to the Q&A who may think that what you wrote is valid syntax; which it isn't. – Funk Forty Niner Nov 30 '16 at 17:38
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 30 '16 at 17:44
  • 1
    Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Nov 30 '16 at 17:45
  • @JayBlanchard is right though. A prepared statement is much better than basic escaping (PHP.net didn't see the future back then) ;-) Your answer would attract more votes when referencing prepared statements. Just trying to help ;-) – Funk Forty Niner Nov 30 '16 at 17:48
  • Really thanks for the help guys, i really appreciated it! – Jacopo Nov 30 '16 at 18:24
  • @JacopoBontà *prego* ;-) e grazie! – Funk Forty Niner Nov 30 '16 at 20:21