2016-07-06 34 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; 

为了更适应答案,请给我你的两个表定义

我希望它有帮助! :)