2017-12-28 27 views
0

私は新しい@PD PDO mysqlであり、フロントエンドでmysqlに古いアクセスDBを作成しようとしています。フォームからデータを取得してテーブルaのデータIDをテーブルbのデータIDに挿入します

私のテーブルは次のとおりです。

table a: devices_type. 
table b: devices. 

私は「」私たちはテーブルから、対応付けIDを使用するようにスクリプトを伝えることができますオプションボックスからテーブルからいくつかのデータを選択するためにユーザが必要「」 「B」のテーブルからIDに(一緒に、デバイスのシリアル番号を例えば有する

これは私が既に持っているコードである。

<?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"; ?> 

Iはsepared SQLクエリLを置くことを試みましたike

<?php $sql: "select devices_type.devices_type_id, devices_type.devices_type_model from devices_type join devices on devices_type.devices_type_id = devices.devices_type_id?> 

オプションボックスの値です。 それは働いていない、私は1週間これに固執しています。誰かがこれについてのトリックを知っていますか?

+0

私は2番目のDB接続を聖霊降臨祭た試みた別のものではないdevice_type_id挿入時にエラーを受け取っ値は選択可能ですが、私はいつもdevice_type_idが空でないことを挿入にエラーが発生します –

+2

これで、PDOを使用しているので、適切な方法で - [** prepared statements **](https:// secure.php.net/manual/en/pdo.prepare.php)と[** bound parameters **](https://secure.php.net/manual/en/pdostatement)を参照してください。 bindparam.php)。 'sprintf()'でqueiresを構築しないでください。 –

+0

こんにちはアレックス、私は解決策を見つけることができる場合、私はそれを見て@それを取るだろう答えのおかげで、 –

答えて

0

は、私はまた、次のよう試みたが、フィールドは私の空:-(

<?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>

関連する問題