Curiosity List Curiosity List - 2 days ago 4
MySQL Question

Connection to the DB is successful, but not visible in MySQL

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!

Answer

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);
Comments