簡體   English   中英

從PHP中的另一個表(查詢1)插入數據庫表(查詢2)

[英]Insert into db table (query 2) from another table (query 1) in php

嗨,我有一個下拉列表,其中包含一個db表中的值,我想單擊按鈕以將特定ID的行保存在另一張表中,我具有這種形式

<table class="table table-bordered table-responsive">
    <tr>
    <td><label class="control-label">Drop down list</label></td>
    <td class="col-xs-4">
        <?php
            $stmt01 = $DB_con->prepare('SELECT * FROM dbtable WHERE chart in (458,459,461) ORDER BY id ASC');
            $stmt01->execute();
            if($stmt01->rowCount() > 0)
            {
            ?>
                <select class="form-control" name="value01">
                    <?php
                        while($row=$stmt01->fetch(PDO::FETCH_ASSOC))
                        {
                            extract($row);
                            echo '<option value="'.$row['id'].'">'.$row['id'].' '.$row['lastName'].' '.$row['firstName'].'</option>';
                        }
                    ?>
                </select>
            <?php
            }
            ?>
        </td>
        <td colspan="2" align="right" class="col-md-2"><button type="submit" name="btnsave01" class="btn btn-default">
            <span class="glyphicon glyphicon-save"></span> &nbsp; Insert
            </button>
        </td>
    </tr>

而我的PHP是

require_once 'dbconfig.php';
if (isset($_POST['btnsave01']))
{
    if (isset($_POST['value01']))
    {
        $chart = $_GET['chart'];
        $chartDescription = $_GET['chartDescription'];
        $lastName = $_GET['lastName'];
        $firstName = $_GET['firstName'];
        $location = $_GET['location'];
        $empPic = $_GET['empPic'];

        $q01 = $DB_con->prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic) VALUES(:uchart, :uchartDescription, :uregNo, :ulastName, :ufirstName, :ulocation, uempPic)');
        $q01->bindParam(':uchart',$chart);
        $q01->bindParam(':uchartDescription',$chartDescription);
        $q01->bindParam(':uregNo',$regNo);
        $q01->bindParam(':ulastName',$lastName);
        $q01->bindParam(':ufirstName',$firstName);
        $q01->bindParam(':ulocation',$location);
        $q01->bindParam(':uempPic',$empPic);
    }
}

你能幫我解決這個問題嗎? 該按鈕工作正常,但該值未存儲在數據庫表中

謝謝

PHP 2個更正

  1. 缺少:INSERT查詢的VALUES中。 更換uempPic:uempPic

  2. INSERT查詢未執行。 在最后一個bindParam語句之后添加以下行。

     $q01->execute(); 

此行錯誤,請檢查一下...

 $q01 = $DB_con->prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic) VALUES(:uchart, :uchartDescription, :uregNo, :ulastName, :ufirstName, :ulocation, :uempPic)');

請查看您的准備聲明:

$ q01 = $ DB_con-> prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic)VALUES(:uchart,:uchartDescription,:uregNo,:ulastName,:ufirstName,:ulocation,uempPic) ');

您忘了在“ uempPic”中添加“:”,請使用以下語句。

$ q01 = $ DB_con-> prepare('INSERT INTO results01(chart,chartDescription,regNo,lastName,firstName,location,empPic)VALUES(:uchart,:uchartDescription,:uregNo,:ulastName,:ufirstName,:ulocation,:uempPic )');

您尚未包括execute語句。 在bindParam之后添加以下語句。

$ q01-> execute();

我認為您的插入問題將得到解決。

暫無
暫無

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

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