我想使用PHP的PDO将数据插入到mysql数据库中。但是没有插入数据

时间:2012-12-11 02:44:11

标签: php mysql pdo

我想使用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();
}
?>

数据库表如下所示: enter image description here

2 个答案:

答案 0 :(得分: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();
}

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

答案 1 :(得分:0)

试试这种方式,可能是:

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

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

  1. 占用较少的代码。
  2. 比上一个更容易。
  3. 那么wedding_date呢?

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