我无法使用php变量设置文本框的值

时间:2017-07-08 10:28:18

标签: php html

我正在尝试将html输入类型文本文本框的值设置为空,当用户单击“搜索”按钮并且empID不匹配时,但是它给出错误:

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>

2 个答案:

答案 0 :(得分:2)

第一次:更改code order,否则会出现undefined错误。你在创建变量之前尝试用html嵌入变量。

第二名应使用单=而不是== empID = $employeeID

第3次mysqlmysqli mysql_fetch_array($result, MYSQL_ASSOC)混合{/ 1}

更改为

mysqli_fetch_array($result,MYSQLI_ASSOC);

第4名并使用isset()确认该变量是否存在,如果存在则回显它,否则回显空字符串。

第5名:改变你的if 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>

答案 1 :(得分:1)

您忘记了';' value="<?php echo htmlentities($employeeID); ?>"