sách gpt4 ai đã đi

php - PDO 更新 帮助执行 pdo 更新

In lại 作者:行者123 更新时间:2023-11-29 12:32:01 31 4
mua khóa gpt4 Nike

该功能非常简单:

变量:$table是正在更新的表$fields 是表中的字段,$values 从帖子生成并放入 $values 数组中而$where是表的索引字段的id值$indxfldnm 是索引字段名称

function SQLUpdate($table,$fields,$values,$where,$indxfldnm) {

//Connect to DB
$dbaddr = DB_HOST;
$dbusr = DB_USER;
$dbpwd = DB_PASSWORD;
$dbname = DB_DATABASE;

$db = new PDO('mysql:host='.$dbaddr .';dbname='.$dbname.';charset=UTF8', $dbusr, $dbpwd);

//build the fields
$buildFields = '';
if (is_array($fields)) {

//loop through all the fields
foreach($fields as $key => $field) :
if ($key == 0) {
//first item
$buildFields .= $field;
} khác {
//every other item follows with a ","
$buildFields .= ', '.$field;
}
endforeach;

} khác {
//we are only inserting one field
$buildFields .= $fields;
}

//build the values
$buildValues = '';
if (is_array($values)) {

//loop through all the values
foreach($values as $key => $value) :
if ($key == 0) {
//first item
$buildValues .= '?';
} khác {
//every other item follows with a ","
$buildValues .= ', ?';
}
endforeach;

} khác {
//we are only updating one field
$buildValues .= ':value';
}
$sqlqry = 'UPDATE '.$table.' SET ('.$buildFields.' = '.$buildValues.') WHERE `'.$indxfldnm.'` = \''.$where.'\');';

$prepareUpdate = $db->prepare($sqlqry);
//execute the update for one or many values
if (is_array($values)) {
$prepareUpdate->execute($values);
} khác {
$prepareUpdate->execute(array(':value' => $values));
}

//record and print any DB error that may be given
$error = $prepareUpdate->errorInfo();
if ($error[1]) print_r($error);



echo $sqlqry;
return $sqlqry;
}

到目前为止一切顺利然而它不起作用在正确的更新语句中将值传输到字段中时出现问题但我不太擅长 pdo 和设置它对修复将参数绑定(bind)到更新中的值的代码有一点帮助不胜感激

Cảm ơn

1 Câu trả lời

尝试将其添加到您的函数中

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

thử {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

// Prepare statement
$stmt = $conn->prepare($sql);

// execute the query
$stmt->execute();

// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "
" . $e->getMessage();
}

$conn = null;
?>

关于php - PDO 更新 帮助执行 pdo 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27341954/

31 4 0
Bài viết được đề xuất: php - MYSQL 按标签查询类似/相关产品
Bài viết được đề xuất: PHP/mysql 忽略带有 Asterix 字符的记录
Bài viết được đề xuất: mysql - MySQL 和 ASP.NET MVC4 的级联删除
Bài viết được đề xuất: ios - Xcode 6.1 在 iPad Air 和 iPad Retina 之间的大小差异
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com