473,811 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert multiple rows into a table from a POST data array.

14 New Member
hi to all

i am confused right now.. im just new in php.. and i have syntax error in my codes.. pls take a look?
Expand|Select|Wrap|Line Numbers
  1. database name: ranking
  2. table: form1
  3. fields: form1academicbachelor
  4.           form1academicschoolyear
  5.  

form:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="b[]" value="" /><input>
  2. <input type="text" name="s[]" value="" /><input>
  3. <input type="text" name="b[]" value="" /><input>
  4. <input type="text" name="s[]" value="" /><input>
  5. <input type="text" name="b[]" value="" /><input>
  6.  
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. include 'dbConnect.php';
  4.     mysql_select_db("ranking",$con);
  5.  
  6.  
  7. foreach($_POST['b'] as &$value) {
  8.     $value="'".$value."'";
  9. }
  10. $datos_uno=implode("<br>", $_POST['b']);
  11.  
  12. $sql="INSERT INTO form1(form1academicbachelor) VALUES ('$datos_uno')";
  13. mysql_query($sql,$con) 
  14.  
  15. or die (mysql_error($con));
  16. ?>
  17.  
thats my error when it was outpued:
Expand|Select|Wrap|Line Numbers
  1. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bsa'
  2. 'bsit'
  3. 'bb'')' at line 1
  4.  
i want my out put have an out put form the textbox..
Expand|Select|Wrap|Line Numbers
  1. b[0]='variable    s[0]='variable
  2. b[1]='variable    s[1]='variable
  3. b[2]='variable    s[2]='variable
  4.  
my problem here is the inserting this multiple fields in the database..
could you make suggestion how to insert an array having multiple records??
Jan 21 '08 #1
7 10454
enumula
7 New Member
Hi, in the below code u hve used the same name for 2 input fields....which is worng...like b ,b n s,s.
form:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="b[]" value="" /><input>
  2. <input type="text" name="s[]" value="" /><input>
  3. <input type="text" name="b[]" value="" /><input>
  4. <input type="text" name="s[]" value="" /><input>
  5. <input type="text" name="b[]" value="" /><input>
  6.  
  7.  

the other way to do it is...use b,b1 ans s,s1.
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="b[]" value="" /><input>
  2. <input type="text" name="s[]" value="" /><input>
  3. <input type="text" name="b1[]" value="" /><input>
  4. <input type="text" name="s1[]" value="" /><input>
  5. <input type="text" name="b2[]" value="" /><input>
  6.  
take two arrays.

can u plzz paste d code wht exactly ur looking for.
Jan 21 '08 #2
annerhexian
14 New Member
Hi, in the below code u hve used the same name for 2 input fields....which is worng...like b ,b n s,s.
form:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="b[]" value="" /><input>
  2. <input type="text" name="s[]" value="" /><input>
  3. <input type="text" name="b[]" value="" /><input>
  4. <input type="text" name="s[]" value="" /><input>
  5. <input type="text" name="b[]" value="" /><input>
  6.  

the other way to do it is...use b,b1 ans s,s1.
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="b[]" value="" /><input>
  2. <input type="text" name="s[]" value="" /><input>
  3. <input type="text" name="b1[]" value="" /><input>
  4. <input type="text" name="s1[]" value="" /><input>
  5. <input type="text" name="b2[]" value="" /><input>
  6.  
take two arrays.

can u plzz paste d code wht exactly ur looking for.
here's my problem..

Expand|Select|Wrap|Line Numbers
  1. <form name="" method="POST" action="">
  2. <td>
  3.                   Bachelor Degree
  4.                       <input type="text" name="bachelordegree[]"  />          
  5. </td>
  6.  <td>
  7.                School Name 
  8.                           <input name="sschoolname[]" type="text" i/>
  9.  </td>
  10.                 Year Graduated 
  11.                      <input type="text" name="yeargraduated[]" />
  12. </td>
  13. <td>
  14.                   Bachelor Degree
  15.                       <input type="text" name="bachelordegree[]"  />          
  16. </td>
  17.  <td>
  18.                School Name 
  19.                           <input name="sschoolname[]" type="text" i/>
  20.  </td>
  21.                 Year Graduated 
  22.                      <input type="text" name="yeargraduated[]" />
  23. </td>
  24. <td>
  25.                   Bachelor Degree
  26.                       <input type="text" name="bachelordegree[]"  />          
  27. </td>
  28.  <td>
  29.                School Name 
  30.                           <input name="sschoolname[]" type="text" i/>
  31.  </td>
  32.                 Year Graduated 
  33.                      <input type="text" name="yeargraduated[]" />
  34. </td>
  35.  
  36.  
  37. </form>
  38.  
