<?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());
}
王先生2019-12-12 11:12:59
ID is self-increasing and automatically generated. It can't be inserted.
王林2019-08-20 17:33:56
Add echo 'Insertion successful!' directly below $num = $pdo->exec($sq);
Please let me know if it is incorrect! Thanks!
天蓬老师2019-08-02 16:45:42
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?