繁体   English   中英

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

[英]how to insert data in mysql( wamp) database through php?

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

以下是我的代码,我刚刚添加了一个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>

最后,您的SQL中还有一个逗号(,)。 正确的是-

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

我还可以看到您当前的代码出现“无法发布”错误,因此可以正常工作。

尝试;

<?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();

}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM