簡體   English   中英

如何從一個表中獲取值並在另一表中插入相同的值

[英]How to fetch values from one table and insert the same values in other table

我在同一頁面中使用了兩個按鈕“獲取”和“提交”。 當我單擊“獲取”按鈕時,我會從一個表 [ticketgeneration] 中獲取值並顯示在表單中,並且獲取工作正常。 當我嘗試將提取記錄插入另一個表時[詳細],記錄沒有插入。

HTML PART
<div class="col-sm-3">
             <center><input type="submit" name="fetch" id="fetch" class="btn btn-primary" value="Fetch Records" style="padding: 4px 10px 4px 10px;float:right;"></center>
        </div>
         <div class="col-sm-3">
                <center><input type="submit" name="submit" id="submit" class="btn btn-primary" value="Submit" style="padding: 4px 10px 4px 10px;float:left;"></center>
        </div>
PHP PART
if (isset($_POST['fetch'])) {
       $ticketid1 = $_POST['tid'];
        $sel = "select * from ticketgeneration where ticket_id='$ticketid1'";
        $res = mysqli_query($conn,$sel);
        $row = mysqli_fetch_array($res);
        
    }

上面的代碼從“ticketgeneration”表中獲取值並顯示在文本框中

<input type="text" class="form-control" name="custname" id="custname" value="<?php echo $row['customer_name'];?>">

當我嘗試將獲取的值插入到其他表 [詳細信息] 時,它沒有插入。

if(isset($_POST['submit'])) {
        $ticketgendate = $_POST['date'];
       $ticketid = $row['ticket_id'];
        $customername = $row['customer_name'];
...
$ins = "insert into details (date,ticket_id,customer_name,...)values (('$ticketgendate','$ticketid','$customername',...)";
$res = mysqli_query($conn,$ins);

我在某處缺少條件...

從一個表插入到另一個表的任何數據都使用此查詢。

$query = 'insert into table_name (col1, col2, columnList....) select col1, col2, columnList... from table_name where ticket_id = $id';

您可以在 select 語句中添加其他列,只需在插入查詢語句中提及列名即可。

謝謝!!!

您需要使用以下查詢在您的案例中插入數據。 僅使用 $ticketid1 插入數據。

$ins = "insert into details (date,ticket_id,customer_name,...) select date,ticket_id,customer_name,... from ticketgeneration where ticket_id='$ticketid1'";

然后運行您的查詢,它將直接插入選定的數據,而無需執行額外的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM