Multiple insert in MySQL table from PHP table

elstiv

I am stuck on the following problem. I have two php files: One which displays a list of students with a combo box to mark each student as either 'Present' or 'Absent'and another that receives these postings and inserts the values in a MySql table. I am having a problem posting the values of the array attendance_status[] resulting in an error: "Array to string conversion". I know it might be something elementary that I am missing but cannot find my way out. These are my two files (I know that it is deprecated and will update accordingly):

index.php

?php 
error_reporting( E_ALL );
ini_set('display_errors', 1);


require "config.php";
$con = mysql_connect ( DBSERVER, DBUSER, DBPASS );
mysql_select_db ( DBNAME, $con );


?>


<h1 align="center"><font color="black"><b>ATTENDANCE FOR GRADE1</font></b></h1>
<table id="attendance" border="1"  cellspacing="1" cellpadding="1" >
<tr >
<th>id</th>
<th>name</th>
<th>surname</th>
<th>attendance</th>

</tr>
<?php

   $query = ("SELECT * FROM `b10_18591250_JC`.`STUDENT`");
   $result = mysql_query($query);

while( $row = mysql_fetch_array($result))

{


  echo "<form action=insertattend.php method=POST>";
  echo "<tr>";
  echo "<td>" . "<input name=stid type=number value=" .$row['ID']." </td>";
  echo "<td>" . "<input name=stname type=text value=" .$row['NAME']." </td>";
  echo "<td>" . "<input name=stsurname type=text value=" .$row['SURNAME']." </td>";

echo "<td>";
echo "<select name=attendance_status[] id=attendance_status>";
echo "<option value=1>Present</option>";
echo "<option value=0>Absent</option>";
echo "</select>";
echo "</td>";
echo "</tr>"; 

}

  echo"<input type=submit value=Submit>";
?>
</table>

</form>

Posting in this page called insertattend.php

<?php 
error_reporting( E_ALL );
ini_set('display_errors', 1);

require "config.php";
$con = mysql_connect ( DBSERVER, DBUSER, DBPASS );
mysql_select_db ( DBNAME, $con );

$stid = $_POST["stid"];
$attendance_status = $_POST["attendance_status"];


mysql_query("INSERT INTO ATTENDANCE (ID, STUDENT_ID, ATTENDANCE) VALUES
(NULL, '$stid', '$attendance_status')") or die (mysql_error());

?>
Gulmuhammad Akbari

You are posting an array of attendance_status and you should loop that.

<?php 
 while( $row = mysql_fetch_array($result))

{


  echo "<form action=insertattend.php method=POST>";
  echo "<tr>";
  echo "<td>" . "<input name=stid[] type=number value=" .$row['ID']." </td>";
  echo "<td>" . "<input name=stname type=text value=" .$row['NAME']." </td>";
  echo "<td>" . "<input name=stsurname type=text value=" .$row['SURNAME']." </td>";

echo "<td>";
echo "<select name=attendance_status[] id=attendance_status>";
echo "<option value=1>Present</option>";
echo "<option value=0>Absent</option>";
echo "</select>";
echo "</td>";
echo "</tr>"; 

}
?>

<?php 
error_reporting( E_ALL );
ini_set('display_errors', 1);

require "config.php";
$con = mysql_connect ( DBSERVER, DBUSER, DBPASS );
mysql_select_db ( DBNAME, $con );

$stid = $_POST["stid"];
$attendance_status = $_POST["attendance_status"];

for($i=0;$i<count($attendance_status);$i++){
   mysql_query("INSERT INTO ATTENDANCE (ID, STUDENT_ID, ATTENDANCE) VALUES
   (NULL, $stid[$i], $attendance_status[$i])") or die (mysql_error());
}

?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple insert in MySQL table from PHP table

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

Mysql insert into with multiple id from another table

From Dev

insert multiple rows mysql from another table

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

From Dev

MySQL insert into a table from multiple table filter by id

From Dev

How insert multiple rows in mysql table with php array?

From Dev

Insert data from json to mysql table from php

From Dev

MySQL - Insert table data from another table

From Dev

INSERT INTO MySQL table SELECT FROM PostgreSQL table

From Dev

MySQL - Insert table data from another table

From Dev

MySQL - INSERT INTO from a Temporary Table

From Dev

Insert ID from one table to another in MySQL php

From Dev

php mysql insert into table from text file with fixed width

From Dev

Retrieve data from mysql database and insert it in a table using php

From Dev

looping mysql query to get multiple rows of data from a table and insert into another table

From Dev

How to insert multiple records from one table into another on update of third table in MySQL

From Dev

Multiple mysql table joins with php?

From Dev

mysql - SELECT AND INSERT QUERY into multiple table with conditions

From Dev

Insert data into multiple table using PHP

From Dev

insert data and multiple images in a table [PHP]

From Dev

Adding total values from multiple table in MySQL with php

From Dev

PHP/MySQL - Get multiple fields from one entry in a table

From Dev

Adding total values from multiple table in MySQL with php

From Dev

PHP + MySQL - one table holding reference to IDs from multiple tables

From Dev

Proper way to insert data into table with php and mysql?

From Dev

Issue trying to insert data into a MySQL table with php

From Dev

MySQL/PHP check if data is in database table, if not insert it

Related Related

  1. 1

    Multiple insert in MySQL table from PHP table

  2. 2

    Insert multiple rows into a MySQL database from a table

  3. 3

    Mysql insert into with multiple id from another table

  4. 4

    insert multiple rows mysql from another table

  5. 5

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  6. 6

    Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL

  7. 7

    MySQL insert into a table from multiple table filter by id

  8. 8

    How insert multiple rows in mysql table with php array?

  9. 9

    Insert data from json to mysql table from php

  10. 10

    MySQL - Insert table data from another table

  11. 11

    INSERT INTO MySQL table SELECT FROM PostgreSQL table

  12. 12

    MySQL - Insert table data from another table

  13. 13

    MySQL - INSERT INTO from a Temporary Table

  14. 14

    Insert ID from one table to another in MySQL php

  15. 15

    php mysql insert into table from text file with fixed width

  16. 16

    Retrieve data from mysql database and insert it in a table using php

  17. 17

    looping mysql query to get multiple rows of data from a table and insert into another table

  18. 18

    How to insert multiple records from one table into another on update of third table in MySQL

  19. 19

    Multiple mysql table joins with php?

  20. 20

    mysql - SELECT AND INSERT QUERY into multiple table with conditions

  21. 21

    Insert data into multiple table using PHP

  22. 22

    insert data and multiple images in a table [PHP]

  23. 23

    Adding total values from multiple table in MySQL with php

  24. 24

    PHP/MySQL - Get multiple fields from one entry in a table

  25. 25

    Adding total values from multiple table in MySQL with php

  26. 26

    PHP + MySQL - one table holding reference to IDs from multiple tables

  27. 27

    Proper way to insert data into table with php and mysql?

  28. 28

    Issue trying to insert data into a MySQL table with php

  29. 29

    MySQL/PHP check if data is in database table, if not insert it

HotTag

Archive