0
我對PHP很新穎,遇到問題。我試圖檢查表中是否存在id,如果不存在,則插入記錄但遇到問題。我目前有:注意:未定義的變量:插入
<?php
if(isset($_POST['add'])){
$id = $_POST['id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$dob = $_POST['dob'];
$telephone = $_POST['telephone'];
$job_title = $_POST['job_title'];
$site = $_POST['site'];
$department = $_POST['department'];
$email = $_POST['email'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$cek = mysqli_query($db, "SELECT * FROM employees WHERE id='$id'");
if(mysqli_num_rows($cek) == 0){
if($pass1 == $pass2){
$pass = md5($pass1);
$insert = mysqli_query($db, "INSERT INTO employees (id, first_name, last_name, dob, telephone, job_title, site, department, email, password)
VALUES('$id','$first_name', '$last_name', '$dob', '$telephone', '$job_title', '$site', '$department', '$email', '$pass')") or die('Error: ' . mysqli_error($db));
if($insert){
echo '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>Employee added</div>';
}else{
echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>Ups, Error, user not added</div>';
}
} else{
echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>Passwords do not match</div>';
}
}else{
echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>Employee Id Exists</div>';
}
}
?>
我遇到一個錯誤:注意:未定義的變量:DB
我試圖谷歌,但無濟於事爲止。有人有主意嗎?
錯誤點到線76是
$cek = mysqli_query($db, "SELECT * FROM employees WHERE id='$id'");
你可以做'WHERE id ='{$ id}''但它是一個非常糟糕的方式,因爲它會讓你打開SQL注入。因爲錯誤提示你設置了$ db變量,所以你需要輸入 – Option
? Mysqli_query試圖打開數據庫,但由於$ db似乎沒有在您的腳本中定義,因此無法打開連接。 – dsadnick
我有 <?php include(「config.php」);?> $ db變量在那裏設置 –