gpt4 book ai didi

php - 如果成功插入 PDO 则显示消息

转载 作者:行者123 更新时间:2023-11-29 01:57:32 26 4
gpt4 key购买 nike

我写了一些 PHP 代码来将记录插入到 MySQL 数据库中。我想显示一条消息,指示记录是否已成功添加到数据库中。我尝试了几次,但都失败了。

代码:

<?php
try {
$db_user = 'root';
$db_pass = 'cea123';
$db = new PDO( 'mysql:host=localhost;dbname=symposium', $db_user, $db_pass );
$form = $_POST;
$sql = "INSERT INTO app (
firstname, lastname, company, homepage, contactno, addressline1, addressline2, city,
postalcode, country, email, abstractdetails )
VALUES (
:firstname, :lastname, :company, :homepage, :contactno, :addressline1, :addressline2,
:city, :postalcode, :country, :email, :abstractdetails )";
$query = $db->prepare( $sql );
$query->execute( array( ':firstname'=>$firstname, ':lastname'=>$lastname, ':company'=>$company, ':homepage'=>$homepage, ':contactno'=>$contactno, ':addressline1'=>$addressline1, ':addressline2'=>$addressline2, ':city'=>$city, ':postalcode'=>$postalcode, ':country'=>$country, ':email'=>$email, ':abstractdetails'=>$abstractdetails ));
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

最佳答案

您可以简单地使用 rowCount 检查功能,

if($query->rowCount() > 0){
echo "Record Inserted Successfully !!";
}

关于php - 如果成功插入 PDO 则显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24449612/

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