Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 2008-03-20, 02:14 PM
Junior Member
 
Join Date: Dec 2007
Posts: 6
cancer10 is on a distinguished road
Question PHP/MySql - Inserting Multiple Records

I want to insert records into multiple tables at one go, I am doing this in the following way, is this the correct way of doing? If not, what is the better way?



Code:
mysql_query("insert into table1 values('abc')");
mysql_query("insert into table2 values('ttt')");
mysql_query("insert into table3 values('5t6')");
mysql_query("insert into table4 values('ghy')");
mysql_query("insert into table5 values('gfd')");


Please help


Thanx
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2008-04-14, 01:47 AM
Junior Member
 
Join Date: Apr 2008
Location: Cheltenham, UK
Posts: 11
hoopyfrood is on a distinguished road
Default FIXABLE: PHP/MySQL inserting multiple records

Hi cancer10,

what you're doing is indeed one of the many ways of doing it. What you're essentially doing is querying the database each time you're entering the data.

providing you know how many records you'd be entering you could either:
  • a) merge it all in one long query
  • b) use foreach/while (whichever you prefer) to build the dynamic SQL and query it automatically
  • c) does each record goes to defferent table? you could optimise your SQL to group relevant SQL...
If you feel like providing sample of your code I might be able to help you more...

Good luck,
hoopyfrood
__________________
Love your documents!

DocuView - The best Web 2.0 document management system (DMS) - utilising the latest technology to help your company achieve ISO 9001 certification.
Reply With Quote
  #3 (permalink)  
Old 2008-04-14, 07:44 PM
Junior Member
 
Join Date: Apr 2008
Posts: 7
flforlife is on a distinguished road
Default I had the same question

insert multple records in a table at one time!



Hi i'm trying to develop an application where I can add records using one php script and one form. Form that add multiple records to a database. I've been searching but i cant find a solution.

Majors scoreboard

Place Team WL GB %AGE etc....
1 cards 5-0 7 1
2 jays 9-0 7 3
3 yanks 5-0 7 3
4 soxs 9-0 7 2



here is the database setup from ^....

CREATE TABLE major (
id INT NOT NULL AUTO_INCREMENT,
place VARCHAR(65) NOT NULL DEFAULT '',
team VARCHAR(65) NOT NULL DEFAULT '',
wl VARCHAR(65) NOT NULL DEFAULT '',
gb VARCHAR(65) NOT NULL DEFAULT '',
pcage VARCHAR(65) NOT NULL DEFAULT '',
rs VARCHAR(65) NOT NULL DEFAULT '',
ra VARCHAR(65) NOT NULL DEFAULT '',
ags VARCHAR(65) NOT NULL DEFAULT '',
PRIMARY KEY(id)
);




here is the form...


<form method="post" action="add.php">
<h1>Major</h1>
<table border="1">

<tr>
<td>PLACE</td>
<td>TEAM</td>
<td>WIN-LOSE</td>
<td>GB</td>
<td>%AGE</td>
<td>RUNS SCORED</td>
<td>RUNS ALLOWED</td>
<td>AVERAGE GAME SCORE</td>
</tr>

<tr>
<td><input name="place" type="text" id="place" /></td>
<td><input name="team" type="text" id="team" /></td>
<td><input name="wl" type="text" id="wl" /></td>
<td><input name="gb" type="text" id="gb" /></td>
<td><input name="pcage" type="text" id="pcage" /></td>
<td><input name="rs" type="text" id="rs" /></td>
<td><input name="ra" type="text" id="ra" /></td>
<td><input name="ags" type="text" id="ags" /></td>
</tr>

<tr>
<td><input name="place" type="text" id="place" /></td>
<td><input name="team" type="text" id="team" /></td>
<td><input name="wl" type="text" id="wl" /></td>
<td><input name="gb" type="text" id="gb" /></td>
<td><input name="pcage" type="text" id="pcage" /></td>
<td><input name="rs" type="text" id="rs" /></td>
<td><input name="ra" type="text" id="ra" /></td>
<td><input name="ags" type="text" id="ags" /></td>
</tr>


</table>
<input type="submit" />

</form>


I want to add more records using php. here is my add.php code


<?php

//add fourteam data

$hostname = 'localhost';
$username = 'root';
$password = ''; //add later
$database = 'test';

$mysql = mysql_connect($hostname, $username, $password);
mysql_select_db($database);



$sql="INSERT INTO major (place, team, wl, gb, pcage, rs, ra, ags)
VALUES
('$_POST[place]','$_POST[team]','$_POST[wl]','$_POST[gb]','$_POST[pcage]','$_POST[rs]','$_POST[ra]','$_POST[ags]')";

if (!mysql_query($sql,$mysql))
{

die('Error: ' . mysql_error());

}
header("location:ftadmin.php");
echo "1 record added";

mysql_close($mysql)






?>
Reply With Quote
  #4 (permalink)  
Old 2008-04-14, 08:27 PM
Junior Member
 
Join Date: Apr 2008
Location: Cheltenham, UK
Posts: 11
hoopyfrood is on a distinguished road
Default what about this?

See my reply at: insert multple records in a table at one time!

Hope it helps.

Let me know.

hoopyfrood
__________________
Love your documents!

DocuView - The best Web 2.0 document management system (DMS) - utilising the latest technology to help your company achieve ISO 9001 certification.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.0 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.0 (a popular PHP IDE) and NuCoder 1.4 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:43 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.