duanjian7343 2014-09-10 12:26
浏览 34
已采纳

将包含数组的表单数据安全插入到mysql数据库中

I found many answers about how to insert form data into mysql using PDO, also I found some answers related to insert data from an array like this one but actually my problem is related to the third question in my form for which I am not sure if I wrote the query in a correct way

This is my code but it give me this error:

PHP Parse error: syntax error, unexpected ';' in line ... (the line related to insert statement)

<?php
session_start();

if(isset($_POST['submit']))
{
 $_SESSION['q1'] = $_POST['q1'];
 $_SESSION['q2'] = $_POST['q2'];
 $_SESSION['q3'] = implode(',', $_POST['genre']);

 $q1 = mysql_real_escape_string($_SESSION['q1']);
 $q2 = mysql_real_escape_string($_SESSION['q2']);
 $q3 = mysql_real_escape_string($_SESSION['q3']);

 $conn = new PDO('mysql:dbname=Application;host=localhost;charset=utf8', 'user', 'xxxx');
 $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt = $conn->prepare('INSERT INTO test (q1, q2, q3) VALUES (:q1, :q2, :q3)');
 $stmt->execute(array(':q1' => $q1,':q2' => $q2,':q3' => ".$q3."));
 }

 catch(Exception $e) {
     echo 'Exception -> ';
     var_dump($e->getMessage());
  }

 header('Location: Thankyou.php');
   exit;
}
?>
  • 写回答

1条回答 默认 最新

  • douliu1092 2014-09-10 12:29
    关注

    First of all don't mix mysql with PDO, Also use try with catch exception and

    change

    $stmt->execute(array(':q1' => $q1,':q2' => $q2,':q3' => ".$q3."));
    

    to

    $stmt->execute(array(':q1' => $q1,':q2' => $q2,':q3' => $q3));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办