this is my form.page.. like for example that they want to input in every textbox like textbox1 Bachelor degree is BSA, textbox in Bachelor Degree is BEED amd textbox2 Bachelor degree is BSIT..and like the other textbox as weso when i view it.. it will be like this
Expand|Select|Wrap|Line Numbers
  1. BACHELOR DEGREE  SCHOOL NAME             SCHOOL YEAR
  2. BSAA             schoolname              1956
  3. BEED             schoolnameyeha          1975
  4. BEsa             usc                     2004
  5.  
how to insert this kind of arrays in teh database.....ta nx
Jan 21 '08 #3
Markus
6,050 Recognized Expert Expert
Hi, in the below code u hve used the same name for 2 input fields....which is worng...like b ,b n s,s.
form:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="b[]" value="" /><input>
  2. <input type="text" name="s[]" value="" /><input>
  3. <input type="text" name="b[]" value="" /><input>
  4. <input type="text" name="s[]" value="" /><input>
  5. <input type="text" name="b[]" value="" /><input>
  6.  
It isn't wrong, it's actually correct what he/she's done. you can use the same name= if you plan to put them into arrays, hence the brackets after the name.

To the OP: may i ask why you are putting them into arrays? Arrays are normally used for things such as checkboxes, etc. It seems pointless to use in your case..
Jan 21 '08 #4
Atli
5,058 Recognized Expert Expert
Hi everybody.

Please use [code] tags when posting your code examples. (See How to ask a question)

[code=php] ...PHP code goes here... [/code]

I've also edited the title's thread to better describe it's contents.
Please do not use phrases like "help me" or "need help" as your title.

Thank you.
Jan 21 '08 #5
Atli
5,058 Recognized Expert Expert
The problem is, like Marcus pointed out, not that you use arrays when getting the values.

The problem is how you try to put them into your database.

To insert multiple rows into a table, your query should look something like this:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `Table1` (`Column1`)
  2. VALUES ('Data1'), ('Data2'), ('Data3') /* etc. */
  3.  
So, if you have an array of textboxed posted, you could do something like:
Expand|Select|Wrap|Line Numbers
  1. # Create the query
  2. $query = "INSERT INTO `Table1` (`Column1`) VALUES";
  3.  
  4. # Add each text box value to the query
  5. foreach($_POST['myTextboxArray'] as $text) {
  6.   $query .= " ('". addslashes($text) ."'),";
  7. }
  8.  
  9. # Remove the last comma from the query
  10. $query[strlen($query)-1] = "";
  11.  
