INSERT INTO array with MySQL and PDO

RemiC

I read many tutorials to understand how inserting multiple lines in one query since I have to deal with multiple choices question in a PHP form (v5.4).

Unfortunately, I still have errors in my query.

Could you please help me?

To be more precise about the purpose, I made a form and I created a Database in MySQL with:

  • one table (A) to store all single values from radio button and text questions with a SERIAL id.

  • a table for each multiple choices question, in which I listed all possible answers in one column, and an ID code in another col.

  • and an "intermediate" table for each multiple choices question, to store the same id than the one created automatically during insertion in table A and the ID from the values selected in the checkbox question
    (so, for one form filled, I expect to get one row in table A, and as much rows as the selected values in each corresponding "intermediate" table.

My PHP code (which refers only to one multiple choices question as a test):

try {
    $pdo = new PDO('mysql:host=myserver_url;dbname=my_db', 'my_user','my_pass');
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}
catch(PDOException $e) {
    echo 'Fail to connect';
    exit();
}

try {
    if(isset($_POST['add'])){   
        if(isset($_POST['checkbox_name'])) {
            $values = '('.implode('),(', $_POST['checkbox_name']).')';
            $sql =$pdo->exec("INSERT INTO my_db.my_table (field1)
                            VALUES ($values) ");
        }
    }
}   
catch(PDOException $e) {
    $msg = 'ERROR PDO in ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
    die($msg);
}

HTML code:

<body>      
    <form name="form_1" method="post" action="form_1.php">
        <input type="checkbox" name="checkbox_name[]" value="1"><label >Telephone</label><br/>
            <input type="checkbox" name="checkbox_name[]" value="2"><label >Mail</label><br/>
            <input type="checkbox" name="checkbox_name[]" value="3"><label >Other</label><br/>
            <br/>
            <br/>           
            <input type="submit" name="add" value="SEND"/>
    </form>

Thanks a lot for your help!

RemiC

I finally solved my problem. For those interested in the solution, here it is (with a "get the last id bonus"):

try {
    if(isset($_POST['add'])){   
        if(isset($_POST['nature_contact'])) {
            $sql = "INSERT INTO db.int_contact (id_g,id_nature_contact) VALUES " .rtrim(str_repeat('('.$lastID.', ?),', count($_POST["nature_contact"])), ',');
            $statement = $pdo->prepare($sql);   
            $count = 1;
            foreach($_POST["nature_contact"] as $nature_contact) {
                $statement->bindValue($count++, $nature_contact);
            }
            $statement->execute();
        }
    }   
}
// and then add a catch exceptions

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting MySql Insert To PDO

From Dev

PDO insert BindParam mysql

From Dev

PDO array INSERT

From Dev

PDO insert array

From Dev

PHP PDO batch insert from multi dimensional array failing to insert into MySQL

From Dev

PHP PDO batch insert from multi dimensional array failing to insert into MySQL

From Dev

MYSQL Insert Where Not Exists with PDO

From Dev

Using mysql insert with implode in PDO

From Dev

Confirm record INSERT with PDO and MySQL

From Dev

Insert cookies and date pdo mysql

From Dev

PDO Insert Error - execute(array());

From Dev

insert element PDO result array

From Dev

array for PDO MySQL function

From Dev

How to tell if PDO MySQL INSERT was successful with IGNORE

From Dev

Insert JSON decoded data into mysql with PDO

From Dev

Can't INSERT into mysql via PDO

From Dev

PDO Insert multiple checkbox values in Mysql

From Dev

PDO Insert operation using PHP and MYSQL not working

From Dev

PHP; PDO; MySQL; INSERT query not taking input

From Dev

PHP; PDO; MySQL INSERT query with drop down

From Dev

Insert form value into mysql database with pdo

From Dev

Unable to insert into table using PDO in mysql

From Dev

Trying to insert pdo bound parameters into mysql database

From Dev

Mysql - conditional insert query with select and PDO

From Dev

PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

From Dev

PDO MYSQL insert statement not working (no error given)

From Dev

PDO insert into MySQL Database not working with another PDO query

From Dev

PDO mysql LIMIT with placeholder in an array

From Dev

PDO insert statement with loop through $_POST array