我正在嘗試設置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>
相信你會得到未定義的變量錯誤。 – JYoThI
建議:在輸入值中使用'isset()'來檢查並確保該值在聲明它被放置在輸入之前被設置; ''=「<= php =」[ –
可能重複[PHP:]注意:未定義的變量「,」注意:未定義的索引「和」注意:未定義的偏移「](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice- undef) –