Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,193 members, 7,953,694 topics. Date: Thursday, 19 September 2024 at 11:40 PM

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

Nairaland Forum / Science/Technology / Programming / Assist Me Wit Php Code To Insert Multiple Records In A Table (897 Views)

My Php Code Does Not Execute But Displays Code On Browser, / PHP Code To Insert And Retrieve Images From Mysql Server / Pls Help Me With This Php Code (2) (3) (4)

(1) (Reply) (Go Down)

Assist Me Wit Php Code To Insert Multiple Records In A Table by dandylass: 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?

1 Like

Re: Assist Me Wit Php Code To Insert Multiple Records In A Table by dandylass: 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: 8:56pm On Jul 02, 2015
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: 12:15am On Jul 03, 2015
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?*/
$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;
}


/* 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: 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)

Check My Tv App That i Develop Using My Android Phone / Who Need Android App For Kids Learning / D.S.A Design And Relational DB Questions

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.