Correction status:Uncorrected
Teacher's comments:
更新代码
<?php // 预处理技术实现更新操作 //第一步连接数据库 define('db_host','127.0.0.1'); define('db_user','root'); define('db_pas','root'); define('db_name','php'); define('db_char','utf8'); $db = mysqli_connect(db_host,db_user,db_pas); //第二步准备SQL语句 $sql = "UPDATE class SET grade=? WHERE class_id=2"; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)){ mysqli_stmt_bind_param($stmt, "l", $grade); $grade = 90; mysqli_stmt_execute($stmt); echo '<br>新增了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);
删除代码
<?php define('db_host','127.0.0.1'); define('db_user','root'); define('db_pas','root'); define('db_name','php'); define('db_char','utf8'); $db = mysqli_connect(db_host,db_user,db_pas); if ($db != false){ echo '连接成功'; }else { echo '连接失败'; } $sql = "DELETE IGNORE * FROM class WHERE name=?"; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt, $sql)) { mysqli_stmt_bind_param($stmt, 'i',$name); $name='张三'; mysqli_stmt_execute($stmt); }else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);