I want to INSERT, but I can't.(PDO)

Asked 2 years ago, Updated 2 years ago, 73 views

7/24 14:55 Add (1)
I chose $dbh->prepare instead of $pdo->prepare, and after INSERT, I entered INTO, and it looked like this:

Connected.ERROR:SQLSTATE [3D000]:Invalid catalog name:1046 No database
selected

If you create a file called php.pdo and access it with localhost/php.pdo, it will only show "Connected".I would like to insert data into phpmyadmin, but what is missing at this stage?I would like to insert data into the table called friend in the database called personl.Thank you very much for letting me know.

<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_NAME', 'personal');
     
// Error display settings: Show all but notification    
error_reporting(E_ALL&~E_NOTICE);
     
try{
         
$dbh = new PDO ('mysql:'.DB_NAME.';'.DB_HOST, DB_USER, DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
print('Connected.');
>             
// Create SQL statement (add new record)
 $sql="INSERT friend(name, price)VALUES
("French fries", 100),
("Tacos", 200),
("Fried Chicken", 300);
// Create Prepared Statements
$stm = $pdo->prepare($sql);
// Run SQL statements
$stm->execute();           
}
catch(PDOException$e){
print('ERROR:'.$e->getMessage());
exit;
}
?>

Enter a description of the image here

php phpmyadmin

2022-09-30 19:06

1 Answers

print followed by an unnecessary string, so it might have stopped there?

 print('Connected.');
>

Since PHP errors are probably no longer printed, you should add error_reporting to the beginning of the file to display the errors.

error_reporting(E_ALL); // Display all errors


2022-09-30 19:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.