2017-02-09 67 views
1

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

'mysqli_query($ con,$ sql1); mysqli_query($ con,$ sql2)' –

+0

U對兩個不同的查詢使用相同的變量。使用不同的變量 –

+1

我不知道我完全理解這將被插入。如果(mysqli_query($ con,$ sql)&& mysqli_query($ con,$ sql2))''我會將我的完整代碼添加到問題@u_mulder – Caspar

回答

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(): -

http://php.net/manual/en/mysqli.multi-query.php

+2

? –

+0

@u_mulder是的。我正在編輯那一個。但是,謝謝你指出 –

+1

謝謝,讓它工作。 – Caspar

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"; 
} 
+1

謝謝你kalai – Caspar

+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_error in the manual。或者,正如已經指出的那樣,只需執行兩個mysqli_queries並檢查它們的返回值即可。 或者,如果您需要執行整個查詢,也可以使用稱爲「事務」的事物,查看其中一個註釋示例。取自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();