【问题标题】:Why the inserted data into the table does not show when the inserted also successful [duplicate]为什么插入的数据在插入也成功时不显示到表中[重复]
【发布时间】:2019-06-02 16:59:15
【问题描述】:

我创建了一个表单来将数据插入到在 localhost 的 mysql(phpmyadmin) 中创建的表中。当我填写表格并单击保存按钮时,数据未插入表中。但是,行 ID 增加了。它也没有显示任何错误消息。根据编码,说明消息数据插入也成功了。

使用 phpMyAdmin 4.8.4、MySql、PHP 版本 5.6.40。我在 localhost 中尝试过。

数据库名称:采购测试; 表名:budgettypes; 列名称:ID、budgetDescription、shortDescription、budgetCode

这是连接数据库的代码,分别是“insert.php”和“budgetTypes.php”。

$con = mysqli_connect("localhost","root","","procurementtest");
if(!$con){
    echo 'Not connected to server';
}

if(!mysqli_select_db($con,'procurementtest')){
    echo 'Database Not Selected';
}

$BudgDes = isset($_POST['Budget Description']);
$ShorDes = isset($_POST['Short Description']);
$BudCode = isset($_POST['Budget Code']);

$sql = "INSERT INTO budgettypes (budgetDescription, shortDescription, budgetCode) VALUES ('$BudgDes', '$ShorDes' , '$BudCode'); ";

if (!mysqli_query ($con, $sql)){
    echo 'Not Inserted';
}else{
    echo 'Inserted';
}

    <div class="container">

    <h1>Budget Types</h1>

    <div class="pill-nav">
       <!-- <a class="active" href="#home">Home</a>-->
        <a href="#new">New</a>
        <a href="#Edit">Edit</a>
        <a href="#Delete">Delete</a>
    </div>


                <form action="insert.php"  method="POST">
                    <div class="form-group">
                    <label>Budget Description</label>
                    <input type="text" name="Budget Description" class="form-control" >
                    </div>

                    <div class="form-group">
                    <label>Short Description</label>
                    <input type="text" name="Short Description" class="form-control" >
                    </div>

                    <div class="form-group">
                    <label>Budget Code</label>
                    <input type="text" name="Budget Code" class="form-control" >
                    </div>

                    <div class="form-group">
                    <button type="submit" class="btn btn-default" id="Save" name="Save"> Save</button>
                    </div>
                </form>

                </a>
            </li>       
    </div>
</body>

插入数据后显示此消息:

“插入”

This is the output in the table. 我是一个新的学习者。我参考了 youtube 视频来创建这个“insert.php”文件。感谢您的帮助。

【问题讨论】:

  • 你的问题是 isset() 的使用。请阅读手册。

标签: php html


【解决方案1】:

其实你是在检查相反的情况,请用这个来纠正它

if (mysqli_query($conn, $sql)) {
     echo 'Inserted';
} else {
     echo 'Not Inserted';
}

记得在任何数据库操作之后关闭连接:)

mysqli_close($conn);

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 2019-12-05
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多