I use the addslashes function there to make sure there are no single quote marks (') in the text to mess up the query.
(Which, btw, was the problem with the query in your first post)
Jan 21 '08 #6
enumula
7 New Member
Hey i think u hve posted a form titled have a problem in arrays that having multiple forms.

i hve posted a reply for it try to execute it 1's...then i hope u wont get any doubts on inserting the data.

anyways here is code for u ....check it out ..

Code:-[HTML]

form.html

<form name="" method="POST" action="insert. php">
<td>Bachelor Degree<input type="text" name="bachelord egree"/>
</td><br>
<td>School Name<input name="sschoolna me" type="text" />
</td><br>Year Graduated<input type="text" name="yeargradu ated" />
</td><br><input type="submit" name="submit" value="submit">
</form>
[/code]

CODE:-[PHP]

dbConnect.php

if ($connection_ty pe == "local")
{
$db_servername= "localhost" ;
$db_username="r oot";
$db_password="" ;
$db_name="form" ;
$db_link = mysql_connect($ db_servername, $db_username, $db_password) or die("could not connect to the local atebase server");
$db= mysql_select_db ($db_name,$db_l ink) or die("Not a valid database");

}
[/code]

change the database name accordingly...h ere it is "form".

CODE:-[PHP]

function.php

<?php


class execute_query
{
//getting the list of categorys
function get_result_deta ils($select_que ry)
{
require 'dbConnect.php' ;
$results=array( );
$select_query_r esult=mysql_que ry($select_quer y,$db_link) or die(" Not a valid Select Query Category ".mysql_error() );
if(mysql_num_ro ws($select_quer y_result) > 0)
{
while($get_deta ils=mysql_fetch _array($select_ query_result))
{
$array_count=co unt($get_detail s)/2;
for($i=0;$i<$ar ray_count;$i++)
{
$results[]=$get_details[$i];
}
}
}
//To Free the result set
mysql_free_resu lt($select_quer y_result);
//To Close the connection object
mysql_close($db _link);
return $results;
}


//Insert Query function ....
function insert_query($i nsert_query)
{
require 'dbConnect.php' ;
$insert_id="";
$insert_query_r esult=mysql_que ry($insert_quer y,$db_link) or die(" Not a valid Insert Query ".mysql_error() );
$insert_id=mysq l_insert_id();
mysql_close($db _link);
return $insert_id;
}


}
?>

[/code]

CODE:-[php]

insert.php



<?
session_Start() ;

require 'function.php';
$obj_query= new execute_query() ;
$bachelordegree =$_POST['bachelordegree '];
$sschoolname=$_ POST['sschoolname'];
$yeargraduated= $_POST['yeargraduated'];
$insert_name="i nsert into bachelor (bachelordegree ,sschoolname,ye argraduated) values ('$bachelordegr ee','$sschoolna me','$yeargradu ated')";
$insert_value= $obj_query->insert_query($ insert_name);
header("Locatio n:form.html");
exit;
?>

[/code]

CODE:- text.

database text.

-- phpMyAdmin SQL Dump
-- version 2.6.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 22, 2008 at 11:13 AM
-- Server version: 4.1.9
-- PHP Version: 4.3.10
--
-- Database: `form`
--
-- --------------------------------------------------------
--
-- Table structure for table `bachelor`
--
DROP TABLE IF EXISTS `bachelor`;
CREATE TABLE `bachelor` (
`bachelor_id` int(11) NOT NULL auto_increment,
`bachelordegree ` varchar(100) NOT NULL default '',
`sschoolname` varchar(100) NOT NULL default '',
`yeargraduated` varchar(100) NOT NULL default '',
PRIMARY KEY (`bachelor_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT= 10 ;
--
-- Dumping data for table `bachelor`
--
INSERT INTO `bachelor` VALUES (1,'BSC', 'Keys high school', '2000');
Jan 22 '08 #7
annerhexian
14 New Member
Hey i think u hve posted a form titled have a problem in arrays that having multiple forms.

i hve posted a reply for it try to execute it 1's...then i hope u wont get any doubts on inserting the data.

anyways here is code for u ....check it out ..

Code:-[HTML]

form.html

<form name="" method="POST" action="insert. php">
<td>Bachelor Degree<input type="text" name="bachelord egree"/>
</td><br>
<td>School Name<input name="sschoolna me" type="text" />
</td><br>Year Graduated<input type="text" name="yeargradu ated" />
</td><br><input type="submit" name="submit" value="submit">
</form>
[/code]

CODE:-[PHP]

dbConnect.php

if ($connection_ty pe == "local")
{
$db_servername= "localhost" ;
$db_username="r oot";
$db_password="" ;
$db_name="form" ;
$db_link = mysql_connect($ db_servername, $db_username, $db_password) or die("could not connect to the local atebase server");
$db= mysql_select_db ($db_name,$db_l ink) or die("Not a valid database");

}
[/code]

change the database name accordingly...h ere it is "form".

CODE:-[PHP]

function.php

<?php


class execute_query
{
//getting the list of categorys
function get_result_deta ils($select_que ry)
{
require 'dbConnect.php' ;
$results=array( );
$select_query_r esult=mysql_que ry($select_quer y,$db_link) or die(" Not a valid Select Query Category ".mysql_error() );
if(mysql_num_ro ws($select_quer y_result) > 0)
{
while($get_deta ils=mysql_fetch _array($select_ query_result))
{
$array_count=co unt($get_detail s)/2;
for($i=0;$i<$ar ray_count;$i++)
{
$results[]=$get_details[$i];
}
}
}
//To Free the result set
mysql_free_resu lt($select_quer y_result);
//To Close the connection object
mysql_close($db _link);
return $results;
}


//Insert Query function ....
function insert_query($i nsert_query)
{
require 'dbConnect.php' ;
$insert_id="";
$insert_query_r esult=mysql_que ry($insert_quer y,$db_link) or die(" Not a valid Insert Query ".mysql_error() );
$insert_id=mysq l_insert_id();
mysql_close($db _link);
return $insert_id;
}


}
?>

[/code]

CODE:-[php]

insert.php



<?
session_Start() ;

require 'function.php';
$obj_query= new execute_query() ;
$bachelordegree =$_POST['bachelordegree '];
$sschoolname=$_ POST['sschoolname'];
$yeargraduated= $_POST['yeargraduated'];
$insert_name="i nsert into bachelor (bachelordegree ,sschoolname,ye argraduated) values ('$bachelordegr ee','$sschoolna me','$yeargradu ated')";
$insert_value= $obj_query->insert_query($ insert_name);
header("Locatio n:form.html");
exit;
?>

[/code]

CODE:- text.

database text.

-- phpMyAdmin SQL Dump
-- version 2.6.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 22, 2008 at 11:13 AM
-- Server version: 4.1.9
-- PHP Version: 4.3.10
--
-- Database: `form`
--
-- --------------------------------------------------------
--
-- Table structure for table `bachelor`
--
DROP TABLE IF EXISTS `bachelor`;
CREATE TABLE `bachelor` (
`bachelor_id` int(11) NOT NULL auto_increment,
`bachelordegree ` varchar(100) NOT NULL default '',
`sschoolname` varchar(100) NOT NULL default '',
`yeargraduated` varchar(100) NOT NULL default '',
PRIMARY KEY (`bachelor_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT= 10 ;
--
-- Dumping data for table `bachelor`
--
INSERT INTO `bachelor` VALUES (1,'BSC', 'Keys high school', '2000');

=============== =============== ====
Tanx..I already know how to insert it to the database from the form - i declare tha variable as an array den after that i use the implode for its functionality is array become as a string.. so wen i input it in my textbox1=BSA textbox2=BSIT and textbox 3= BSCS... it was already inserted in the database..tanx for the help friend...
ite become like this..

in the database...
BachelorDgree
BSA
BSIT
BSCS

THANK YOU FOR THE HELP AND TIPS.....
Jan 31 '08 #8

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

Similar topics

9
3466
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, VARCHAR(10) WO.PROBLEMCODE, VARCHAR(8)
3
4332
by: Hai Nguyen | last post by:
Hi all I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field (PR) (PR) ID Project Ans 1 2 a 1 3 b 1 4 c 1 5 d
12
3014
by: shank | last post by:
I'm trying to use online samples for submitting multiple records from ASP into a stored procedure. Failing! Through the below form, a user could be submitting many records at a time. I'm not getting any records inserted. For troubleshooting, I cut the form down to 1 textbox and when submitted it populated 5 rows of the same data. So I know I'm connected, but not getting the LOOP and NEXT correct? How do I set this up so many records can be...
2
3686
by: wombat53 | last post by:
Hi Group Are there any DB2 UDB ESE DPF V8.2 users exploiting "buffered inserts" (BIND parm INSERT BUF) *and* "multi-row INSERTS" (many rows associated with the VALUES clause of the INSERT to minimize number of calls to the RDMBS engine) in an SQL PL Stored Procedure? I ask, as the latter would imply embedded DYNAMIC SQL due to potentially varying number of rows to be inserted (assume it's varying), and so, the SQL INSERT would have be...
4
2918
by: Michel Esber | last post by:
Hello, Environment: db2 V8 FP 13 LUW Our application currently uses: insert into table values ('A'),('B'),...('Z') We have used CLI arrays inserts (1000 array and commit size) and managed to insert 1 Million rows into an empty table in 32 seconds. Our
2
7055
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
11
15303
by: TechnoAtif | last post by:
INSERT AND UPDATE MULTIPLE CHECKBOX DATA USING PHPMYSQL OR JAVASCRIPT Hi All I want to check the multiple checkboxes update them after revisiting that page. I am taking the name as name=type..in the form of array..but on checking it the result in the database simply gives the world 'array'.i want multiple checkboxes to be checked and updated simultaneously.. and the query to insert the checkbox value has to be included along with...
24
7153
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases where the table is not empty, it can take care of the updating part, which makes the app cleaner. However, my concern is the merge state would slow dowm the insertion of new data, since in most cases the table is empty. So my questions (before I...
58
8154
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.