I have a form that is created by querying my database for user names and id. I then create the rest of the form using an array for the hours input box. When the form is submitted, I want to insert the information into my database. When I submit the page now, I get an error about the array. Does anyone have experience with this. I really appreciate any help you maybe able to provide. Here is my code:
<?php
if (isset($_POST['submitted'])) {
$pline = ($_POST['pline']);
$date = ($_POST['date']);
$pid = ($_POST['pid']);
$hours=$_POST['hours'];
foreach($hours as $value)
{
include 'config.php';
/*** create a new mysqli object with default database***/
$connection = mysqli_connect($hostname, $username, $password, $dbname) or die ("Unable to connect");
// create insert command
$sql = "insert into prod_timesheet
(emp_no, date, hours, prod_line) values
('$id', '$date', '$hours, $pid')";
//excute query
$result = mysqli_query($connection, $sql) or die ("Error in query: $sql. ".mysqli_error());
// close connection
mysqli_close($connection);
}
//if - else statement isset
} else {
?>
<head>
<title>Production Employee List</title>
</head>
<body>
<?php
$date = date("Y-m-d G:i:s");
?>
<div align="center">
<table width="300" border="1" cellpadding="4" style='border-collapse: collapse'>
<tr><form name="main" method="post" action="employee_hours_2.php">
<td colspan=4 align=center bgcolor="#CCCCCC"><strong>Select Production Line</strong></td>
</tr>
<tr>
<td colspan=4 align="center"><select id="pline" name="pline">
<option selected>- Click Here -</option>
<?php
include 'config.php';
/*** create a new mysqli object with default database***/
$connection = mysqli_connect($hostname, $username, $password, $dbname) or die ("Unable to connect");
//create query
$query1 = "SELECT id, production_line FROM prod_line where active = 'yes'";
//excute query
$result1 = mysqli_query($connection, $query1) or die ("Error in query: $query1. ".mysqli_error());
//create list of variables from query results
while(list($pid, $prod_line) = mysqli_fetch_row($result1))
{
echo "<option value=$pid>$prod_line</option>";
}
?>
</option>
</select>
</td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<p>
<div align="center">
<tr>
<td width="50" bgcolor="#CCCCCC"><div align="center"><strong>ID</strong></div></td>
<td width="125" bgcolor="#CCCCCC"><strong>First Name</strong></td>
<td width="125" bgcolor="#CCCCCC"><strong>Last Name</strong></td>
<td width="100" bgcolor="#CCCCCC"><strong>Hours</strong></td>
</tr>
<?php
//create query
$query = "SELECT id, fname, lname FROM prod_employee where active = 'yes'";
//excute query
$result = mysqli_query($connection, $query) or die ("Error in query: $query. ".mysqli_error());
// see if any rows were returned
if (mysqli_num_rows($result)) {
// yes
// print them one after another
while (list($id, $fname, $lname) = mysqli_fetch_row($result))
{
echo '<tr>';
echo '<td><div align=center>'.$id.'</div></td>';
echo '<td>'.$fname.'</td>';
echo '<td>'.$lname.'</td>';
echo '<td><input name="hours[]" type="text" size="4" maxlength="4" value="0">';
echo '</tr>';
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysqli_free_result($result);
// close connection
mysqli_close($connection);
?>
<tr>
<td colspan ="4"><div align="center"> </div></td>
</tr>
<tr>
<td colspan ="4"><div align="center"><input type="submit" name="Submit" value="Submit">
<input type="hidden" name="submitted" value="true" />
<input type="hidden" name="eid" value="<?php echo $id ?>">
<input type="hidden" name="pid" value="<?php echo $pid ?>">
<input type="hidden" name="date" value="<?php echo $date; ?>">
</div></td>
</tr>
<tr>
<td colspan ="4"><div align="center"> </div></td>
</tr>
<tr>
<td colspan ="4"><div align="center"><a href="index.php">Home</a></div></td>
</tr>
</table>
</div>
</body>
<?php
}
?>
Thanks for looking.
A