473,320 Members | 1,719 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,320 software developers and data experts.

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

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 10220
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
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.....tanx
Jan 21 '08 #3
Markus
6,050 Expert 4TB
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 Expert 4TB
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 Expert 4TB
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
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="bachelordegree"/>
</td><br>
<td>School Name<input name="sschoolname" type="text" />
</td><br>Year Graduated<input type="text" name="yeargraduated" />
</td><br><input type="submit" name="submit" value="submit">
</form>
[/code]

CODE:-[PHP]

dbConnect.php

if ($connection_type == "local")
{
$db_servername="localhost";
$db_username="root";
$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_link) or die("Not a valid database");

}
[/code]

change the database name accordingly...here it is "form".

CODE:-[PHP]

function.php

<?php


class execute_query
{
//getting the list of categorys
function get_result_details($select_query)
{
require 'dbConnect.php';
$results=array();
$select_query_result=mysql_query($select_query,$db _link) or die(" Not a valid Select Query Category ".mysql_error());
if(mysql_num_rows($select_query_result) > 0)
{
while($get_details=mysql_fetch_array($select_query _result))
{
$array_count=count($get_details)/2;
for($i=0;$i<$array_count;$i++)
{
$results[]=$get_details[$i];
}
}
}
//To Free the result set
mysql_free_result($select_query_result);
//To Close the connection object
mysql_close($db_link);
return $results;
}


//Insert Query function ....
function insert_query($insert_query)
{
require 'dbConnect.php';
$insert_id="";
$insert_query_result=mysql_query($insert_query,$db _link) or die(" Not a valid Insert Query ".mysql_error());
$insert_id=mysql_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="insert into bachelor (bachelordegree,sschoolname,yeargraduated) values ('$bachelordegree','$sschoolname','$yeargraduated' )";
$insert_value= $obj_query->insert_query($insert_name);
header("Location: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
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="bachelordegree"/>
</td><br>
<td>School Name<input name="sschoolname" type="text" />
</td><br>Year Graduated<input type="text" name="yeargraduated" />
</td><br><input type="submit" name="submit" value="submit">
</form>
[/code]

CODE:-[PHP]

dbConnect.php

if ($connection_type == "local")
{
$db_servername="localhost";
$db_username="root";
$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_link) or die("Not a valid database");

}
[/code]

change the database name accordingly...here it is "form".

CODE:-[PHP]

function.php

<?php


class execute_query
{
//getting the list of categorys
function get_result_details($select_query)
{
require 'dbConnect.php';
$results=array();
$select_query_result=mysql_query($select_query,$db _link) or die(" Not a valid Select Query Category ".mysql_error());
if(mysql_num_rows($select_query_result) > 0)
{
while($get_details=mysql_fetch_array($select_query _result))
{
$array_count=count($get_details)/2;
for($i=0;$i<$array_count;$i++)
{
$results[]=$get_details[$i];
}
}
}
//To Free the result set
mysql_free_result($select_query_result);
//To Close the connection object
mysql_close($db_link);
return $results;
}


//Insert Query function ....
function insert_query($insert_query)
{
require 'dbConnect.php';
$insert_id="";
$insert_query_result=mysql_query($insert_query,$db _link) or die(" Not a valid Insert Query ".mysql_error());
$insert_id=mysql_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="insert into bachelor (bachelordegree,sschoolname,yeargraduated) values ('$bachelordegree','$sschoolname','$yeargraduated' )";
$insert_value= $obj_query->insert_query($insert_name);
header("Location: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
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,...
3
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 ...
12
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...
2
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...
4
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...
2
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,...
11
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...
24
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...
58
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.