簡體   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