我试图在我的sql数据库中插入两个语句。但是当我点击发送时,我看到只发布了第一个语句。
有人知道我的代码有什么问题以及如何解决它?
这是我的代码:
$query1 = "INSERT INTO table1(firstname, lastname) VALUES (?, ?)";
$stmt1 = $db->prepare($query1);
$stmt1->execute(array($firstname, $lastname));
$query2 = "INSERT INTO table2(firstname, lastname) VALUES (?, ?)";
$stmt2 = $db->prepare($query2);
$stmt2->execute(array($firstname, $lastname));
答案 0 :(得分:0)
您不能总是假设您的查询始终有效。查询失败的原因有很多。无论何时执行查询,都必须检查一切是否正常。还可以打开错误报告以及检查错误日志。这个问题也应该在那里报告
以下修改将有助于过滤查询失败的问题
<?php
error_reporting(E_ALL);
$query2 = "INSERT INTO table2(`firstname`, `lastname`) VALUES (?, ?)";
$stmt2 = $db->prepare($query2);
if($stmt2->execute(array($firstname, $lastname))){//check if it saved
echo 'Saved';
} else {
echo 'Failed Because '.$stmt2->error;//Oops something happened
}
答案 1 :(得分:0)
试试这个==&gt;
$query1 = "INSERT INTO table1(firstname, lastname) VALUES (?, ?)";
$stmt1 = $db->prepare($query1);
$stmt1->bind_param("ss",$firstname, $lastname);
$stmt1->execute();
$query2 = "INSERT INTO table2(firstname, lastname) VALUES (?, ?)";
$stmt2 = $db->prepare($query2);
$stmt2->bind_param("ss",$firstname, $lastname);
$stmt2->execute();
在此处查看有关PHP Prepared Statements
的更多信息答案 2 :(得分:0)
当您prepare
sql语句时,您可以在尝试执行sql之前检查一切正常,然后通过为execute
方法的结果分配变量,检查是否存在成功查询并继续下一个语句。
Two very basic tables - identical except in name.
mysql> describe table1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| firstname | varchar(50) | YES | | NULL | |
| lastname | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
mysql> describe table2;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| firstname | varchar(50) | YES | | NULL | |
| lastname | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
mysql> select * from table1;select * from table2;
+------------------------+-----------------------+
| firstname | lastname |
+------------------------+-----------------------+
| firstname59365bf480611 | lastname59365bf480613 |
+------------------------+-----------------------+
1 row in set (0.00 sec)
+------------------------+-----------------------+
| firstname | lastname |
+------------------------+-----------------------+
| firstname59365bf480611 | lastname59365bf480613 |
+------------------------+-----------------------+
1 row in set (0.00 sec)
$firstname=uniqid('firstname');
$lastname=uniqid('lastname');
$dbhost = 'localhost';
$dbuser = 'root';
$dbpwd = '';
$dbname = '';
$db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$result = false;
$sql='insert into `table1` (`firstname`, `lastname`) values (?, ?)';
$stmt = $db->prepare( $sql );
if( $stmt ){
$stmt->bind_param('ss',$firstname, $lastname );
$result = $stmt->execute();
$stmt->free_result();
$stmt->close();
} else {
exit( 'Error: Statement #1' );
}
if( $result ){
$sql='insert into `table2` (`firstname`, `lastname`) values (?, ?)';
$stmt = $db->prepare( $sql );
if( $stmt ){
$stmt->bind_param('ss',$firstname, $lastname );
$result = $stmt->execute();
$stmt->free_result();
$stmt->close();
} else {
exit( 'Error: Statement #2' );
}
}
$db->close();