How to insert data as array values into MySQL?


Problem :

job_no is primaary key

i want save description as array, when i insert many description to one job in database, last description only saved in saved database i can't see other descriptions.

var_dump($description): ??

html: ??

php:

<?php
include ("connection.php");

$sel_type = $_POST["type"];
$ref_no = $_POST["refno"];
$in_date = date("Y-m-d", strtotime($_POST['date']));
$inv_to = $_POST["to"];
$inv_attn  = $_POST["attn"];
$job_no = $_POST["jobno"];
$subject = $_POST["subject"];
$description = $_POST["descrip"];


if(!$_POST["submit"]){
    echo "please fill out";
    header('Location: index.php');
    }
    else{
        if($description){
            foreach($description as $c){
$sql = "INSERT INTO student(sel_type,ref_no,in_date,inv_to,inv_attn,job_no,subject,description) 
VALUES('$sel_type','$ref_no','$in_date','$inv_to','$inv_attn','$job_no','$subject','$c')";
        }}
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}
?>


Solution :

You are creating insert SQL inside foreach loop. But the execution of the insert SQL is happening outside the foreach loop. So you need to move query execution function call inside foreach loop.

foreach($description as $c){
$sql = "INSERT INTO  student(sel_type,ref_no,in_date,inv_to,inv_attn,job_no,subject,description) 
VALUES('$sel_type','$ref_no','$in_date','$inv_to','$inv_attn','$job_no','$subject','$c')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

}

However keep in mind, executing database operation inside a loop is strictly no-no. It hampers performance. You can try with mapping table or multi-insert queries.


Mysql Tutorials

Mysql Howto..

how to merge same value rows in mysql

How to get a new EC2 instance to mount an existing volume on which there is a MySQL database?

MYSQL How to show most popular items in the last two days

How to select maximum two values using LIMIT in MySQL?

How to know where data comes from in a UNION mysql query

How to put multiple updates in a trigger?

How to Monitor MYSQL changes

How to trigger PHP from Mysql Stored Procedure?

how to update a column in a mysql table using sqlachemy?

How to add “error codes” to result?

How can I recover the fike base from this ID? Data Recovery for MySQL

How to use a set of data to generate date list between two date for each ID in mysql

MySQL How to apply WHERE clause to all selected columns except one?

How to connect MySQL database through two SSH hosts

How to store sequential data in mysql

mysql run query but dont show results

How can I compare this datetime from the MySQL database with now?

How to find the file causing a mysql syntax error

how to put condition check each group by value in mysql

How to connect MYSQL database From a Admin system using Eclipse Juno?

ElasticSearch - how to get the auto generated id from an insert query

How to query without doing N+1 query in MySQL

how to implement the an effective search algorithm when using php and a mysql database?

How to permanetly fix a mysql error: Data truncated for column with an alter table statement?

How to make a select statement PDO and end up with $row['field']

Need advice on how to optimize this database further

How to integrate mysql with Zend framework?

How to use Django's index_together for query both with filter and order_by?

how to use “IN” in node-mysql

How do I use MySQL Join to sort forum threads by their last reply?