简体   繁体   中英

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

Hi I have a drop down list that has values from one db table and I want on button click to save the row of the specific id in another table I have this form

<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>

And my php is

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);
    }
}

Can you help me fix this? The button is working fine but the value is not stored inside db table

Thank you

2 corrections in your PHP

  1. Missing : in VALUES of INSERT query. Replace uempPic with :uempPic .

  2. INSERT query is not executed. Add below line after last bindParam statement.

     $q01->execute(); 

此行错误,请检查一下...

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

Please have a look on your prepare statement:

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

You have forgot to add ":" with "uempPic", Please use below statement.

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

You have not included execute statement. Add below statement after bindParam.

$q01->execute();

I think your insert issue will get resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM