douling0053 2014-08-02 21:49
浏览 84
已采纳

无法使用PHP将数据插入MYSQL表[关闭]

I am trying to insert data into MySQL tables but I am not sure why it is not working

<?php
        include 'connect.php';
        $userName = $_POST['name']; 
        $firstname = $_POST['FirstName']; 
        $Surname = $_Post['Surname'];
        $email = $_POST['EmailAddress']; 
        $password = $_POST['Password']; 
        $gender = $_POST['Gender']; 
        $dob = $_POST['DOB'];
        $query = 'INSERT INTO `User` (`username`,`Password`,`First Name`,`Surname`, `Gender`, `DOB`, `Email Address`) 
                            VALUES ('.$userName.','.$password.','.$firstname.','.$Surname.', '.$gender.', '.$dob.', '.$email.')';
        $stmt = $conn->prepare($query);
        try {
            $myarray = array(
                ":userName" => $userName,
                ":password" => $password,
                ":firstname" =>$firstname,
                ":Surname" => $Surname,
                ":gender" => $gender,
                ":dob" => $dob,
                ":email" => $email);
            print_r($myarray);
            $stmt->execute($myarray);
        } catch(PDOException $err) {
            echo "Houston we have a problem: $err";
        }
    ?>

Please tell me if I am doing something wrong.

  • 写回答

4条回答 默认 最新

  • douyin8809 2014-08-02 21:54
    关注

    You're using Prepared Statements wrong. With $db->prepare() you're actually submitting the query with placeholders and not with the values (called sending a template). In your case:

    $query = 'INSERT INTO `User` (`username`,`Password`,`First Name`,`Surname`, `Gender`, `DOB`, `Email Address`) 
                    VALUES (:userName, :password, :firstname, :Surname, :gender, :dob, :email)';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端