-1

I'm trying to set value of html input type text textboxes to empty when user clicks Search button and empID is not matched, but its giving error:

mysqli_num_rows() expects parameter 1 to be mysqli_result

Here is code:

<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>
Jazib_Prince
  • 39
  • 11
  • sure you will get undefined variable error . – JYoThI Jul 08 '17 at 10:30
  • 1
    Suggestion: use `isset()` in your inputs values to check and make sure the value is set before declaring it be placed there in the input; `value=""` – dale landry Jul 08 '17 at 10:40
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – user3942918 Jul 08 '17 at 10:48

2 Answers2

2

1st : Change your code order otherwise you will get undefined error . your trying the embed the variable with html before creating the variable .

2nd : should be use single = not == empID = $employeeID

3rd : your mixing mysql with mysqli here mysql_fetch_array($result, MYSQL_ASSOC)

Change to

mysqli_fetch_array($result,MYSQLI_ASSOC);

4th: And also use isset() to confirm that variable exists or not if exists echo it otherwise echo the empty string .

5th: change your if like this 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>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • I changed the order by making php code before but errors are still there. – Jazib_Prince Jul 08 '17 at 10:36
  • I changed code order, used = to match empID, and used mysqli_fetch_array($result,MYSQLI_ASSOC); but errors are still there – Jazib_Prince Jul 08 '17 at 10:46
  • can you update your new code in post . @Jazib_Prince – JYoThI Jul 08 '17 at 10:49
  • Thanks JYoThl by follwoing all steps you mentioned+using isset solved the issue. Now I'm trying to do this: 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 = ""; }its not working error: mysqli_num_rows() expects parameter 1 to be mysqli_result – Jazib_Prince Jul 08 '17 at 10:58
  • Please dont' push the code in comment box . it's too hard to read . try to edit your post using edit button @Jazib_Prince – JYoThI Jul 08 '17 at 11:04
  • change your if like this if(mysqli_num_rows($result)>0){ } – JYoThI Jul 08 '17 at 11:10
  • @JYoThl I edited the post and if(mysqli_num_rows($result)>0) this didn't worked – Jazib_Prince Jul 08 '17 at 11:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148678/discussion-between-jazib-prince-and-jyothi). – Jazib_Prince Jul 08 '17 at 11:12
  • still your code not updated fully . here empID == $employeeID . please update entire code . @Jazib_Prince – JYoThI Jul 08 '17 at 11:15
1

you have forgotten ';' value="<?php echo htmlentities($employeeID); ?>"

Leo
  • 7,274
  • 5
  • 26
  • 48
  • Thanks for pointing out mistake, but errors are still there after inserting ; at the end of all statements. – Jazib_Prince Jul 08 '17 at 10:33
  • 1
    @Jazib_Prince and change the order like JYoThI said. – Leo Jul 08 '17 at 10:34
  • @Jazib_Prince try change ur query SELECT * FROM Employee where empID == $employeeID" and try to echo the variables after foreach loop to see if u are getting them – Leo Jul 08 '17 at 10:40
  • var_dump() your query to see if it gets results. Check the query stmt in SQL and see if passes an SQL entry in phpMyAdmin if available. – dale landry Jul 08 '17 at 10:42