2016-07-06 36 views
-1

我在下面的PHP代碼檢索「formID」在這裏使用它在其他SQL查詢如何訪問SQL查詢的結果,並且在PHP

<?php 

     header('Content-type=application/json;charset=utf-8'); 

     include("connection.php"); 
     session_start(); 

     if($_SERVER["REQUEST_METHOD"] == "POST") { 

     $event_date = mysqli_real_escape_string($con,$_POST['event_date']); 

     $event_location = mysqli_real_escape_string($con,$_POST['event_location']); 

     $organisation_name= mysqli_real_escape_string($con,$_POST['organisation_name']); 




     $query = "SELECT * FROM feedbackform_db WHERE event_date = '$event_date' and event_location = '$event_location' and organisation_name = '$organisation_name'"; 

     $response=mysqli_query($con,$query); 
     $rows = mysqli_num_rows($response); 


     if($rows == 0) { 
      $data['welcome'] = "unsucessful"; 
     } 
     else { 
      $row = mysqli_fetch_row($response); 
      $array = array(
       array(

        "formID"=>$row[0], 


       ) 
      ); 
      $data['welcome'] = "successful"; 
      $data['details'] = $array; 
      $data['success'] = 1; 
      $data['message']="successful"; 
      } 
      echo json_encode($data); 

    } 
    mysqli_close($con); 

    ?> 

我想運行在同一個PHP多了一個INSERT SQL查詢代碼,我想在其他數據庫表中插入相同的formID 我該怎麼做?

+2

嘗試想到的第一件事。我敢打賭它會起作用。 – Solarflare

+0

我的回答對你有幫助嗎? – Mcsky

回答

0

要在SELECT查詢後運行。

$insertStr = "INSERT INTO othertable (someCol, rowId) VALUES (1, $row[0])"; 

$insertQry = mysqli_query($con, $insertStr); 

if ($insertQry) { 
    // What to do if the insert was successful 
} 
-1

你應該用這種方法告訴給mysql的迴歸結果陣列,請http://php.net/manual/fr/mysqli-result.fetch-array.php

$array = []; 
while ($row = $result->mysqli_fetch_assoc()) { //Iterate on the rows returned by SQL 
    // $row is an array here 
    // I don't know your database model and relation between your 2 tables 
    // It isn't a good practice doing sql query while iterating 
    // For example with scalar values 
    $formId = $row['formID']; 
    $some = $row['some']; 
    $other = $row['other']; 
    $field = $row['field']; 
    $array[] = $row; 

    // Insert the line 
    $insertQuery = 'INSERT INTO yourothertable(formIdColumnOtherTable, some, other, field) VALUES($formId, $some, $other, $field)'; 
    $statement  = mysqli_prepare($insertQuery); 
    $successInsert = mysqli_stmt_execute($statement); 
} 
mysqli_close($con); 
... 
$data['details'] = $array; 
echo json_encode($data); 
exit; 

爲了更適應答案,請給我你的兩個表定義

我希望它有幫助! :)