Not able to insert new rows in a database table | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| |
Hi,
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. - <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 ","\nLINE 3: (,,,,,,,,,,,) \n ^
Many thanks in advance
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,949
| | | re: Not able to insert new rows in a database table
You are not defining the variables anywhere in your script.
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Not able to insert new rows in a database table
I 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]') ";
It inserts one row in the table now. But not 10
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: Not able to insert new rows in a database table Quote:
Originally Posted by tarunkhatri It inserts one row in the table now. But not 10 why should it insert 10 rows?
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Not able to insert new rows in a database table
Because there are 10 rows in the form, in which user is inptting different values. I Just figured out may be I need an array in the input type name of every td, and then a loop at the insert statement.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: Not able to insert new rows in a database table
that sounds reasonable.
| | Newbie | | Join Date: Sep 2009
Posts: 4
| | | re: Not able to insert new rows in a database table
They're all the same name though.
having a form with inputs like this: -
<input type="text" name="sun" value="0" size = "2">
-
<input type="text" name="sun" value="0" size = "2">
-
Means that only one sun will be shown.
To acheive what you want you need to name them: - <input type="text" name="sun[]" value="0" size = "2">
-
<input type="text" name="sun[]" value="0" size = "2">
Then after you POST you must look through them all like sooo.. - foreach($_POST['sun'] as $x=>$Value)
-
{
-
$_POST['mon'][$x] // means if $x is 2 it will get mon[2]
-
}
| | Member | | Join Date: Sep 2009 Location: London
Posts: 36
| | | re: Not able to insert new rows in a database table
Guys, Thank you very much an array at input type name and a loop at insert statement did my job. :)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|