I cannot insert the student id to table. Would you mind give me some recommentation,please!

<body>
<form id="form1" name="form1" method="post" action="">
  <?php

/* This page receives and handles the data generated by "form.html". */

// $_POST["FirstName"] is an array!!
$total = count($_POST["checkbox"]);
// print "Number of FirstName(s) submitted: " . total;
$eca_id = $_POST["eca_id"];


for ($i=0; $i<$total; $i++) {
if(isset($_POST["Submit"])){
$fruit=isset($_POST["checkbox"]["0"])?$_POST["checkbox"]["0"]:"";
  if(isset($_POST["checkbox"]["$i"]))
  {
  if(!empty($fruit))
  $fruit.=", ";
  $fruit.=$_POST["checkbox"]["$i"];
 
  }
 } 
   // Note: The FirstNames are stored as 
   //       $_POST["FirstName"]["0"],  $_POST["FirstName"]["1"],  etc
   
   print "ECA_ID: " . $_POST["eca_id"]["$i"]. ".<BR>\n";  
   print "ID: " . $_POST["checkbox"]["$i"] . ".<BR>\n"; 
   
   $insertSQL = sprintf("INSERT INTO enrollment (eca_id,id) VALUES ('%s','%s')",
                       $eca_id[$i],
                       $fruit[$i]);
  mysql_select_db($database_connection, $connection);
  $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());

}
?>
</form>

Recommended Answers

All 3 Replies

Hi,

I had review your code.you need to do some changes

$fruit.=$_POST["checkbox"]["$i"];

See in this variable, you used this type of syntax everywhere in your code. let me know you if you use php variable in array dimension then you cant use "" there if its simple number lik "0" then its ok but for dynamic php variable you cant use it.

So now your variable will come like
$fruit.=$_POST["checkbox"][$i];

And please remove the "" from every $i and try again.

Best luck...

Hi,

I had review your code.you need to do some changes

$fruit.=$_POST["checkbox"]["$i"];

See in this variable, you used this type of syntax everywhere in your code. let me know you if you use php variable in array dimension then you cant use "" there if its simple number lik "0" then its ok but for dynamic php variable you cant use it.

So now your variable will come like
$fruit.=$_POST["checkbox"][$i];

And please remove the "" from every $i and try again.

Best luck...

It can insert 3 record but they are same
id eca_id
8 1
8 1
8 1

hi,

You need to do $eca_id = $_POST["checkboxname"]; your checkbox name will come here either its "checkbox" or "eca_id".

As per your output , it seems that everytime value 1 is comes in $eca_id[$i] and value 8 is comes in $fruit[$i].

Which you need to check, there is something wrong in data which are comes after post.

use $_POST["eca_id"]["$i"] instead of $eca_id[$i] and use $_POST["checkbox"]["$i"] instead of $fruit[$i] in your query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.