I’m trying to enter employee id’s (Emp_ID) into the schedule table 30 times. The employee id’s are being pulled from the employee table. It will work on the first loop but then breaks on the second. The error I get is
“Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\server\roster\dates.php on line 110”
Line 110 is the while loop. I can only assume this is happening because the result set is being emptied, but I’m not sure how to fix it.
<?php
//Select all of the current Employees by ID number
$sql = ("SELECT Emp_ID FROM employee");
//Run a check on the query to make sure it worked.
//if it failed then print the error.
if(!$result = $mysqli->query($sql))
{
die('There was an error getting the Emp_ID from the employee table [' . $mysqli->error . ']');
}
//Loop through the results...
while($row = $result->fetch_assoc())
{
//...and for each employee ID, enter it into the table 30 times.
for($i = 1; $i <= 30; $i++ )
{
$sql = ("INSERT INTO schedule (Emp_ID) VALUES ('" . $row['Emp_ID'] . "')");
//Run a check on the query to make sure it worked.
//if it failed then print the error.
if(!$result = $mysqli->query($sql))
{
die('There was an error inserting the Emp_ID into the schedule [' . $mysqli->error . ']');
}
}
}
?>
$resultis already in use onwhileloop and you can’t use same name inside the loop that will overwrite the current values of$resultChange the
$resultto different variable name$result_insert(I mean after the insert query)if(!$result_insert = $mysqli->query($sql))