【问题标题】:Insert into db table (query 2) from another table (query 1) in php从 php 中的另一个表(查询 1)插入 db 表(查询 2)
【发布时间】:2016-12-09 07:14:47
【问题描述】:

您好,我有一个下拉列表,其中包含一个数据库表中的值 我想点击按钮将特定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);
    }
}

你能帮我解决这个问题吗? 按钮工作正常,但值未存储在 db 表中

谢谢

【问题讨论】:

  • @ManojSharma 全部由您的答案编辑和修复,但我收到此错误Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'chart' cannot be null' ( ! ) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'chart' cannot be null
  • 谢谢大家帮助我解决了另一个想法,我想出了再次感谢

标签: php html mysql dropdown


【解决方案1】:

PHP 中的 2 处更正

  1. VALUESINSERT 查询中缺少 :。将uempPic 替换为:uempPic

  2. INSERT 查询未执行。在最后一个 bindParam 语句之后添加以下行。

    $q01->execute();
    

【讨论】:

  • 我打印了所有 php 变量 $chart 等,它们都是空 SQLSTATE[23000] 为什么我的代码没有从下拉列中获取选定的 id?
  • 你能帮助我为什么得到空值吗?
  • 那是因为,您已经使用POST 提交了值,并使用$_GET 获取了值。请改用$_POST 来检索提交的值。
  • 我改变了它,现在我正在使用$_POST,但是下拉列表中有空值可以在提交时插入到另一个表中
【解决方案2】:

此行出错,请检查...

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

【讨论】:

    【解决方案3】:

    请查看您的准备语句:

    $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)');

    您没有包含执行语句。在 bindParam 之后添加以下语句。

    $q01->执行();

    我认为您的插入问题会得到解决。

    【讨论】:

    • 这意味着,你应该检查字段'chart'的值,它应该有一些值。请尝试打印您的查询并检查 $chart 变量的值。当 $chart 修复时,它就会起作用。
    • 第一个数据库表充满了值,并在其他页面视图中正确表示问题是我无法将这些从下拉列表复制到具有相同列的另一个空数据库表 $chart 变量应该包含图表行列值我的代码中没有正确的东西
    • 所有字段值都是 null 我 var 转储了它们,但它们没有从下拉列表中获取值
    • 其实你并没有在这里发布完整的表单代码,但是我会要求你验证这一点。 1.请告诉我表格方法。如果您不能这样做,请将 $_GET 更改为 $_REQUEST。还有一件事要记住我看不到这个下拉列表的名称,请设置这个下拉列表的名称,那么你肯定会得到 $chart 的值。
    • 1. form method = post 2. 下拉列表的名称
    猜你喜欢
    • 2018-05-05
    • 2017-12-10
    • 2014-03-12
    • 2013-12-14
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多