2017-07-08 55 views
-1

我正在嘗試設置HTML輸入型文本的文本框的值,當用戶點擊搜索按鈕,清空和EMPID不匹配,但它給錯誤設置文本框的值:我無法使用PHP變量

mysqli_num_rows()預計參數1被mysqli_result

這裏是代碼:

<html> 
<body> 
<form action="" method="post"> 
<h2>Employee Form</h2> 

<input type="text" name="empID"> 
<input type="submit" name="searchRec" value="Search" /> 
<hr> 

    Employee ID: <input type="text" name="empIDC" value="<?php echo htmlentities($employeeID); ?>"> 
    <br><br> 

    Name: <input type="text" name="name" value="<?php echo htmlentities($Name); ?>"> 
    <br><br> 

    Address: <input type="text" name="address" value="<?php echo htmlentities($Address); ?>"> 
    <br><br> 
</form> 
<?php 
if(isset($_REQUEST['searchRec'])) 
{ 
$employeeID = ($_POST["empID"]); 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "bc140_DB"; 

// Create connection 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
$sql = "SELECT empID, Name, Address, Dateofbirth, Salary, Timein from Employee where empID == $employeeID"; 
$result = mysqli_query($conn, $sql); 

if(mysqli_num_rows($result > 0)){ while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { $employeeID = $row['empID']; $Name = $row['Name']; $Address = $row['Address']; $Dateofbirth = $row['Dateofbirth']; $Salary = $row['Salary']; $timestamp = $row['timeIn']; } }else{  $employeeID = ""; $Name = ""; $Address = ""; $Dateofbirth = ""; $Salary = ""; $timestamp = ""; } 

} 
?> 
</body> 
</html> 
+0

相信你會得到未定義的變量錯誤。 – JYoThI

+1

建議:在輸入值中使用'isset()'來檢查並確保該值在聲明它被放置在輸入之前被設置; ''=「<= php =」[ –

+0

可能重複[PHP:]注意:未定義的變量「,」注意:未定義的索引「和」注意:未定義的偏移「](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice- undef) –

回答

2

1st:更改您的code order否則您將收到undefined錯誤。你在創建變量之前試圖用html嵌入變量。

第二:應使用單===empID = $employeeID

3:您的混合mysqlmysqli這裏mysql_fetch_array($result, MYSQL_ASSOC)

更改爲

mysqli_fetch_array($result,MYSQLI_ASSOC); 

4:並且還使用isset()來確認變量是否存在,如果存在則回顯它,否則回顯空字符串。

5:改變你如果這樣if(mysqli_num_rows($result)>0){ }

file.php

<?php 
if(isset($_REQUEST['searchRec'])) 
{ 
    ...... 
    $employeeID = $row['empID']; 
    $Name = $row['Name']; 
    $Address = $row['Address']; 
    $Dateofbirth = $row['Dateofbirth']; 
    $Salary = $row['Salary']; 
    $timestamp = $row['timeIn']; 
    ...... 
} 
?> 

<html> 
<body> 
..... 
    Employee ID: <input type="text" name="empIDC" value="<?php if(isset($employeeID)){ echo htmlentities($employeeID); } else { echo ""; } ?>"> 
..... 

</body> 
</html> 
+0

我在之前通過使php代碼改變了順序,但錯誤仍然存​​在 –

+0

不僅僅是順序,完全讀取了我的答案@Jazib_Prince – JYoThI

+0

我更改了代碼順序,使用了=匹配empID,並使用mysqli_fetch_array($ result,MYSQLI_ASSOC);但錯誤仍然存​​在 –

1

你已經忘記了​​3210 value="<?php echo htmlentities($employeeID); ?>"

+0

感謝您指出錯誤,但插入後仍然存在錯誤;在所有的陳述結束。 –

+1

@Jazib_Prince並改變JYoThI所說的順序。 –

+0

@Jazib_Prince試着改變你的查詢SELECT * FROM Employee where empID == $ employeeID「並嘗試在foreach循環後回顯變量,看看你是否得到它們 –