Loading...
start the course

Learn PHP

Codes and notes

INSERT DATA INTO TABLE
Back Next

After a database and a table have been created, we can start adding data in them.

Here are some syntax rules to follow:
The SQL query must be quoted in PHP
String values inside the SQL query must be quoted
Numeric values must not be quoted
The word NULL must not be quoted
syntax
<?php
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; ";
? >

Back Next

Code editor

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    
    # if statement in php
    
    
        $conn 
        
        = mysqli_connect("localhost","root","","firstTable");
        
   
        
        $sql 
        
        = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
        
              
        if  
            ($conn->query($sql) === TRUE) {
             
        
            
        echo
             
            
            "Data inserted successfully";
          
        
        
            }else {
         
        
        echo
             
        
        "Error inserting into table: " . $conn->error;
         
        
            
        $conn->close(); 
        }
    
              

        
    ?>

    </body>
    </html>
    

Output