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 Display Modes
  #1 (permalink)  
Old 2008-04-14, 07:46 PM
Junior Member
 
Join Date: Apr 2008
Posts: 7
flforlife is on a distinguished road
Default 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
Sponsored Links
  #2 (permalink)  
Old 2008-04-14, 08:25 PM
Junior Member
 
Join Date: Apr 2008
Location: Cheltenham, UK
Posts: 12
hoopyfrood is on a distinguished road
Default there's a way!

Hi,

If I got it right you want to enter each entered value into db. This could get ugly, but it might be easier than you think.

First of all: if you have two inputs of the same name (although the ID might differ), unless you store the values in an array PHP will only work with the last one submitted (e.g.:
Code:
<input class="" type="text" name="place" id="place" value="" /> (say you enter "5"
...
<input class="" type="text" name="place" id="place" value="" /> (and here "1")
First of all: clash of IDs, page wouldn't validate. And if you get PHP to print the value of $_POST['place'] you'd get "1", not "5, 1".

Fix:
Code:
<input class="" type="text" name="place[]" id="ditch this" value="" /> (say you enter "5"
...
<input class="" type="text" name="place[]" id="ditch this" value="" /> (and here "1")
Once submitted use something like
PHP Code:
foreach($_POST['place'] as $place => $value)
{
  
// insert into db here
}

or

for(
$i=0$i<count($_POST['place']); ++$i)
{
  
// do your operations here
}

or 
finally while() 
Hope that puts you on track, I've done this for my CRM in the past (multiple telephone numbers of different type (mobile, landline, work...) for each contact using a bit of a mixture of javascript and PHP to dynamically extend the form for more/less records and insert into DB.

If you get stuck (or it isn't clear enough) please give us a shout again.

Cheers,
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-15, 06:16 AM
Junior Member
 
Join Date: Apr 2008
Posts: 7
flforlife is on a distinguished road
Default Insert multple records in a table

ok I have it set up this way but i'm stuck!!!

input form code.....
<tr>
<td><input name="entry[0][place]" type="text" /></td>
<td><input name="entry[0][team]" type="text" /></td>
<td><input name="entry[0][wl]" type="text" /></td>
<td><input name="entry[0][gb]" type="text" /></td>
<td><input name="entry[0][pcage]" type="text" /></td>
<td><input name="entry[0][rs]" type="text" /></td>
<td><input name="entry[0][ra]" type="text" /></td>
<td><input name="entry[0][ags]" type="text" /></td>
</tr>

<tr>
<td><input name="entry[1][place]" type="text" /></td>
<td><input name="entry[1][team]" type="text" /></td>
<td><input name="entry[1][wl]" type="text" /></td>
<td><input name="entry[1][gb]" type="text" /></td>
<td><input name="entry[1][pcage]" type="text" /></td>
<td><input name="entry[1][rs]" type="text" /></td>
<td><input name="entry[1][ra]" type="text" /></td>
<td><input name="entry[1][ags]" type="text" /></td>
</tr>


add.php code........

foreach($_POST['sql']){

sql = $_POST["entry"][0] = array("place"=>"...", "team"=>"...");

sql = $_POST["entry"][1] = array("place"=>"...", "team"=>"...");
$result = mysql_query($sql);
}


I get an error message!!!
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
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 12:08 PM.


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.