Skip to main content
deleted 10 characters in body; edited tags
Source Link
Mm-Art-In
  • 23.1k
  • 13
  • 81
  • 144

I read many tutotutorials 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

Unfortunately, I still have errors in my query.
Could

Could you please help me?
To

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

 :

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

    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.

    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.

    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.

I read many tuto 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 DB 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.

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.

deleted 10 characters in body
Source Link
Alok Patel
  • 8.1k
  • 6
  • 35
  • 48



I I read many tuto 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 DB 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!



I read many tuto 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 DB 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!

I read many tuto 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 DB 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!

Source Link
RemiC
  • 67
  • 13

INSERT INTO array with MySQL and PDO



I read many tuto 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 DB 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!