【问题标题】:Insert into database with a select and an URL ID使用选择和 URL ID 插入数据库
【发布时间】:2018-02-10 21:10:12
【问题描述】:

当我尝试在具有多个外键的表上插入数据时。此表单在导航栏上有一个 ID。

所以我创建了一个表单,在其中从不同的表(链接到我要插入值的表)中选择值,并在同一页面中执行操作:

<form method="post" action="backend_valuechain.php?id=<?php echo $_GET['id'] ?> 
    <label>Nom du segment</label>
    <input type="text" class="form-control input-lg" id="segmentname"/> 

    <label>My Select Name</label>
    <select id="select1">
       <?php 
           foreach ($pdo->query($typosegment = 'SELECT ID_SEGMENT_VC_TYPO, SEGMENT_VC_TYPOLOGY FROM segment_vc_typo;') as $row) {
                 echo '<option  name="' . $row['SEGMENT_VC_TYPOLOGY'] . '" value="' .$row['ID_SEGMENT_VC_TYPO'] . '"> ' . $row['SEGMENT_VC_TYPOLOGY'] . '</option>'; } ?>
   </select>
......

我还有其他几个选择和输入文本,但为了让我的问题更易于阅读,我只是抽取了一个示例。创建表单后,我计划将数据插入数据库。

<?php
    try{
        $vcNewSegment = "INSERT INTO segment_vc (ID_VC,SEGMENT_VC_NAME,ID_SEGMENT_VC_TYPO) VALUES (:id,:segmentname,:typosegment)";

        $stmt = $pdo->prepare($vcNewSegment);
        $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
        $stmt->bindParam(':segmentname', $_REQUEST['segmentname'], PDO::PARAM_STR);
        $stmt->bindParam(':typosegment', $_REQUEST['typosegment'], PDO::PARAM_INT);
        $stmt->execute();
        echo "OK !";
    }
    catch(PDOException $e){
        die("ERROR: Could not able to execute $vcNewSegment. " .
            $e->getMessage());
    } 
    unset($pdo);
?>

当我通过表单插入数据时,我在表中看不到输入,但在此表中插入了“NULL”值(我删除了一些不相关的列):

CREATE TABLE `segment_vc` (
  `ID_SEGMENT_VC` int(20) NOT NULL,
  `ID_VC` int(20) DEFAULT NULL,
  `ID_SEGMENT_VC_TYPO` int(20) DEFAULT NULL,
  `SEGMENT_VC_NAME` varchar(90) DEFAULT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ID_SEGMENT_VC 是主键 ID_VC 是一个外键(它出现在 URL 中).. ID_SEGMENT_TYPO 也是外键

【问题讨论】:

    标签: php mysql sql pdo prepared-statement


    【解决方案1】:

    我不确定$_REQUEST['typosegment'] 来自哪里。

    &lt;select id="select1"&gt; 的值会被$_POST['select1'] 获取。

    【讨论】:

    • 看来Davids的$_REQUEST['typosegment']是你的$_POST['select1'] ;-) 因为每个select选项的值都引用了“ID_SEGMENT_VC_TYPO”字段,在backend_valuechain.php的insert语句中也用到了这个字段。跨度>
    • 它是对的:typosegment 是 select1...在 POST 中更改 REQUEST 不会改变我的问题...实际上我不想插入我在选择选项中输入的文本,但是我在选项中的值: ..在这种情况下,我想在表中插入“1”作为整数...
    猜你喜欢
    • 2013-12-25
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多