2012-11-26 68 views
0

我試圖創建一個多字段搜索選項。在表單中有四個字段,用戶可以將信息輸入到任意一個或多個信息中,並且它將查詢數據庫。mySQLi準備失敗,多重?從表單

SQL在myPHP admin中可以工作,但是我從mySQL中得到了一堆醜陋的錯誤。

電流誤差

Prepare failed: (1054) Unknown column 'Employee.Fname' in 'field list' Fatal error: Call to a member function bind_param() on a non-object in /nfs/stak/students/g/greenjas/public_html/employee_info.php on line 41

這裏是表單代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 

<head> 
    <title>Bicycle Store Employees</title> 
    <link rel="stylesheet" type="text/css" href="web.css" /> 
</head> 
<body> 
    <h1>Bicycle Store Manager</h1> 
    <h2>Customized Business Management for Bicycle Stores</h2> 

    <h3>FORM: RETRIEVE EMPLOYEE INFORMATION</h3> 

    <form action="employee_info.php" method="post"> 
    <h3>Employee Information</h3> 
    <h4>First Name: <input type="text" name="Fname"/> 
    Last Name: <input type="text" name="Lname"/></h4> 

    <h3>Store Information</h3> 
    <h4> 
    Department #: <input type="text" name="Dno"/> 
    Store #: <input type="text" name="Store_num"/> 

    <input type="submit" name="submit" value="SEARCH"/> 
    </h4> 
    </form> 

    <p><a href="index.html">HOME</a></p> 

</body> 
</html> 

這裏是PHP

if($_POST["submit"]) { 
    if (!($stmt =$mysqli->prepare(" 
      SELECT Fname, Minit, Lname, Phone, Address, Sname, Saddress, Dno, Hourly 
      FROM 
      (
      SELECT LOCATION.*, 
      Employee.Fname, 
      Employee.Minit, 
      Employee.Lname, 
      Employee.Address, 
      Employee.Hourly, 
      Employee.Dno, 
      Employee.Phone 
      FROM EMPLOYEE, DEPARTMENT, LOCATION 
      WHERE EMPLOYEE.Dno=DEPARTMENT.Dnumber AND 
      LOCATION.Store_num=DEPARTMENT.Store_num AND 
      (Fname='?' OR Lname='?' OR Dno='?' OR LOCATION.Store_num ='?')) X"))) { 
      print "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; 
    } 
    if (!$stmt->bind_param("ssii",$_POST['Fname'], $_POST['Lname'], $_POST['Dno'], $_POST['Store_num'])) { 
     print "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 
    if (!$stmt->execute()) { 
     print "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 
    $stmt->store_result(); 

    if (!$stmt->bind_result($Fname,$Minit,$Lname,$Phone,$Address,$Slocation,$Saddress,$Dno,$Hourly)) { 
      print "Binding output parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 
    if ($stmt->num_rows == 0){ 
     print "No results were found for the following search <p>" 
     .$_POST['Fname'].$_POST['Lname'].$_POST['Store_num'].$_POST['Dno']."</p>"; 
     } 
    else { 
     print "<table border=2 cellpadding=4> 
      <tr bgcolor=white> 
      <th>First Name</th> 
      <th>Middle Initial</th> 
      <th>Last Name</th> 
      <th>Phone</th> 
      <th>Address</th> 
      <th>Store</th> 
      <th>Store Location</th> 
      <th>Dept #</th> 
      <th>Hourly Rate</th> 
      </tr>"; 
     while ($stmt->fetch()){ 
      print "<tr><td>".$Fname."</td><td>".$Minit."</td><td>".$Lname. 
      "</td><td>".$Phone."</td><td>".$Address."</td><td>".$Slocation."</td> 
      <td>".$Saddress."</td><td>".$Dno."</td><td>".$Hourly."</td></tr>"; 
     } 
     print "</table>"; 
    } 
$stmt->free_result(); 
} 

$mysqli->close(); 
?> 

回答

1

MySQL表名稱是區分在linux敏感。看到這一點的文件:http://dev.mysql.com/doc/refman/5.1/en/identifier-case-sensitivity.html

所以如果實際的表是「員工」,那麼你不能稱它爲「員工」。

+0

dang ....我認爲這可能太明顯了。謝謝。 – hobbes131

+0

我不會說這很明顯(特別是因爲它因操作系統而異,並且顯然是從底層操作系統o_O上的文件名的區分大小寫繼承的)。我已經使用了很多mysql,直到我測試了一下你的查詢之後才知道。然而,這是在代碼中使用一致的命名約定的一個原因。 – FoolishSeth