Connecting Tech Pros Worldwide Forums | Help | Site Map

Not able to insert new rows in a database table

Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#1: Sep 29 '09
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.

Expand|Select|Wrap|Line Numbers
  1. <form action=test_insert.php method="post">
  2. <table>
  3. <?php for($i=0; $i<10; $i++)
  4. { ?>
  5.  
  6.     <tr>
  7.     <td><input type="text" name="employee_id" value="0" size = "2" ></td>
  8.     <td><input type="text" name="task_no" value="0" size = "2" ></td>
  9.     <td><input type="text" name="discription" value="0" size = "2"></td>
  10.     <td><input type="text" name="mon" value="0" size = "2"></td>
  11.     <td><input type="text" name="tue" value="0" size = "2"></td>
  12.         <td><input type="text" name="wed" value="0" size = "2"></td>
  13.         <td><input type="text" name="thu" value="0" size = "2"></td>
  14.         <td><input type="text" name="fri" value="0" size = "2"></td>
  15.         <td><input type="text" name="sat" value="0" size = "2"></td>
  16.         <td><input type="text" name="sun" value="0" size = "2"></td>
  17.     <td><input type="text" name="total" value="0" size = "2"></td>          
  18.     <td><input type="text" name="week_no" value="0" size = "2"></td>
  19.     </tr>
  20. <?php } ?>
  21.  
  22. </table>
  23.  
  24. <input name="submit" value="Submit" type="submit">
  25. </form>
  26.  
  27. <?php
  28. $conn = pg_connect("host=localhost port=5432 dbname=*** user=postgres password=*****");
  29.  
  30. $query = "insert into public.grid_data (employee_id, task_no, discription, mon, tue, wed, thu, fri, sat, sun, total, week_no)
  31. Values
  32. ($employee_id,$task_no,$discription,$mon,$tue,$wed,$thu,$fri,$sat,$sun,$total,$week_no) ";
  33.  
  34. $res = pg_query($conn, $query) or die(pg_last_error());
  35. if($res){echo("Record added for : $employee_id");}
  36.  
  37. ?>
  38.  
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

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#2: Sep 29 '09

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
#3: Sep 29 '09

re: Not able to insert new rows in a database table


I changed my sql query to

Expand|Select|Wrap|Line Numbers
  1. $query = "insert into public.grid_data (employee_id, task_no, discription, mon, tue, wed, thu, fri, sat, sun, total, week_no)
  2. Values
  3. ('$_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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#4: Sep 29 '09

re: Not able to insert new rows in a database table


Quote:

Originally Posted by tarunkhatri View Post

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
#5: Sep 29 '09

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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#6: Sep 29 '09

re: Not able to insert new rows in a database table


that sounds reasonable.
Newbie
 
Join Date: Sep 2009
Posts: 4
#7: Sep 29 '09

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:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="sun" value="0" size = "2">
  2. <input type="text" name="sun" value="0" size = "2">
  3.  
Means that only one sun will be shown.

To acheive what you want you need to name them:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="sun[]" value="0" size = "2">
  2. <input type="text" name="sun[]" value="0" size = "2">
Then after you POST you must look through them all like sooo..

Expand|Select|Wrap|Line Numbers
  1. foreach($_POST['sun'] as $x=>$Value)
  2. {
  3.   $_POST['mon'][$x] // means if $x is 2 it will get mon[2]
  4. }
Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#8: Sep 29 '09

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. :)
Reply