问题描述
您好,我试图让一个code到更新或修改问卷答案,每个答案的意见,但是当我执行该功能submiting形式,它没有任何值保存到数据库中。我能做些什么来解决这个问题?
我在PDO是新。
在此先感谢。
数据库结构
问题(idquestion,问题)调查(idsurvey,idquestion,答案,comments_per_question,survey_number)
更新功能
公共职能ModifySurveyMulti($答案=阵列())
{
如果(!空($答案)){
的foreach($答案$问题1 => $值){
$这个 - > MyDB->写(更新调查SET(
`idquestion` ='。$问题1。,
`answers` ='。$值[0]。',
`comments_per_answer` =[注释] [$问题1]。$ _ POST。'));
}
}
}
modify_surveyform.php 的
<第i< PHP的echo $行[问题];?>< /第i
&所述; TD>
<输入类型=文本
名称=回答并[d?PHP的echo $行['idquestion'];?>] []
值=< PHP的echo $行[答案];?>>
< /输入>
< / TD>
&所述; TD>
< textarea的类型=文本
NAME =评论[<?PHP的echo $行['idquestion'];?>]
COLS =50行=3/> <?PHP的echo $行[意见];?
< / textarea的>
< / TD>
< / TR><?PHP}>
Mydbconnect.php
< PHP
//我加入我的PDO数据库,因为你是去precated
一流的数据库连接
{
公共$ CON;
//创建一个默认的数据库元素
公共职能__construct($主机='',$ DB ='',$ USER ='',$传球='')
{
尝试{
$这个 - > CON =新PDO(MySQL的:主机= $主机;
DBNAME = $分贝,$用户,
$传球,阵列(
PDO :: ATTR_ERRMODE
= GT; PDO :: ERRMODE_WARNING
)
);
}
赶上(例外$ E){
返回0;
}
} //简单的提取和返回的方法
公共职能取($ _ SQL)
{
$查询= $这个 - > CON-> prepare($ _ SQL);
$查询 - >执行();
如果($查询 - > rowCount等()大于0){
而($阵列= $查询 - >取(PDO :: FETCH_ASSOC)){
$行[] = $阵列;
}
}
返回(使用isset($行)及和放大器; $行== 0安培;!&安培;!空($行))? $行:0;
} //简单写分贝方法
公共职能写入($ _ SQL)
{
$查询= $这个 - > CON-> prepare($ _ SQL);
$查询 - >执行();
}
?}>
您需要做的几件事情:
- 首先沟这个code,它是无用的,暴露你的SQL
注射 - 使用PDO直接与prepared声明
- 您所需要的查询是:
更新调查SET(`answers` =?`comments_per_answer` =?),其中idquestion =?
您需要调整您的类只创建连接
类DBCONNECT
{
公共$ CON; 公共职能__construct($主机='',$ DB ='',$ USER ='',$传球='')
{
尝试{
$这个 - > CON =新PDO(
MySQL的:主机= $主机,数据库名= $ DB,
$用户,$传球,
阵列(PDO :: ATTR_ERRMODE => PDO :: ERRMODE_WARNING)
);
}
赶上(例外$ E){
死亡($ E);
}
} 公共职能get_connection(){
返回$这个 - > CON;
}}
所以,你可以像这样创建它:
$ DB =新的数据库连接(/ *通参数在这里* /);
$这个 - > MYDB = $ DB-GT&; get_connection();
修改,并在你的函数中使用它:
公共职能ModifySurveyMulti($答案=阵列(),$评论)
{
$的SQL =更新调查SET(`answers` =?`comments_per_answer` =?)
?WHERE idquestion =';
$ stmt-> prepare($的SQL);
的foreach($答案$问题1 => $值){
$ stmt->执行(阵列(价值$,$评论[$问题1],$问题1));
$数= $ stmt-> rowCount时();
回声$ COUNT> 0? $问题1。更新:$问题1。没有更新';
}
}
调用函数:
如果(使用isset($ _ POST ['答案'],$ _ POST ['意见'])){
$答案= $ _ POST ['答案'];
$评论= $ _ POST ['意见'];
ModifySurveyMulti($答案,$评论);
}
Hello I'm trying to make a code to UPDATE or EDIT the survey answers and comments per answer but when I execute the function submiting the form, it did not save any value into the database. What can I do to fix it?
I'm new in PDO.
Thanks in advance.
Database Structure
"Questions" (idquestion, question)
"Surveys" (idsurvey, idquestion, answers, comments_per_question, survey_number)
Update function
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
<th><?php echo $row["questions"];?></th>
<td>
<input type = "text"
name = "answer[<?php echo $row['idquestion'];?>][]"
value = "<?php echo $row["answers"];?>">
</input>
</td>
<td>
<Textarea type = "text"
name = "comment[<?php echo $row['idquestion'];?>]"
cols = "50" rows = "3"/> <?php echo $row["comment"];?
</textarea>
</td>
</tr><?php } ?>
Mydbconnect.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();
}
}?>
Few things you need to do:
- First of all ditch this code, it is useless and expose you to sqlinjection
- Use PDO directly with prepared statement
- The query you need is :
UPDATE survey SET(`answers`= ?,`comments_per_answer`= ?) WHERE idquestion = ?
You will need to adjust your class to only create the connection
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;
}
}
So that you can create it like this:
$db = new DBConnect(/*pass arguments here*/);
$this->MyDB = $db->get_connection();
Modify and use it in your function:
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';
}
}
Call the function :
if(isset($_POST['answer'], $_POST['comments'])){
$answers = $_POST['answer'];
$comments = $_POST['comments'];
ModifySurveyMulti($answers, $comments);
}
这篇关于使用PHP的MySQL的PDO UPDATE阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!