我尝试使用mysqli_query()
进行插入,但它似乎无法正常工作,我无法弄明白。 mysqli_error()
也没有帮助我(它返回NULL)。这是我的代码:
<?php
include('db_fns.php');
function register() {
// register new person with db
// return true or error message
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['email'];
$password = $_POST['password'];
//$education = $_POST['education'];
//$faculty = $_POST['faculty'];
//$department = $_POST['department'];
// connect to db
include('db_fns.php');
// check if email is unique
$result = mysqli_query($db, "select * from users where email='".$email."'");
if (!$result) {
throw new Exception('Could not execute query');
}
if (mysqli_num_rows($result)>0) {
throw new Exception('Το email χρησιμοποιείται. Παρακαλώ διαλέξτε κάποιο άλλο.');
}
// if ok, put in db
$result = mysqli_query($db,"insert into users values
('".$fname."', '".$lname."', '".$email."', sha1('".$password."'))");
mysqli_query($db,"COMMIT");
if ( false===$result ) {
printf("error: %s\n", mysqli_error($db));
}
return true;
}
register();
?>
这是db_fns.php:
<?php
$db = mysqli_connect ("localhost","root","","sfhmmy10") or die ("Could not connet to MySQL");
mysqli_select_db ($db,"sfhmmy10") or die ("No database");
mysqli_set_charset($db, "utf8");
?>