从表单中获取数据以将表a中的数据id插入表b中的数据id

时间:2017-12-28 14:44:53

标签: php mysql pdo

我是新的@php PDO mysql并尝试使用前端创建我的旧访问数据库到mysql。

我的表格如下:

root@i:~# docker pull container-registry.oracle.com/kubernetes/kube-apiserver-amd64
Using default tag: latest
Pulling repository container-registry.oracle.com/kubernetes/kube-apiserver-amd64
Error: image kubernetes/kube-apiserver-amd64:latest not found

我需要用户从选项框中选择表'a'中的一些数据,这样我们就可以告诉脚本使用表'a'中的对应ID到表'b'中的ID(例如)设备的序列号。

这是我已有的代码:

table a: devices_type.
table b: devices. 

我试图把一个像

这样的separed sql查询
<?php

if (isset($_POST['submit']))
{

    require "../config.php";
    require "../common.php";

    try 
    {
        $connection = new PDO($dsn, $username, $password, $options);

        $nieuwe_device = array(
            "devices_serial" => $_POST['devices_serial'],
            "devices_date"  => $_POST['devices_date'],
            "devices_type_id" => $_POST['devices_type_id'],
        );

        $sql = sprintf(
                "INSERT INTO %s (%s) values (%s)",
                "devices", 
                implode(", ", array_keys($nieuwe_device)),
                ":" . implode(", :", array_keys($nieuwe_device))
        );

        $statement = $connection->prepare($sql);
        $statement->execute($nieuwe_device);
    }

    catch(PDOException $error) 
    {
        echo $sql . "<br>" . $error->getMessage();
    }

}
?>

<?php require "templates/header.php"; ?>

<?php 
if (isset($_POST['submit']) && $statement) 
{ ?>
    <blockquote><?php echo $_POST['devices_serial']; ?> is succesvol toegevoegd.</blockquote>
<?php 
} ?>

<h2>Nieuwe Devices</h2>

<form method="post">
    <label for="devices_serial">Serienummer</label>
    <input type="text" name="devices_serial" id="devices_serial">
    <label for="devices_date">Datum</label>
    <input type="date" name="devices_date" id="devices_date">
<select>
        <option value= name="devices_type_id" id="devices_type_id"></option>
</select>
    </br></br><input type="submit" name="submit" value="Submit" class="button">
</form>
<form method="get" action="index.php">
    <button type="submit" class="button">Home</button>
</form>
</br></br>
<?php require "templates/footer.php"; ?>

带有选项框的值。 它没有用,我坚持了一个星期。有谁知道这个诀窍?

1 个答案:

答案 0 :(得分:0)

我也尝试了以下但是在插入时收到错误,字段device_type_id我不是空的:-(

&#13;
&#13;
<?php
 
//Connect to our MySQL database using the PDO extension.
$pdo = new PDO('mysql:host=localhost;dbname=rma', 'root', '');
 
//Our select statement. This will retrieve the data that we want.
$sql = "SELECT devices_type_id, devices_type_name, devices_type_model FROM devices_type";
 
//Prepare the select statement.
$stmt = $pdo->prepare($sql);
 
//Execute the statement.
$stmt->execute();
 
//Retrieve the rows using fetchAll.
$nieuwe_device = $stmt->fetchAll();
 
?>
 
<select>
<?php foreach($nieuwe_device as $nieuwe_device): ?>
    <option value="<?= $nieuwe_device['devices_type_id']; ?>"><?= $nieuwe_device['devices_type_name']; ?> &nbsp; <?= $nieuwe_device['devices_type_model']; ?></option>
<?php endforeach; ?>
</select>
&#13;
&#13;
&#13;