使用PDO插入行不起作用

时间:2014-01-21 08:36:51

标签: php mysql pdo

我在这里不知所措。我正在尝试使用PDO向表中插入一行,但它根本不起作用。我总是这样用其他表格来做它从来没有给出任何问题!也许这与桌子有关... Anwyway,我的剧本看起来像这样:

<?php

// Connect to DB.
$hostname = "localhost";
$database = "website";
$username = "leon";
$password = "B6T8WGfs";

try
{
    $connect = new PDO("mysql:host=$hostname; dbname=$database; charset=utf8", $username, $password) or die("Kan geen verbinding maken met database!");
}
catch (Exception $e)
{
    throw new Exception( 'Error connecting to database: ', 0, $e);
}

#### Prepare variables for PDO.
$user_id=28; // will change this later
$university=$_POST['university'];
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$order_items=$_POST['message']; // will change this later
$sent='false';

#### Add order to database.
try {
    $add_order=$connect->prepare("INSERT INTO orders(user_id, university, name, email, message, order_items, sent)VALUES(:user_id, :university, :name, :email, :message, :order_items, :sent)");
    $add_order->execute(array(':user_id'=>$user_id,
                                    ':university'=>$university,
                                    ':name'=>$name,
                                    ':email'=>$email,
                                    ':message'=>$message,                               
                                    ':order_items'=>$order_items,                               
                                    ':sent'=>$sent));
    echo "Success";
}
catch(PDOException $ex) {
    echo "An Error occured!"; //user friendly message
    echo ($ex->getMessage());
}

$connect = null;
?>

它似乎有效,因为页面上的输出显示“成功”。但实际上,该行未添加到表中。我的MYSQL'orders'表格如下所示:

PHPMyAdmin Table orders

注意:在Browse(Verkennen)选项卡中有一行,我手动输入以测试是否可以在表格中输入内容。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

我测试了你的代码,它就像一个魅力。我能想到的唯一可以解决的问题是,你的表单元素可能没有被命名。

即:<input type="text" name="university">

如果您的表单与我以下的表格不相似且未命名,那就是问题所在。

我已成功将您的表架构/信息复制到T

<form action="db_insert.php" method="post">
University: 
<input type="text" name="university">
<br>
Name: 
<input type="text" name="name">
<br>
Email: 
<input type="text" name="email">
<br>
Message: 
<input type="text" name="message">
<br>
Orders time: 
<input type="text" name="orders_items">
<br>
<input type="submit" name="submit" value="Submit">
</form>

<强>脚注:

为了区别于两个表单元素,我在测试中更改了以下内容:

$order_items=$_POST['message']; // will change this later

为:

$order_items=$_POST['orders_items'];

一起去:

<input type="text" name="orders_items">

答案 1 :(得分:0)

您的脚本始终为echo 'success',因为它位于try{}括号内。把它移到那里:

try {
    $add_order=$connect->prepare("INSERT INTO orders(user_id, university, name, email, message, order_items, sent)VALUES(:user_id, :university, :name, :email, :message, :order_items, :sent)");
    $add_order->execute(array(':user_id'=>$user_id,
                                    ':university'=>$university,
                                    ':name'=>$name,
                                    ':email'=>$email,
                                    ':message'=>$message,                               
                                    ':order_items'=>$order_items,                               
                                    ':sent'=>$sent));

}
catch(PDOException $ex) {
    echo "An Error occured!"; //user friendly message
    echo ($ex->getMessage());
    exit; // so that your script will stop if there's an error.
}
echo "Success"; // here

那里。如果catch做某事,成功将不会回应,否则,您的查询有效