我想使用PHP的PDO将数据插入mysql数据库.但是没有插入数据 [英] I want to insert data into mysql database using PDO of PHP. But the data is not inserted

查看:64
本文介绍了我想使用PHP的PDO将数据插入mysql数据库.但是没有插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to insert data into mysql database using PDO of PHP. But the data is not inserted. I used PDO before, but did not face any problem. But in the following case, I do not understand where i do wrong. Can anyone please help me? Output is showing well with echo.

<?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文件在这里:

My config.php File is here:

<?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();
}
?>

数据库表如下所示:

Database Table looks like:

推荐答案

您应该添加适当的错误处理,以便准确了解失败的原因和原因.

You should add proper error handling so that you know exactly what is failing and why.

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

First you need to tell PDO to throw exceptions:

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

然后,您可以将数据库操作包装在try-catch块中:

Then you can wrap your database operations in a try - catch block:

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();
}

评论太久了,但应该可以解决问题...

Too long for a comment, but it should help to solve the problem...

这篇关于我想使用PHP的PDO将数据插入mysql数据库.但是没有插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