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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分