【问题标题】:why php mysqli does not insert data to database in Xampp为什么 php mysqli 不向 Xampp 中的数据库插入数据
【发布时间】:2014-11-05 18:30:27
【问题描述】:

我使用以下代码将数据插入到名为 sample 的数据库中,该表也称为 sample.. 它们是四个字段 first_name、last_name、bio、created。 当我

但是当我使用这段代码时,有一个 html 表单来输入数据..它不起作用。我没有收到任何错误消息...数据没有插入到数据库中..

[代码]

   <?php
    error_reporting(-1);
    ini_set('display_errors', 'On');
    //require 'functions/security.php';
    $db = new mysqli('localhost', 'root', '', 'sample');

    //check connection
    if($db->connect_errno) {
        die('sorry, we are having some problems');
        }else {
        echo 'connected';
    }

    $records = array();

    if(!empty($_POST)) {
      if(isset($_POST['first_name'], $_POST['lats_name'],$_POST['bio'])){

            $first_name  = trim($_POST['first_name']);
            $last_name   = trim($_POST['last_name']);
            $bio         = mtrim($_POST['bio']);

            if(!empty($first_name) && !empty($last_name) && !empty($bio) ){

            $insert = $db->prepare
            ("INSERT INTO sample(first_name, last_name, bio, created) 
            VALUES (?, ?, ?, NOW())");
            $insert->bind_param('sss',$first_name, $last_name, $bio);

            if($insert->execute()){
                header('Location: index.php');
                die();
                }

            }
        }
      } 

    if($results = $db->query("Select * from sample")) {
        if($results->num_rows){
            while($row = $results->fetch_object()){
                $records[] = $row;
            }
            $results->free();
        }
    }


    ?>

    <!DOCTYPE html>
    <html>

        <head>
        <title>Sample</title>
        </head>
        <body>
            <h3>Sample</h3>
            <?php
            if(!count($records)) {
            echo 'No records';

            } else {

            ?>
            <table>
                <thead>
                    <tr>
                        <th>first name</th>
                        <th>Last name</th>
                        <th>Bio</th>
                        <th>Created</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    foreach($records as $r){
                    ?>
                        <tr>
                            <td><?php echo $r->first_name; ?></td>
                            <td><?php echo $r->last_name; ?></td>
                            <td><?php echo $r->bio ?></td>
                            <td><?php echo $r->created; ?></td>
                        </tr>
                        <?php
                        }

                        ?>
                    </tbody>
            </table>
            <?php
            }
            ?>
            <hr>
            <form action=""method="POST">
                <div class="field">
                    <label for="first_name">first_name"</label>
                    <input type ="text" name="first_name" id="first_name">
                </div>
                <div class="field">
                    <label for="last_name">last_name" </label>
                    <input type ="text" name="last_name" id="last_name">
                </div>
                <div class="field">
                    <label for="bio">bio" </label>
                    <textarea name="bio" id="bio"></textarea>
                    <input type="submit" value="insert">
                </div>
            </form>

            </hr>

        </body>
    </html>


[/code]

【问题讨论】:

  • 您没有检查错误。这就是你看不到任何东西的原因。
  • 因为name="last_name" + $_POST['lats_name'] = 没有爱。
  • 还有mtrim($_POST['bio'])没有mtrim PHP函数。

标签: php html mysql mysqli xampp


【解决方案1】:

这是因为name="last_name" + $_POST['lats_name'] 错字不匹配。

应该是$_POST['last_name']

还有mtrim($_POST['bio'])没有mtrim PHP核心函数。

和其他两个一样:

$first_name  = trim($_POST['first_name']);
$last_name   = trim($_POST['last_name']);
$bio         = trim($_POST['bio']);

在您的文件中使用错误报告将有助于生产测试,并且应该表明该错误。

error_reporting(E_ALL);
ini_set('display_errors', 1);

你显然使用了类似的东西,但想把它放在那里。


旁注:考虑将其间隔开一点&lt;form action=""method="POST"&gt;

&lt;form action="" method="POST"&gt;

【讨论】:

  • 谢谢大家我做了这些更正。我没有收到任何错误代码。但数据仍然没有插入数据库...
  • 当我运行这个 [code]if($insert = $db->query(" INSERT INTO sample(first_name, last_name, bio, created) VALUES ('testJane', 'lasttestname', '测试记录', NOW())")){ echo $db->affected_rows; } [/code]
  • 不,它不起作用..它仍然没有将数据插入数据库
  • 我注意到您的 DB ('localhost', 'root', '', 'sample') 和表 INSERT INTO sample 都被命名为相同的“sample”。你确定你没有弄错这些吗?还要检查您的列名及其类型,包括(最大/最小)长度。 @Darius 如果长度太小而无法容纳数据,它将失败。
  • 我这样做是为了让事情变得简单。我创建了这段代码来弄清楚为什么我真正想要生成的代码不起作用..这是一个更简单的形式和代码。如果我能让这个工作..那么我也许能够弄清楚为什么另一个不能工作......
猜你喜欢
  • 2014-12-06
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
相关资源
最近更新 更多