how to insert value in database while field value in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

how to insert value in database while field value in array

Post by manojsemwal1 »

how can i insert two field value in database while field value in array
script are
if(isset($_POST['check']))
{

foreach($_POST['check'] as $key => $val)

{


$sql1="insert into studentmarkstb values('$val','$obtmark','$subjectid')";
echo $sql1;
}
//$rs1 = mysql_query($sql1) or die(mysql_errormsg());



} }

above obtmark value also in array iam attaching a jpg file here in this file iam using Two array field frist Sno in Checkbox array and obtainmarks is also in array field. user can select any one value and submit in the database.
but how can i insert obtmarks value, while it is in array type.

regards,
Attachments
marks.JPG
marks.JPG (20.46 KiB) Viewed 7561 times
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

Any PHPian give some answer...................
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: how to insert value in database while field value in array

Post by manohoo »

foreach works with arrays.
$_POST['check'] is not an array.
$_POST is an array.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

Thanks for your reply

here i know that foreach is for array but here check is array variable u see in the image i used sno and serialno iam using check box ad each checkbox having an studentid and their related marks when user select the student the marks should be insert the database with their id. here obtmarks is also array type variables.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: how to insert value in database while field value in array

Post by manohoo »

Code: Select all

<?php
// Let's say that array $a contains the values you want to insert in the database
$a = array();
 
$a['check'] = "Whatever";
$a['obtmark'] = "65";
$a['subjectid'] = "33";
 
// the sql statement would go like this
$sql = "insert into studentmarkstb values('{$a['check']}','{$a['obtmark']}','{$a['subjectid']}')";
echo $sql; // insert into studentmarkstb values('Whatever','65','33')
?>
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

the above image the course is having n numbers of student and i have to enter the selected students obtainmarks.

while Sno and ObtainMarks value is in Array how we can use both array value insert to the database.
i had tried and use loop but no result...........
pl send some solution.......... :|
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

any phpian is there who can solve my Problem..............
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

manojsemwal1 wrote:any phpian is there who can solve my Problem..............
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: how to insert value in database while field value in array

Post by manohoo »

I use this in one of my scripts, feel free to adapt it to your needs.

Code: Select all

<?php
 
$array = array('a'=> 23, 'n'=>66);
 
$sql = "INSERT INTO myTable (".implode(",", array_keys($array)).")
VALUES ('".implode("','", array_values($array))."')";
 
echo $sql;
 ?> 
Output:
INSERT INTO myTable (a,n) VALUES ('23','66')
You can iterate the above for each record you want to enter.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

i had applied your idea but its working only for one array type value but in my case iam using two array type value frist one when user select the student and enter the marks.
here obtainmarks and Sno is array type value.
when inserting into database obtainmarks and Regdid value should be enter in same query........
see the image
Attachments
entermarks.JPG
entermarks.JPG (45.5 KiB) Viewed 7418 times
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: how to insert value in database while field value in array

Post by manojsemwal1 »

hai manohoo

send some solution iam waiting.......................
Post Reply