【问题标题】:how to insert data in mysql( wamp) database through php?如何通过 php 在 mysql(wamp) 数据库中插入数据?
【发布时间】:2015-10-09 08:18:47
【问题描述】:

我试了很多次,连接成功,但是无法插入数据,请帮帮我。

以下是我的代码,我刚刚添加了一个 if 检查,以查看查询是否没有向数据库添加任何数据,那么它应该给出某种指示,因此它说无法发布。当我从 wamp 数据库中看到数据库表时,没有数据被插入或添加到表中,请帮助我,我们将不胜感激。谢谢。

    <?php
    if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['semester']) && isset($_POST['section'])):
    $post_id = $_POST['id'];
    $post_name = $_POST['name'];
    $post_semester = $_POST['semester'];
    $post_section = $_POST['section'];

    $link = new mysqli('localhost','root','','registration');

    if($link->connect_error)
        die('connection error: '.$link->connect_error);

    $sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."',)";

    //echo $sql;    

    $result = $link->query($sql); 

    if($result > 0):
        echo 'Successfully posted';
    else:
        echo 'Unable to post';
    endif;

    $link->close();
    die();
    endif; 
    ?>
    <!DOCTYPE html>
    <html>
     <head>
     </head>
     <body>
      <h1>Student Registration</h1>
      <form action="add.php" method="post">
        Student ID : <input type="text" name="id"/><br><br>
        Student Name : <input type="text" name="name"/><br><br>
        Semester : <input type="text" name="semester"/><br><br>
        Section : <input type="text" name="section"/><br><br>

        <input type="submit" value="create"/>
      </form>
     </body>
    </html>

【问题讨论】:

  • 插入回显 $link->错误;在无法发布行并与我们分享错误消息之后。您确定要手动填充 id 字段吗?
  • 弃用 mysql 并要求人们使用 mysqli 的全部目的是避免这种查询:“INSERT INTO student_info(student_id, student_name, term, section) VALUES('".$post_id." ', '".$post_name."', '".$post_semester."', '".$post_section."',)";

标签: php mysql database insert add


【解决方案1】:

您的 SQL 末尾有一个额外的逗号 (,)。正确的应该是 -

$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."')";

我还可以看到错误“无法发布”与您当前的代码,所以它工作正常。

【讨论】:

    【解决方案2】:

    试试;

    <?php
    if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['semester']) && isset($_POST['section'])){
    $post_id = $_POST['id'];
    $post_name = $_POST['name'];
    $post_semester = $_POST['semester'];
    $post_section = $_POST['section'];
    
    $link = new mysqli('localhost','root','','registration');
    
    if($link->connect_error)
        die('connection error: '.$link->connect_error);
    
    $sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('$post_id', '$post_name', '$post_semester', '$post_section')";
    
    //echo $sql;    
    
    $result = $link->query($sql); 
    
    if($result){
        echo 'Successfully posted';
    }else{
        echo 'Unable to post';
    }
    
    $link->close();
    die();
    
    }
    ?>
    

    【讨论】:

    • 即使你的答案是正确的,但答案不仅仅是代码的答案,你还应该解释为什么他的代码不起作用,以及为什么你的代码起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 2023-03-18
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多