0

I want to insert multiple rows in a database table, from an submit form. But not able to trouble shoot the problem in my code. Following is the code which is not inserting any row in the database.

<form action=test_insert.php method="post">
<table>
<?php for($i=0; $i<10; $i++)
{ ?>

	<tr>
	<td><input type="text" name="employee_id" value="0" size = "2" ></td>
	<td><input type="text" name="task_no" value="0" size = "2" ></td>
	<td><input type="text" name="discription" value="0" size = "2"></td>
	<td><input type="text" name="mon" value="0" size = "2"></td>
	<td><input type="text" name="tue" value="0" size = "2"></td>
        <td><input type="text" name="wed" value="0" size = "2"></td>
        <td><input type="text" name="thu" value="0" size = "2"></td>
        <td><input type="text" name="fri" value="0" size = "2"></td>
        <td><input type="text" name="sat" value="0" size = "2"></td>
        <td><input type="text" name="sun" value="0" size = "2"></td>
	<td><input type="text" name="total" value="0" size = "2"></td>          
	<td><input type="text" name="week_no" value="0" size = "2"></td>
	</tr>
<?php } ?>

</table>

<input name="submit" value="Submit" type="submit">
</form>

<?php
$conn = pg_connect("host=localhost port=5432 dbname=**** user=postgres password=****");

$query = "insert into public.grid_data (employee_id, task_no, discription, mon, tue, wed, thu, fri, sat, sun, total, week_no)
Values
($employee_id,$task_no,$discription,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$total,$week_no) ";

$res = pg_query($conn, $query) or die(pg_last_error());
if($res){echo("Record added for : $employee_id");}

?>

The 2 errors which I am gettin are. 1. undefined variable for all ( $employee_id, $task_no .....) and
2. Query failed: ERROR: syntax error at or near &quot;,&quot;\nLINE 3: (,,,,,,,,,,,) \n ^

Many thanks in advance

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by tarunkhatri
0

I have changed my sql query to

$query = "insert into public.grid_data (employee_id, task_no, discription, mon, tue, wed, thu, fri, sat, sun, total, week_no)
Values
('$_POST[employee_id]','$_POST[task_no]','$_POST[discription]','$_POST[mon]','$_POST[tue]','$_POST[wed]','$_POST[thu]','$_POST[fri]','$_POST[sat]','$_POST[sun]','$_POST[total]','$_POST[week_no]') ";

and it inserts one row now. But not 10 rows which is my requirement.

Edited by tarunkhatri: n/a

0

Try this? :

<?php
if (!$_POST['submit']) {
?>
<form action=test_insert.php method="post">
<table>
<?php for($i=0; $i<10; $i++)
{ ?>

	<tr>
	<td><input type="text" name="employee_id<?php echo $i; ?>" value="0" size = "2" ></td>
	<td><input type="text" name="task_no<?php echo $i; ?>" value="0" size = "2" ></td>
	<td><input type="text" name="discription<?php echo $i; ?>" value="0" size = "2"></td>
	<td><input type="text" name="mon<?php echo $i; ?>" value="0" size = "2"></td>
	<td><input type="text" name="tue<?php echo $i; ?>" value="0" size = "2"></td>
        <td><input type="text" name="wed<?php echo $i; ?>" value="0" size = "2"></td>
        <td><input type="text" name="thu<?php echo $i; ?>" value="0" size = "2"></td>
        <td><input type="text" name="fri<?php echo $i; ?>" value="0" size = "2"></td>
        <td><input type="text" name="sat<?php echo $i; ?>" value="0" size = "2"></td>
        <td><input type="text" name="sun<?php echo $i; ?>" value="0" size = "2"></td>
	<td><input type="text" name="total<?php echo $i; ?>" value="0" size = "2"></td>          
	<td><input type="text" name="week_no<?php echo $i; ?>" value="0" size = "2"></td>
	</tr>
<?php } ?>

</table>

<input name="submit" value="Submit" type="submit">
</form>
<?php
} else { // If the form is submitted
$conn = pg_connect("host=localhost port=5432 dbname=**** user=postgres password=****");
for ($i=0;$i<10;$i++) {
// All the variables:
$employee_id = $_POST['employee_id$i'];
$task_no = $_POST['task_no$i'];
$discription = $_POST['discription$i'];
$mon = $_POST['mon$i'];
$tue = $_POST['tue$i'];
$wed = $_POST['wed$i'];
$thu = $_POST['thu$i'];
$fri = $_POST['fri$i'];
$sat = $_POST['sat$i'];
$sun = $_POST['sun$i'];
$total = $_POST['total$i'];
$week_no = $_POST['week_no$i'];

$query = "insert into public.grid_data (employee_id, task_no, discription, mon, tue, wed, thu, fri, sat, sun, total, week_no)
Values
('$employee_id','$task_no','$discription','$mon','$tue','$wed','$thu','$fri','$sat','$sun','$total','$week_no') ";
$res = pg_query($conn, $query) or die(pg_last_error());
if($res){echo("Record added for : $employee_id<br />\n");}
} // End of for $i
} // End of the if/else of $_POST['submit']
?>

~G

Edited by Graphix: n/a

0

Hi Graphix,

I was able to solve my problem by using arrays at the input types and loop at the insert statement. Thanks for your help the code u provided is also an alternative.

Many Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.