I'm currently trying to get a certain user based on student number and password...problem is mysqli_query keeps returning false(?) statement. Though I checked the query via testing it in PHPMyAdmin. Here's my query:
$number = $_POST['number'];
$password = $_POST['password'];
$sql = "SELECT studentNumber FROM student where studentNumber = '$number'" ;
$result = mysqli_query($conn, $sql) or die(mysqli_error());;
if(!$result){
echo "FAIL";
}
else if($result > 0){
header("register.html");
}else{
$sql = "INSERT INTO 'sampleDB'.'student'('userID', 'studentNumber', 'password') VALUES ('', $number, $password)";
header("login.html");
}
Here's my connection part of the code:
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
You should check how many rows the query result did yield thru num_rows
, then after that make some insertions if its zero.
Take note that column quotes are actually backticks rather than single quotes:
$number = $_POST['number'];
$password = $_POST['password'];
// checking
$sql = 'SELECT studentNumber FROM student WHERE studentNumber = ?';
$select = $conn->prepare($sql);
$select->bind_param('s', $number);
$select->execute();
// if there is no student number, register
if($select->num_rows > 0) {
// there is student number found
header('Location: index.html'); exit;
} else {
// there is NO student number found
$sql = 'INSERT INTO sampleDB.student(userID, studentNumber, password) VALUES (NULL, ?, ?)';
$insert = $conn->prepare($sql);
$insert->bind_param('ss', $number, $password);
$insert->execute();
header('Location: login.html');
}
Sidenote: You connection codes seems to be truncated on the question. Don't forget to add your database name into the connection:
$conn = new mysqli($servername, $username, $password, $database_name);
$sql = "SELECT studentNumber FROM student where studentNumber = '$number'" ;
改成
$sql = "SELECT studentNumber FROM student where studentNumber = '".$number."'" ;
首先获取结果并在您的条件下使用它。
$result2 = mysql_fetch_array($result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.