* 在資料表新增資料
* 用到的函數:
* 1.mysqli_query(),
* 2.mysqli_errno(),mysqli_error (),
* 3.mysqli_affected_rows(),mysqli_insert_id(),
* 4.mysqli_close()
//1.連接資料庫##
define ('DB_HOST', 'localhost'); define ('DB_USER', 'root'); define ('DB_PASS', 'root'); define ('DB_NAME', 'php'); define ('DB_CHAR', 'utf8'); $db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS); if (mysqli_connect_errno($db)) { exit('连接失败'.mysqli_connect_error($db)); } mysqli_select_db($db, DB_NAME); mysqli_set_charset($db, DB_CHAR);
$sql = "INSERT INTO staff (staff_id,name,sex,age,salary) VALUES (null,'赵敏',1,30,4000)";
$sql = "INSERT staff (name,sex,age,salary) VALUES ('小昭',1,20,2400),('宋青书',0,40,1800),('成昆',0,70,9000)";
$sql = "INSERT staff SET name='灭绝师太',sex=1, age=58, salary=9999";
$res = mysqli_query($db, $sql); var_dump($res);exit; if (mysqli_query($db, $sql)) { if (mysqli_affected_rows($db) > 0) { //返回受影响的记录数与新增主键id echo '成功的新增了'.mysqli_affected_rows($db).'条记录,<br>新记录的主键id是:'.mysqli_insert_id($db); } else { echo '没有记录被新增'; } } else { //项目上线后,不应该将出错信息显示出来,否则会暴露数据库的相关信息 exit(mysqli_errno($db).':'.mysqli_error($db)); }
mysqli_close($db);