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

10,853

Solution 1

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.

Solution 2

Try;

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

}
?>
Share:
10,853
Umer Abbas
Author by

Umer Abbas

One thing I can say for sure is I love solving complex logical problems. Here's my Skype @umerabbas8

Updated on June 04, 2022

Comments

  • Umer Abbas
    Umer Abbas almost 2 years

    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>
    
    • Shadow
      Shadow over 8 years
      insert echo $link->error; after the unable to post line and share the error message with us. Are you sure that the id field is to be populated manually?
    • e4c5
      e4c5 over 8 years
      The whole purpose of deprecating mysql and asking people to use mysqli is to avoid this kind of query: "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."',)";
  • Masivuye Cokile
    Masivuye Cokile about 7 years
    Even though your answer is correct, but however an answer is not just an answer by a code, you should also explain why he's code did not work, and why is yours working.