₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,282 members, 8,421,175 topics. Date: Friday, 05 June 2026 at 09:58 PM

Toggle theme

Assist Me Wit Php Code To Insert Multiple Records In A Table - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingAssist Me Wit Php Code To Insert Multiple Records In A Table (1006 Views)

1 Reply (Go Down)

Assist Me Wit Php Code To Insert Multiple Records In A Table by dandylass(op): 8:51am On Jul 02, 2015
Please can any one assist me wit d php codes on how to insert multiple records at once in a table?
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by Craigston: 3:15pm On Jul 02, 2015
What have you done so far? Have you attempted it?
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by dandylass(op): 4:27pm On Jul 02, 2015
Yes you can see my code below
when submitted it will write failed
if(isset($_POST['submit']))
{
$result = $_POST["grades"];
$total = $ca1+$ca2+$exam;
if ($total <= $gpa6){
$grade4 = $grad1;
$letter = $letter1;
}
elseif ($total <=$gpa7){
$grade4 = $grad2;
$letter = $letter2;
}
elseif ($total <=$gpa8){
$grade4 = $grad3;
$letter = $letter3;
}
elseif ($total <=$gpa9){
$grade4 = $grad4;
$letter = $letter4;
}
elseif ($total <=$gpa10){
$grade4 = $grad5;
$letter = $letter5;
}
if (count($result > 0))
{
$new = array();
foreach ($result as $key => $value)
{
$new[] = "('". $regno . "', '" . $name . "', '" . $sex. "', '" . $admissionyr."', '" . $value["term"] . "', '" . $value["session"] . "', '" . $value["subject"] . "', '" . $value["ca1"] . "', '" . $value["ca2"] . "', '" . $value["exam"] . "', '" . $value["total"] . "', '" . $value["grade"] . "', '" . $value["remark"] . "')";
}
if (count($new) > 0)
{

$checkdepart = mysql_query("select * from yrone where regno = '$regno' and subject = '$subject' and term ='$term'"wink or die ('could not select from registeration'.mysql_error());
$depart = mysql_num_rows($checkdepart);
while($row = mysql_fetch_assoc($checkdepart))
{
$c = $row['regno'];
}

if($depart == 0){
$query = mysql_query("INSERT INTO yrone (regno,name,admissionyr,class,term,session,subject,ca_test,mid_termtest,exam,total,grade, remark, now()) VALUES '.implode(',', $new)'"wink;
if ($query)
{
echo "SUCCESS";
}
else
{
echo "FAILED";
}
}
}
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by Nobody: 5:09pm On Jul 02, 2015
first of all,with mysql functions, you are seriously opened to SQL injection(s)...try switching to mysqli or pdo....



there are multiple ways to do...
1.) You could use the multi_query function...

2.) you could run a foreach loop with the connection parameter outside the loop... you insert the SQL in the loop....
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by babatope88(m): 8:25pm On Jul 02, 2015
OP
Check this line where you have

if (count($result > 0));



I think, it should be like this



if (count($result) > 0);
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by Nobody:
dandylass:
Please can any one assist me wit d php codes on how to insert multiple records at once in a table?
what is in the row column and values.
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by Craigston:
There are several errors which we have to fix for your code to work. Let's see how this goes. Btw, which book are you learning with?

dandylass:
Yes you can see my code below
When submitted it will write failed
<?php
if(isset($_POST['submit']))
{
$result = $_POST["grades"]; /* is $_POST["grades"] receiving an array from the form?*/
[color=#563445]$total = $ca1+$ca2+$exam;
if ($total <= $gpa6){
$grade4 = $grad1;
$letter = $letter1;
}
elseif ($total <=$gpa7){
$grade4 = $grad2;
$letter = $letter2;
}
elseif ($total <=$gpa8){
$grade4 = $grad3;
$letter = $letter3;
}
elseif ($total <=$gpa9){
$grade4 = $grad4;
$letter = $letter4;
}
elseif ($total <=$gpa10){
$grade4 = $grad5;
$letter = $letter5;
}[/color]

/* You assigned your variables to variables that have not been previously initialized. For example,
$grade4 = $grad5;
Fix these first.*/

if (count($result > 0))
{
$new = array();
foreach ($result as $key => $value)
{
$new[] = "('". $regno . "', '" . $name . "', '" . $sex. "', '" . $admissionyr."', '" . $value["term"] . "', '" . $value["session"] . "', '" . $value["subject"] . "', '" . $value["ca1"] . "', '" . $value["ca2"] . "', '" . $value["exam"] . "', '" . $value["total"] . "', '" . $value["grade"] . "', '" . $value["remark"] . "')";
}
if (count($new) > 0)
{

$checkdepart = mysql_query("select * from yrone where regno = '$regno' and subject = '$subject' and term ='$term'"wink or die ('could not select from registeration'.mysql_error());
$depart = mysql_num_rows($checkdepart);
while($row = mysql_fetch_assoc($checkdepart))
{
$c = $row['regno'];
}

if($depart == 0){
$query = mysql_query("INSERT INTO yrone (regno,name,admissionyr,class,term,session,subject,ca_test,mid_termtest,exam,total,grade, remark, now()) VALUES '.implode(',', $new)'"wink;
if ($query)
{
echo "SUCCESS";
}
else
{
echo "FAILED";
}
}
}
Ouch! Many errors. Brb. Fix the undefined values first.
Wait, I hope you're not using register_globals? That's the only way those variables could be assigned uninitialized variables and it's very insecure.
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by Craigston: 12:45am On Jul 03, 2015
Can we know the values received in the $_POST array? Maybe seeing the html form can help.
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by guente02(m): 3:37am On Jul 03, 2015
Choiiii....I'll definitely get past this stage in my infant coding stage.
Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by dandylass(op): 7:18am On Jul 03, 2015
Ok to make evrything easier to people dt want to assist me i will post html form wit d code. tanx
1 Reply

My Php Code Does Not Execute But Displays Code On Browser,PHP Code To Insert And Retrieve Images From Mysql ServerPls Help Me With This Php Code234

............I Need Someone To Teach Me Python & Django **For a Fee**I Need A Programmer That Can Create Live Video Chat