[英]Update a mysqli script into PDO statement
我是菜鸟,我正在PDO领域中迈出第一步,我正在尝试将此脚本从mysqli更新为PDO。 该脚本用于通过jQuery从数据库中删除记录。
与mySql DB的新连接:
$host = "localhost";
$user = "root";
$password = "";
$dbname = "test-2";
try {
$con = new PDO("mysql:host={$host};dbname={$dbname}", $user, $password);
}
// show error
catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
问题与DELETE脚本有关。 这是原始的(摘自本教程: https : //makitweb.com/how-to-de-delete-record-from-mysql-table-with-ajax ),其中包含旧连接的引用。
include "config.php";
$id = $_POST['id'];
if($id > 0){
// Check record exists
$checkRecord = mysqli_query($con,"SELECT * FROM gallery WHERE id_gallery=".$id);
$totalrows = mysqli_num_rows($checkRecord);
if($totalrows > 0){
// Delete record
$query = "DELETE FROM gallery WHERE id_gallery=".$id;
mysqli_query($con,$query);
echo 1;
exit;
}
}
echo 0;
exit;
和我要更新的相同脚本。 它不起作用,因为它给了我“ 无效ID ”警报。
include "config.php";
$id = $_POST['id'];
if($id > 0){
// Check record exists
$checkRecord = "SELECT * FROM gallery WHERE id_gallery=".$id;
$stmt = $con->prepare($checkRecord);
$stmt->bindParam(1, $id);
$stmt->execute();
$totalrows = $stmt->fetchColumn()
if($totalrows > 0){
// Delete record
$query = "DELETE FROM gallery WHERE id_gallery=".$id;
$stmt = $con->prepare($query);
$stmt->bindParam(1, $id);
$stmt->execute();
echo 1;
exit;
}
}
echo 0;
exit;
的script.js
$(document).ready(function(){
$('.delete').click(function(){
var el = this;
var id = this.id;
var splitid = id.split("_");
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax({
url: 'remove.php',
type: 'POST',
data: { id:deleteid },
success: function(response){
if(response == 1){
// Remove row from HTML Table
$(el).closest('tr').css('background','tomato');
$(el).closest('tr').fadeOut(800,function(){
$(this).remove();
});
}else{
alert('Invalid ID.');
}
}
});
});
});
您原始的mysqli代码或PDO代码实际上都没有创建任何绑定参数。
你有这个:
$checkRecord = "SELECT * FROM gallery WHERE id_gallery=".$id;
$stmt = $con->prepare($checkRecord);
$stmt->bindParam(1, $id);
$stmt->execute();
毫无疑问,当您请求bindParam()时,PDO会变得混乱,因为您没有在SQL查询中放置任何参数占位符。 “绑定参数...要做什么?” 它是正确的奇迹。
您应该使用此:
$checkRecord = "SELECT * FROM gallery WHERE id_gallery=?";
$stmt = $con->prepare($checkRecord);
$stmt->execute([$id]);
在PDO :: prepare()的文档中找到更多代码示例。
第一个问题,我真的需要运行此选择吗? 我建议下面的代码
include "config.php";
$id = $_POST['id'];
if($id > 0){
// Delete record
$checkRecord = "DELETE FROM gallery WHERE id_gallery= :id ";
$stmt = $con->prepare($checkRecord);
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
// It executes the delete, if any record is deleted, it will print "Registration deleted"
if($stmt->execute()){
echo "Registration deleted";
}else{
echo "Id does not exist";
}
}
exit;
我还建议始终使用PDO::PARAM_...
,因为它将为您的应用程序带来更多安全性
– PDO :: PARAM_STR –参数值字符串,数据,消息…
– PDO :: PARAM_INT –半价inteiros
– PDO :: PARAM_BOOL – val val booleano(true ou false)
– PDO :: PARAM_NULL – val nulo(空)
– PDO :: PARAM_LOB – dados的瓦莱雷斯·格兰德·奎迪达德
– PDO :: PARAM_STMT –代表驱动程序的驱动程序,驱动程序的驱动程序
– PDO :: PARAM_INPUT_OUTPUT –具体说明“存储过程”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.