<?php
/**
Use PDO to complete the database connection
*/
require 'pdo_conf.php';
try
{
$pdo = new PDO($dsn,$userName ,$password);
//echo '<h3>Connection successful</h3>';
$sql = "INSERT student (id,name,course,grade) VALUES ('11',Wu Song ','mysql','80')";
$num = $pdo->exec($sql);
$insertId = $pdo->lastInsertId();
if($ NUM & GT; 0) {
PRINT 'successfully added'. $ NUM. 'Receives, and the new primary key ID was'. $ insertid;
}
} cach (pdoexception $ e) {
die('Operation failed'.$e->getMessage());
}
ID is self-increasing and automatically generated. It can't be inserted.
Add echo 'Insertion successful!' directly below $num = $pdo->exec($sq);
Please let me know if it is incorrect! Thanks!
The database connection is successful, no problem
I don’t see your connection code, not sure if your connection is successful? You can print out the SQL statement and see if it is correct? Or directly execute it in the SQL command line to locate the error?