/    Sign up×
Community /Pin to ProfileBookmark

Please help! How do I do this ….

Hello,
I want to create a form and insert multiple rows in my mysql database.
All my form processing to date with php has usually inserted just one row so
I am not sure how to do this.

What I want to make is a batch input form.

Lets say my database table contains fields named: data1, data2, data3, data4

In a form with a table I will have:

<tr>
<td><input type=’TEXT’ name=’dat1′ value = “<?php echo $dat1 ?>” size = ’10’ maxlength=’10’/></td></td>
<td><input type=’TEXT’ name=’dat2′ value = “<?php echo $dat2 ?>” size = ’10’ maxlength=’10’/></td></td>
<td><input type=’TEXT’ name=’dat3′ value = “<?php echo $dat3 ?>” size = ’10’ maxlength=’10’/></td></td>
<td><input type=’TEXT’ name=’dat4′ value = “<?php echo $dat4 ?>” size = ’10’ maxlength=’10’/></td></td>
</tr>

NOW this is one row – but I would like TEN rows to be input and after the ten rows another row that shows the
total of these ten rows – The total will also be INSERTED as on extra row in the mysql table.

I prefer to do what I can with php on the server side but I suspect I will have to use javascript as well to
generate the form ? and to calculate the totals ???.

— below is certainly a php problem not js but I leave it in for clarity of
my problem. —-

I want to send the form once with all ten rows and then INSERT them into my table.

To insert them I guess I use:

$sql = “INSERT INTO homes (data1,data2,data3,data4)
VALUES (‘$data1′,$data2′,$data3′,$data4’)”;

mysql_query($sql) or die(“could not execute INSERT query”);

BUT do I put this is a while () Loop ? so that I get all eleven rows inserted ???

I really need some help !

Thanks for any assistance you can give me.
David.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroApr 16.2005 — Well, what you will need to do in php to make this happen, is to have something like[code=php]<form name="theForm" id="theForm" method="post" action="">
<table>
<?php
for($i=0; $i<10; $i++) { //for loop to create ten rows of inputs
?>
<tr>
<td>Row <?php echo $i+1; ?></td>
<td><input type="text" name="dat1[]" value="<?php if(isset($_POST["dat1"][$i])) echo $_POST["dat1"][$i]; ?>" onchange="calc(this.form)"></td>
<td><input type="text" name="dat2[]" value="<?php if(isset($_POST["dat2"][$i])) echo $_POST["dat2"][$i]; ?>" onchange="calc(this.form)"></td>
<td><input type="text" name="dat3[]" value="<?php if(isset($_POST["dat3"][$i])) echo $_POST["dat3"][$i]; ?>" onchange="calc(this.form)"></td>
<td><input type="text" name="dat4[]" value="<?php if(isset($_POST["dat4"][$i])) echo $_POST["dat4"][$i]; ?>" onchange="calc(this.form)"></td>
</tr>
<?php
}
?>
<!-- Creates a row to show the totals -->
<tr>
<td>Total</td>
<td><span id="tot1">0</span></td>
<td><span id="tot2">0</span></td>
<td><span id="tot3">0</span></td>
<td><span id="tot4">0</span></td>
</tr>
</table>
<input type="submit">
</form>[/code]
to generate the table with the ten inputs. Then, you will need the javascript&lt;script type="text/javascript"&gt;
function calc(frm) {
var t1=0; //variables to hold the totals
var t2=0;
var t3=0;
var t4=0;
for(var i=0; i&lt;frm.elements.length; i++) {
switch(frm.elements[i].name) {
case "dat1[]":
if(/^[d]+$/.test(frm.elements[i].value)) t1+=parseInt(frm.elements[i].value); //if the value is an integer, add to the total
break;
case "dat2[]":
if(/^[d]+$/.test(frm.elements[i].value)) t2+=parseInt(frm.elements[i].value);
break;
case "dat3[]":
if(/^[d]+$/.test(frm.elements[i].value)) t3+=parseInt(frm.elements[i].value);
break;
case "dat4[]":
if(/^[d]+$/.test(frm.elements[i].value)) t4+=parseInt(frm.elements[i].value);
break;
}
}
document.getElementById("tot1").firstChild.data=t1; //show the totals
document.getElementById("tot2").firstChild.data=t2;
document.getElementById("tot3").firstChild.data=t3;
document.getElementById("tot4").firstChild.data=t4;
}
&lt;/script&gt;
You'll notice I've put the totals in spans, not form inputs. This is because it is best to perform the totalling on the server-side.[code=php]<?php
if(isset($_POST["dat1"])) {
$t1=0;
$t2=0;
$t3=0;
$t4=0;
$error=0;
for($i=0; $i<10; $i++) {
if(preg_match('/[^d]/', $_POST["dat1"][$i])) $error++; //if the value is not an integer, error
if(preg_match('/[^d]/', $_POST["dat2"][$i])) $error++;
if(preg_match('/[^d]/', $_POST["dat3"][$i])) $error++;
if(preg_match('/[^d]/', $_POST["dat4"][$i])) $error++;
}
if($error==0) { //if no errors, we can insert
for($i=0; $i<10; $i++) {
if(preg_match('/^[s]*$/', $_POST["dat1"][$i])) $_POST["dat1"][$i]=0; //set empty values to 0
if(preg_match('/^[s]*$/', $_POST["dat2"][$i])) $_POST["dat2"][$i]=0;
if(preg_match('/^[s]*$/', $_POST["dat3"][$i])) $_POST["dat3"][$i]=0;
if(preg_match('/^[s]*$/', $_POST["dat4"][$i])) $_POST["dat4"][$i]=0;
$t1+=$_POST["dat1"][$i];
$t2+=$_POST["dat2"][$i];
$t3+=$_POST["dat3"][$i];
$t4+=$_POST["dat4"][$i];
$sql = "INSERT INTO homes (data1,data2,data3,data4) VALUES ('".$_POST["dat1"][$i]."','".$_POST["dat2"][$i]."','".$_POST["dat3"][$i]."','".$_POST["dat4"][$i]."')";
mysql_query($sql) or die("could not execute INSERT query");
}
$sql = "INSERT INTO homes (data1,data2,data3,data4) VALUES ('".$t1."','".$t2."','".$t3."','".$t4."')";
mysql_query($sql) or die("could not execute INSERT query");
} else { //if errors, display message
echo "There were some problems (".$error.") with your submission.<br>";
}
}
?>[/code]
Of course, your validation needs may differ from what I've posted. I've assumed it's ok to leave values empty, but those will be converted to 0 before being inserted. I've also assumed the values are to be integers. You may have to adjust it to suit your needs.
Copy linkTweet thisAlerts:
@phpnoviceApr 16.2005 — I prefer to do what I can with php on the server side but I suspect I will have to use javascript as well to

generate the form ? and to calculate the totals ???.[/QUOTE]

That depends upon where the data comes from in the first place. If it comes from the database then, no, no JavaScript would be required. PHP would build all of the form and data and do the totalling. Otherwise, JavaScript would only be required for client-side toalling (to be re-totalled on the server-side) -- unless you wish to have the ability to dynamically add additional rows to the form without making a trip to the server. That would also require JavaScript.
×

Success!

Help @DaveinLondon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...