无法使用PHP将数据插入MYSQL表

时间:2014-08-02 21:49:40

标签: php mysql pdo phpmyadmin

我正在尝试将数据插入MySQL表中,但我不确定它为什么不起作用

<?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";
        }
    ?>

如果我做错了,请告诉我。

4 个答案:

答案 0 :(得分:2)

您使用准备好的陈述是错误的。使用$ db-&gt; prepare(),您实际上是使用占位符提交查询,而不是使用值(称为发送模板)。在你的情况下:

$query = 'INSERT INTO `User` (`username`,`Password`,`First Name`,`Surname`, `Gender`, `DOB`, `Email Address`) 
                VALUES (:userName, :password, :firstname, :Surname, :gender, :dob, :email)';

答案 1 :(得分:0)

首先确保您已正确连接到sql然后将代码更改为此代码:

$table='User';
$sql="INSERT INTO
$table(username,Password,First Name,Surname,Gender,DOB,Email Address) VALUES ('$userName','$password','$firstname','$Surname', '$gender', '$dob', '$email')";

$result=$conn->query($sql) ;

祝你好运!

答案 2 :(得分:0)

使用此连接:

$db = "mysql:host=yourhost;dbname=yourdbname";
$username = "  ";
$password = "  ";
$conn = new PDO( $db, $username, $password ) or die("Error Connection !! ");
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");

答案 3 :(得分:0)

尝试这种方法:

<?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'];

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$query = 'INSERT INTO `User` (`username`,`Password`,`First Name`,`Surname`, `Gender`, `DOB`, `Email Address`) 
          VALUES (:userName, :password, :firstname, :Surname, :gender, :dob, :email)'
try {
    $stmt  = $conn->prepare($query);
    $result = $stmt->execute(array(
                        ":userName" => $userName,
                        ":password" => $password,
                        ":firstname" =>$firstname,
                        ":Surname" => $Surname,
                        ":gender" => $gender,
                        ":dob" => $dob,
                        ":email" => $email));
    if ($result) {
       // success!
       echo 'lastInsertId = '.$conn->lastInsertId(); 

    } else {
       // Query failed.
       echo 'errorcode = '.$stmt  ->errorCode();
    }
} catch(PDOException $err) {
        echo "Houston we have a problem: $err <br />";
        echo 'errorcode = '.$stmt  ->errorCode();
}