非常新的PHP。如果这个问题很荒谬,请原谅我。无论如何,我有一些PHP将数据发送到数据库。以下是其中一些内容:
else {
$sql = "INSERT INTO users (username, firstname, lastname, password)
VALUES ('$username', '$firstname', '$lastname', '$pass')";
$sql = "CREATE TABLE $username (
day VARCHAR(10) NOT NULL,
P1 VARCHAR(10) NOT NULL,
P1_WORK VARCHAR(30) NOT NULL,
P2 VARCHAR(10) NOT NULL,
P2_WORK VARCHAR(30) NOT NULL,
P3 VARCHAR(10) NOT NULL,
P3_WORK VARCHAR(30) NOT NULL,
P4 VARCHAR(10) NOT NULL,
P4_WORK VARCHAR(30) NOT NULL,
P5 VARCHAR(10) NOT NULL,
P5_WORK VARCHAR(30) NOT NULL,
P6 VARCHAR(10) NOT NULL,
P6_WORK VARCHAR(30) NOT NULL
)";
if (mysqli_query($con, $sql)) {
echo "okay";
} else {
echo "error";
}
}
当我运行它时,我意识到只会执行第二个$sql=
,因为我在同一个实例中定义了$sql
两次。我怎样才能这样做,所以这两个事件都是一次性触发的?谢谢!
编辑:完整代码
<?php
echo error_reporting(E_ALL);
$username = strval($_GET['enteredUserName']);
$pass = strval($_GET['enteredPassword']);
$firstname = strval($_GET['enteredFirstName']);
$lastname = strval($_GET['enteredLastName']);
$con = mysqli_connect('localhost','XXXX','XXXX','XXXX');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
} else {
$sql="SELECT * FROM users WHERE username = '".$username."'";
$result = mysqli_query($con,$sql);
if ($result->num_rows > 0) {
echo "pop";
} else {
$sql = "INSERT INTO users (username, firstname, lastname, password)
VALUES ('$username', '$firstname', '$lastname', '$pass') ";
$sql = "CREATE TABLE $username (
day VARCHAR(10) NOT NULL,
P1 VARCHAR(10) NOT NULL,
P1_WORK VARCHAR(30) NOT NULL,
P2 VARCHAR(10) NOT NULL,
P2_WORK VARCHAR(30) NOT NULL,
P3 VARCHAR(10) NOT NULL,
P3_WORK VARCHAR(30) NOT NULL,
P4 VARCHAR(10) NOT NULL,
P4_WORK VARCHAR(30) NOT NULL,
P5 VARCHAR(10) NOT NULL,
P5_WORK VARCHAR(30) NOT NULL,
P6 VARCHAR(10) NOT NULL,
P6_WORK VARCHAR(30) NOT NULL
)";
if (mysqli_query($con, $sql)) {
echo "okay";
} else {
echo "error";
}
}
mysqli_close($con);
}
?>
答案 0 :(得分:1)
就像u_mulder在评论中所说,你可以这样做,就像mysqli_error returns empty string if there is no error occured一样。
$sql1 = 'first query';
$sql2 = 'second query';
mysqli_query($h, $sql1);
mysqli_query($h, $sql2);
$errorOccured = false;
if (strlen(mysqli_error($h)) > 1) {
$errorOccured = true;
}
或者,正如已经指出的那样,只需执行两个mysqli_queries并检查它们的返回值。 或者,如果您需要整体执行查询,您还可以使用名为&#34; transactions&#34;的内容,查看其中一个注释示例。取自mysqli_rollback in the manual:
$all_query_ok=true; // our control variable
//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE
//now let's test our control variable
// This is shorthand if else statement
// ($statement) ? If true : if false;
$all_query_ok ? $mysqli->commit() : $mysqli->rollback();
$mysqli->close();
答案 1 :(得分:0)
为您的两个查询使用$sql1
和$sql2
两个不同的变量,然后
要么(一次两个): -
if(mysqli_query($con, $sql1) && mysqli_query($con, $sql2)){
echo "Ok";
}else{
echo "Error";
}
或逐一: -
if (mysqli_query($con, $sql)) {
if(mysqli_query($con, $sql2)){
echo "OK";
}else{
echo "error";
}
} else {
echo "error";
}
或者您需要检查mysqli_multi_query()
: -
答案 2 :(得分:0)
为不同的查询使用不同的变量
$sql1 = "INSERT INTO users (username, firstname, lastname, password)
VALUES ('$username', '$firstname', '$lastname', '$pass')";
$sql2 = "CREATE TABLE $username (
day VARCHAR(10) NOT NULL,
P1 VARCHAR(10) NOT NULL,
P1_WORK VARCHAR(30) NOT NULL,
P2 VARCHAR(10) NOT NULL,
P2_WORK VARCHAR(30) NOT NULL,
P3 VARCHAR(10) NOT NULL,
P3_WORK VARCHAR(30) NOT NULL,
P4 VARCHAR(10) NOT NULL,
P4_WORK VARCHAR(30) NOT NULL,
P5 VARCHAR(10) NOT NULL,
P5_WORK VARCHAR(30) NOT NULL,
P6 VARCHAR(10) NOT NULL,
P6_WORK VARCHAR(30) NOT NULL
)";
if (mysqli_query($con, $sql1) && mysqli_query($con, $sql2)) {
echo "okay";
} else {
echo "error";
}