473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Not able to insert new rows in a database table

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
Sep 29 '09 #1
7 2278
Markus
6,050 Expert 4TB
You are not defining the variables anywhere in your script.
Sep 29 '09 #2
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
Sep 29 '09 #3
Dormilich
8,658 Expert Mod 8TB
@tarunkhatri
why should it insert 10 rows?
Sep 29 '09 #4
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.
Sep 29 '09 #5
Dormilich
8,658 Expert Mod 8TB
that sounds reasonable.
Sep 29 '09 #6
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. }
Sep 29 '09 #7
Guys, Thank you very much an array at input type name and a loop at insert statement did my job. :)
Sep 29 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
10
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable...
5
by: Paul Shaw | last post by:
Can anyone explain why an insert might cause multiple logical bufferpool data reads? Here's a situation that has me scratching my head. Table A's data resides in tablespace B Table A's...
6
by: Roy Gourgi | last post by:
Hi, I am trying to add a row to my table but I get the error message "invalid column name SOBN and BN1" on this statement. Basically, I am trying to add the row into the same table that I am...
11
by: Sezai YILMAZ | last post by:
Hello I need high throughput while inserting into PostgreSQL. Because of that I did some PostgreSQL insert performance tests. ------------------------------------------------------------ --...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
46
by: dunleav1 | last post by:
I have a process that does inserts that runs 50% slower that Oracle and Mssql. Queries normally take 50% slower than normal. DB2, Oracle, Mssql all are configured on same os, same disk array...
0
by: Robert Bravery | last post by:
HI all, VS 2005 C# MSSQL2005 I have a parent child grandchild related tables. Claimheader, claim (deteails), claimfinancialloss When ever I try to add row to claimfinancialloss table, I get...
2
by: cday119 | last post by:
Can someone help me out with this one. I am trying to insert a row of data into an access database. The primary key is set to auto increment. It will work once but the next time you try an insert...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.