2017-02-22 81 views
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">&times;</button>Employee added</div>'; 
        }else{ 
         echo '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</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">&times;</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">&times;</button>Employee Id Exists</div>'; 
      } 
     } 
     ?> 

我遇到一個錯誤:注意:未定義的變量:DB

我試圖谷歌,但無濟於事爲止。有人有主意嗎?

錯誤點到線76是

 $cek = mysqli_query($db, "SELECT * FROM employees WHERE id='$id'"); 
+0

你可以做'WHERE id ='{$ id}''但它是一個非常糟糕的方式,因爲它會讓你打開SQL注入。因爲錯誤提示你設置了$ db變量,所以你需要輸入 – Option

+0

? Mysqli_query試圖打開數據庫,但由於$ db似乎沒有在您的腳本中定義,因此無法打開連接。 – dsadnick

+0

我有 <?php include(「config.php」);?> $ db變量在那裏設置 –

回答

0

我剛剛從<代碼

最開始的失蹤PHP的包括:( 「config.php中」);?的>

代替

其中之一其右,但有隻是看不到它失蹤。必須有意外刪除或刪除了某些東西而未被發現。

0

這裏是a link幫助MySQL連接

$db=mysqli_connect("localhost","my_user","my_password","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

那麼你可以使用

$cek = mysqli_query($db, "SELECT * FROM employees WHERE id='$id'"); 

希望幫助一個一點點