1

我想使用 PHP 的 PDO 将数据插入 mysql 数据库。但是没有插入数据。我之前使用过 PDO,但没有遇到任何问题。但是在以下情况下,我不明白我在哪里做错了。谁能帮帮我吗?输出显示良好的回声。

<?php

include 'includes/config.php';

$name1 = $_POST['name1'];
$address = $_POST['address'];
$city = $_POST['city']; 
$state = $_POST['state'];
$zip_code = $_POST['zip_code'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$fiance = $_POST['fiance'];
$wedding_date = $_POST['wedding_date'];
$number_of_guest = $_POST['number_of_guest'];

$radio = $_POST['radio']; 
if($radio=='on') $radio = 'yes'; 

$newspaper = $_POST['newspaper']; 
if($newspaper=='on') $newspaper = 'yes';

$facebook = $_POST['facebook']; 
if($facebook=='on') $facebook = 'yes';

$website = $_POST['website']; 
if($website=='on') $website = 'yes';

$hear_by_other = $_POST['hear_by_other']; 
if($hear_by_other=='on') $hear_by_other = 'yes';

$by_other = $_POST['by_other'];


$date1 = date("m-d-Y");
$status = 0;



echo $name1.'<br />';
echo $address.'<br />';
echo $city.'<br />';
echo $state.'<br />';
echo $zip_code.'<br />';
echo $telephone.'<br />';
echo $email.'<br />';
echo $fiance.'<br />';
echo $wedding_date.'<br />';
echo $number_of_guest.'<br />';
echo $radio.'<br />';
echo $newspaper.'<br />';
echo $facebook.'<br />';
echo $website.'<br />';
echo $hear_by_other.'<br />';
echo $by_other.'<br />';
echo $date1.'<br />';
echo $status.'<br />';


$statement = $pdo->prepare('INSERT INTO arefin (name1,address,city,state,zip_code,telephone,email,fiance,wedding_date,number_of_guest,radio,newspaper,facebook,website,hear_by_other,by_other,date1,status) VALUES (:var1,:var2,:var3,:var4,:var5,:var6,:var7,:var8,:var9,:var10,:var11,:var12,:var13,:var14,:var15,:var16,:var17,:var18)');

$statement->bindParam(':var1',$name1);
$statement->bindParam(':var2',$address);
$statement->bindParam(':var3',$city);
$statement->bindParam(':var4',$state);
$statement->bindParam(':var5',$zip_code);
$statement->bindParam(':var6',$telephone);
$statement->bindParam(':var7',$email);
$statement->bindParam(':var8',$fiance);
$statement->bindParam(':var9',$wedding_date);
$statement->bindParam(':var10',$number_of_guest);
$statement->bindParam(':var11',$radio);
$statement->bindParam(':var12',$newspaper);
$statement->bindParam(':var13',$facebook);
$statement->bindParam(':var14',$website);
$statement->bindParam(':var15',$hear_by_other);
$statement->bindParam(':var16',$by_other);
$statement->bindParam(':var17',$date1);
$statement->bindParam(':var18',$status);
$statement->execute();

?>

我的 config.php 文件在这里:

<?php

$dbhost = 'localhost';
$dbname = 'arefinDB';
$dbuser = 'root';
$dbpass = '';
try {
    $pdo = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
    $conn = $pdo;
}
catch( PDOException $excepiton ) {
    echo "Connection error :" . $excepiton->getMessage();
}
?>

数据库表如下所示: 在此处输入图像描述

4

2 回答 2

2

您应该添加适当的错误处理,以便您确切知道失败的原因和原因。

首先你需要告诉 PDO 抛出异常:

$pdo = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
// add this:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

然后你可以将你的数据库操作包装在一个try-catch块中:

try
{
  $statement = $pdo->prepare('INSERT INTO arefin (name1,address,city,state,zip_code,telephone,email,fiance,wedding_date,number_of_guest,radio,newspaper,facebook,website,hear_by_other,by_other,date1,status) VALUES (:var1,:var2,:var3,:var4,:var5,:var6,:var7,:var8,:var9,:var10,:var11,:var12,:var13,:var14,:var15,:var16,:var17,:var18)');

  $statement->bindParam(':var1',$name1);
  // etc.

  $statement->execute();
}
catch ( PDOException $exception )
{
    echo "PDO error :" . $exception->getMessage();
}

评论太长了,但它应该有助于解决问题......

于 2012-12-11T02:59:03.153 回答
0

试试这个方法,可能是:

$statement->execute(array(':var1'=>$name1,
                  ':var2'=>$address));

我要求您这样做的原因是:

  1. 它占用的代码量更少。
  2. 它比上一个更容易。

那么wedding_date呢?

您是否以正确的格式插入?

于 2012-12-11T02:49:54.350 回答