使用php-mysql更新PDO数组
您好,我正在尝试编写一个代码来更新或编辑调查答案和每个答案的注释,但是当我执行提交表单的功能时,它没有将任何值保存到数据库中。我能做些什么来修复它 我是PDO的新手 提前谢谢 数据库结构使用php-mysql更新PDO数组,php,mysql,arrays,pdo,survey,Php,Mysql,Arrays,Pdo,Survey,您好,我正在尝试编写一个代码来更新或编辑调查答案和每个答案的注释,但是当我执行提交表单的功能时,它没有将任何值保存到数据库中。我能做些什么来修复它 我是PDO的新手 提前谢谢 数据库结构 "Questions" (idquestion, question) "Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number) 更新功能 public function ModifySurveyMulti(
"Questions" (idquestion, question)
"Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)
更新功能
public function ModifySurveyMulti($answer = array())
{
if(!empty($answer)) {
foreach($answer as $questi => $value ) {
$this->MyDB->Write("UPDATE survey SET(
`idquestion` = '".$questi."',
`answers` = '".$value[0]."',
`comments_per_answer`= '".$_POST["comment"][$questi]."')");
}
}
}
modify_surveyform.php
<?php
// I'm adding my PDO database because yours is deprecated
class DBConnect
{
public $con;
// Create a default database element
public function __construct($host = '',$db = '',$user = '',$pass = '')
{
try {
$this->con = new PDO("mysql:host=$host;
dbname=$db",$user,
$pass, array(
PDO::ATTR_ERRMODE
=> PDO::ERRMODE_WARNING
)
);
}
catch (Exception $e) {
return 0;
}
}
// Simple fetch and return method
public function Fetch($_sql)
{
$query = $this->con->prepare($_sql);
$query->execute();
if($query->rowCount() > 0) {
while($array = $query->fetch(PDO::FETCH_ASSOC)) {
$rows[] = $array;
}
}
return (isset($rows) && $rows !== 0 && !empty($rows))? $rows: 0;
}
// Simple write to db method
public function Write($_sql)
{
$query = $this->con->prepare($_sql);
$query->execute();
}
}?>
您需要做的几件事:
- 首先,抛开这段代码,它是无用的,让您接触sql
注射
- 直接将PDO与准备好的语句一起使用
- 您需要的查询是:
您将需要调整类以仅创建连接
class DBConnect
{
public $con;
public function __construct($host = '',$db = '',$user = '',$pass = '')
{
try {
$this->con = new PDO(
"mysql:host=$host;dbname=$db",
$user,$pass,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING)
);
}
catch (Exception $e) {
die($e);
}
}
public function get_connection(){
return $this->con;
}
}
这样您就可以像这样创建它:
$db = new DBConnect(/*pass arguments here*/);
$this->MyDB = $db->get_connection();
在功能中修改并使用它:
public function ModifySurveyMulti($answer = array(), $comments)
{
$sql = 'UPDATE survey SET(`answers`= ?,`comments_per_answer`= ?)
WHERE idquestion = ?';
$stmt->prepare($sql);
foreach($answer as $questi => $value ) {
$stmt->execute(array($value, $comments[$questi],$questi));
$count = $stmt->rowCount();
echo $count > 0 ? $questi.' updated' : $questi.' did not update';
}
}
调用函数:
if(isset($_POST['answer'], $_POST['comments'])){
$answers = $_POST['answer'];
$comments = $_POST['comments'];
ModifySurveyMulti($answers, $comments);
}
使用PDO的主要原因之一是支持参数化查询,但您仍然只是直接从表单连接值。你有没有研究过这些文件?只要您不向我们显示$this->MyDB->Write()
的代码,我们就无法帮助您…我已经添加了mydbconnect.php,这里有我目前正在使用的所有pdo函数。@dcft您检查与数据库的连接了吗?对我来说,PDO对象采用奇数连接字符串mysql:host=$host;dbname=$db“
(或者您的主机和数据库名是否真的被称为$host
和$db
,它们应该被硬编码的值替换,或者假设的变量需要转义)。我还注意到您将
提前关闭(在rows属性是结束标记之后)rows=“3“/>
.Hell@Sascha我创建连接时插入此代码$con=new DBConnect('localhost','sistema\u bss','root',”)
进入包含更新函数的文件。抱歉@meda我不得不再次发布,因为我不理解你的答案,我添加了你的连接类,但它向我发送了一个带有一些变量的错误,我不知道为什么?发生这种情况时,你应该回来告诉我错误,以便我可以调整我的答案
public function ModifySurveyMulti($answer = array(), $comments)
{
$sql = 'UPDATE survey SET(`answers`= ?,`comments_per_answer`= ?)
WHERE idquestion = ?';
$stmt->prepare($sql);
foreach($answer as $questi => $value ) {
$stmt->execute(array($value, $comments[$questi],$questi));
$count = $stmt->rowCount();
echo $count > 0 ? $questi.' updated' : $questi.' did not update';
}
}
if(isset($_POST['answer'], $_POST['comments'])){
$answers = $_POST['answer'];
$comments = $_POST['comments'];
ModifySurveyMulti($answers, $comments);
}