gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:54:06 24 4
gpt4 key购买 nike

我想使用 PHP 的 PDO 将数据插入到 mysql 数据库中。但是没有插入数据。我以前使用过 PDO,但没有遇到任何问题。但在以下情况下,我不明白我哪里做错了。谁能帮帮我吗?带有 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 文件在这里:

<?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

最佳答案

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

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

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

然后您可以将数据库操作包装在 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();
}

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

关于php - 我想使用 PHP 的 PDO 将数据插入到 mysql 数据库中。但是没有插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com