Insert into does not put data into database, yet not errors produced

I have an insert into statement that runs that doesn’t insert data into database, but there are no errors that show me where the insert into statement fails. I looked all over for a similar problem but I couldn’t find anything with my particular problem. Everybody else insert into statement that fails show errors. I would appreciate if someone else could look at my code and perhaps see something that I’m missing.

session_start();
require_once 'debase.php';
if (isset($_POST['user_name']) && !empty($_POST['user_name'])) {

try{
    $conn = new PDO($dsn,$username,$password);
    $stmt = $conn->prepare("INSERT INTO name(user_name,password)VALUES(?,?");
    $stmt->bindValue(1,'user_name');
    $stmt->bindValue(2,'$password');
    $stmt->execute();
    $stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    header("location: home.php");
    
}catch(PDOException $e){
$error_message = $e->getMessage();
echo("Error description: " . mysqli_error($stmt));
}
}

Could this be the reason for no error message?
You are using PDO.

I’m not sure about that. I actually coded this app along with a video I saw on YouTube. https://www.youtube.com/watch?v=9Q0UHtcGmEY&t=1748s

I have trouble keeping up with coding videos, they usually move along too fast for me to absorb everything.

But I did not see

catch(PDOException $e){
$error_message = $e->getMessage();
echo("Error description: " . mysqli_error($stmt));
}

What I saw was

catch(PDOException $ex){
echo $ex->getMessage();
}
1 Like

I don’t understand your reply. Your first block of code is exactly what I have.

Yes, and that is what I did not see in the video. And it is what you are having problems with.

In other words, because you are echo-ing something that does not exist, you should be echo-ing something that does (might) exist. At least while you’re working up the code. For production use you should take out all of the developer “show me” stuff.

Right under $stmt->execute();
I tried that with if(!$stmt){
echo “Connection failed”;
}
The app ran and redirected to home.php but didn’t show an errors. What do you mean by developer “show me” stuff?
I even tried under $stmt->execute();
if (!$stmt) {
echo “\nPDO::errorInfo():\n”;
print_r($dbh->errorInfo());
}
The code ran, redirected to home.php without an error. When I check the database there was nothing inserted.

Ah, your code example did not show a redirect.

If you have a “output this to the current page then go to a different page” you’ll have to look quick to see it.

By “developer show me stuff” I mean information that comes in useful while developing but should not be shown to site visitors. Logging is much better.

You could try this to avooid the redirect from happening.

catch(PDOException $ex){
echo $ex->getMessage();
exit;
}

What I meant by redirect is that the page was redirected to the header("location: home.php); There is a select statement in home.php that show the data on the home page in the browser that I put in the database in xampp;The select statement works.

This doesn’t look right to me

$stmt->bindValue(2,'$password');

Why are there single-quotes around the variable name? And presumably you’re setting the value of $password somewhere in your code that isn’t shown above, as you’re also using the same variable to connect to the database.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.