dongtan7201 2015-10-09 08:18
浏览 171
已采纳

如何通过php在mysql(wamp)数据库中插入数据?

I have tried many times, connection is successful, but I can not insert the data please help me.

Following is my code, I have just added an if check, to see if the query does't adds any data to the database then it should give some kind of indication so its saying unable to post. When I see in database table from wamp database, no data is inserted or added to the table, please help me it will be much appreciated. thanks.

    <?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>
  • 写回答

2条回答 默认 最新

  • douzhan1031 2015-10-09 08:26
    关注

    There is an extra comma (,) in your SQL at the end. The correct one would be -

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

    I was also able to see error 'Unable to Post' with your current code so that is working fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器