2017-12-28 7 views
0

저는 새로운 @PP PDO mysql이며 프론트 엔드가있는 mysql에 이전 액세스 db를 만들려고합니다.테이블에서 데이터 ID를 테이블 b의 데이터 ID에 삽입하기 위해 양식에서 데이터를 가져옵니다.

내 테이블은 다음과 같습니다 :

table a: devices_type. 
table b: devices. 

내가 'A'그래서 우리는 테이블에서 맞습니다 ID를 사용하는 스크립트를 말할 수있는 테이블에서 일부 데이터 옵션 상자에서 선택하는 사용자가 필요를 'A' .

<?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 쿼리 리터를 넣어 시도 : 함께 장치의 예를 들어 일련 번호 테이블 'B'(의 ID에

이 이미 가지고있는 코드 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?> 

옵션 상자의 값입니다. 작동하지 않습니다. 일주일 동안 붙어 있습니다. 누구든지 이것에 대한 속임수를 알고 있습니까?

+0

내가 두 번째 DB 연결 엉 된 시도 다른 것은하지 device_type_id 삽입에 오류를 받았다 값을 선택할 수는 있지만 insert_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()'로 큐어를 빌드하지 마십시오. –

+0

안녕하세요 알렉스, 답변을 주셔서 감사합니다 내가 만약 내가 오순절 솔루션을 찾을 수있는 C로보세요 @ 걸릴 것입니다. –

답변

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>

관련 문제