Page 1 of 1

how to insert value in database while field value in array

Posted: Sat Jan 02, 2010 7:25 am
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,

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

Posted: Sun Jan 03, 2010 10:27 pm
by manojsemwal1
Any PHPian give some answer...................

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

Posted: Sun Jan 03, 2010 10:51 pm
by manohoo
foreach works with arrays.
$_POST['check'] is not an array.
$_POST is an array.

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

Posted: Sun Jan 03, 2010 11:04 pm
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.

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

Posted: Sun Jan 03, 2010 11:52 pm
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')
?>

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

Posted: Tue Jan 05, 2010 4:13 am
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.......... :|

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

Posted: Thu Jan 07, 2010 6:12 am
by manojsemwal1
any phpian is there who can solve my Problem..............

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

Posted: Fri Jan 08, 2010 12:15 am
by manojsemwal1
manojsemwal1 wrote:any phpian is there who can solve my Problem..............

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

Posted: Fri Jan 08, 2010 2:28 am
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.

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

Posted: Wed Jan 13, 2010 1:21 am
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

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

Posted: Thu Jan 14, 2010 7:24 am
by manojsemwal1
hai manohoo

send some solution iam waiting.......................