您好,我有一个表格,教师可以作为用户创建新学校,而该教师的学校会获得ID。因此,我检查数据库以确保ID在“学校”表中尚不存在。如果可以,我会获得该学校ID,以便老师可以使用它创建个人资料。如果尚不存在,则会创建一个新表,并将其输入到该教师所在学校的“学校”表和“用户”表中。
它工作正常,只是每次填写表单时都要两次输入数据,但只能在“ USERS”表中输入。在“学校”表中,应按原样输入一次。
有人可以告诉我为什么会这样吗?我已经找了几个星期了。
代码:
if(isset($_POST['submit'])){
$name=$_POST['name'];
$name = mysqli_real_escape_string($con,$name);
$lastname=$_POST['lastname'];
$lastname = mysqli_real_escape_string($con,$lastname);
$email=$_POST['email'];
$email = mysqli_real_escape_string($con,$email);
$phone=$_POST['phone'];
$phone = mysqli_real_escape_string($con,$phone);
$school=$_POST['school'];
$school = mysqli_real_escape_string($con,$school);
$address=$_POST['address'];
$address = mysqli_real_escape_string($con,$address);
$region=$_POST['region'];
$region = mysqli_real_escape_string($con,$region);
$state=$_POST['state'];
$state = mysqli_real_escape_string($con,$state);
$zip = $_POST['zip'];
$zip = mysqli_real_escape_string($con,$zip);
$password= $_POST['password'];
$hash = password_hash($password, PASSWORD_BCRYPT);
//GET STATE NAME
$getState = "SELECT state from `states` WHERE id= '$state'";
$stateRes = mysqli_query($con, $getState);
$stateRow = mysqli_fetch_array($stateRes);
$stateName = $stateRow['state'];
//CHECK SCHOOL
$checkSchool = "SELECT school from `schools` WHERE school= '$school'";
$schoolRes = mysqli_query($con, $checkSchool);
$schoolCount = mysqli_num_rows($schoolRes);
if($schoolCount >0){
//if school exist get it's id
$schoolIdSql = "SELECT id from `schools` WHERE school= '$school'";
$schoolIdRes = mysqli_query($con, $schoolIdSql);
$schoolRow = mysqli_fetch_array($schoolIdRes);
$schoolId = $schoolRow['id'];
}else{
//if doesn't exist insert new school
$schoolquery = "INSERT INTO schools (state_id, school) VALUES ('$state','$school')";
$schoolresult = mysqli_query($con, $schoolquery);
//get new school id
$schoolIdSql = "SELECT id from `schools` WHERE school= '$school'";
$schoolIdRes = mysqli_query($con, $schoolIdSql);
$schoolRow = mysqli_fetch_array($schoolIdRes);
$schoolId = $schoolRow['id'];
}
//CHECK USER
$checkUser = "SELECT email from `Users` WHERE email= '$email'";
$userRes = mysqli_query($con, $checkUser);
$userCount = mysqli_num_rows($userRes);
if($userCount >0){
$submitted = "Email is not available";
$invalid = '<input id="email" type="text" name="email" class="form-control is-invalid" required="required" data-error="email is required." data-remote="/validate">';
} else{
$userId = rand(1,9999999);
$check_userId ="select count(*) count from Users where user_id = " . $userId;
while ($row['count'] > 0);
$query = "INSERT INTO Users (id, user_type, name, lastname, email, phone, school, address, state, zip, password, status) VALUES ('$userId','teacher','$name', '$lastname', '$email', '$phone', '$schoolId', '$address', '$stateName', '$zip', '$hash', 'active')";
$result = mysqli_query($con, $query);
if(!$result = $con->query($query)){
die('there was an error running query [' . $con->error . ']');
}else {
header("location: thankyou");
}
}
}
答案 0 :(得分:0)
似乎早期编程的一些旧手法仍然存在:
也许这是新查询:
$result = mysqli_query($con, $query);
也许这是旧的?
if(!$result = $con->query($query))
应该这样做,并且是重复插入的原因。