donglang1894 2016-07-07 10:53
浏览 93
已采纳

如何使用外键将PHP表单中的数据插入到MySQL数据库中?


I am dealing with a PHP form containing checkboxes and a MySQL db. I finally achieved to insert multiple rows, one for each selected item, by looping over the array.
But now, I have to face another issue: in my DB, I have one principal table to store the one-choice questions and another table to store the answers from the checkboxes.
I would like to first execute the query inserting the one-choice answers into the principal table (one row per form), so that it generates a serial ID.
And secondly, to get back this ID and associate it to every row inserted into the checkbox table in order to link the two tables though this ID.
Is that possible please and how should I do?

Here the HTML code:

<input type="checkbox" name="nature_contact[]" value="1"><label >Phone</label><br/>
<input type="checkbox" name="nature_contact[]" value="2"><label >Mail</label><br/>
<input type="checkbox" name="nature_contact[]" value="3"><label >Visit</label><br/>
<input type="checkbox" name="nature_contact[]" value="4"><label >Unk</label>    <br/><br/> 
<input type="text" name="coord"/>
            <br/>
<input type="text" name="tel"/>
            <br/><br/>              
<input type="submit" name="add" value="SEND"/>

And here the PHP part:

try {
    if(isset($_POST['add'])){   
        if(isset($_POST['coord'])) {
            $coord=$_POST['coord'];
        }
        else { $coord = '';
        }
        if(isset($_POST['tel'])) {
            $tel=$_POST['tel'];
        }
        else { $tel = '';
        }           
    $query="INSERT INTO nmp_mfs.general (coord, tel) VALUES ('".$coord."', '".$tel."')";
    $statement_gnl = $pdo->prepare($query); 
    $statement_gnl->execute();  
    }
}   
catch(PDOException $e) {
    $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
    die($msg);
}

try {
    if(isset($_POST['add'])){   
        if(isset($_POST['nature_contact'])) {
            $sql = "INSERT INTO nmp_mfs.t_temporaire (nature_contact) VALUES ".rtrim(str_repeat('(?),', count($_POST["nature_contact"])), ',');
            $statement = $pdo->prepare($sql);
            $count = 1;
            foreach($_POST["nature_contact"] as $nature_contact) {
                $statement->bindValue($count++, $nature_contact);
            }
            $statement->execute();
        }
    }   
}   
catch(PDOException $e) {
    $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
    die($msg);
}
  • 写回答

2条回答 默认 最新

  • dtr87341 2016-07-07 12:41
    关注

    Yes this is possible.

    You need the last inserted id of the principal table row like:

    $lastInsertedID = $db->lastInsertId();
    

    1.) Insert the question in the database table (principal)

    2.) Get the last inserted id ($lastInsertedID)

    3.) Insert answers related to the question in the answer table and provide the last inserted id.

    $query = "INSERT INTO nmp_mfs.t_temporaire (questionID, nature_contact) 
    VALUES ($lastInsertedID, $nature_contact)"; // Example
    

    4.) Select the ID's of your questions.

    5.) Get the corresponding answers:

    $query = "SELECT awnsers WHERE question_id = questionID"; // Simple example
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