Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to insert values into database in php.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 507
    Comment on it

    Inserting data to a database would be possible by using SQL statements, specifically the INSERT command. Data can be entered  into the database using insert query into the function, here is simple line of code that will create a database using insert query.

    Database connection:-

    <?php
    	$servername = "localhost";
    	$username = "root";
    	$password = "";
    	$dbname = "shopping_db";
    
        // Create connection
     $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
     if ($conn->connect_error) 
        {
        die("Connection failed: " . $conn->connect_error);
        } 
    
       else
        echo "connection sucessfuly";
    ?>
    

    Inserting values to the database using php code:-

    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") 
    {
    	if (isset($_POST['username']) && ($_POST['email']) && ($_POST['password'])) 
    	{
    	     // collect value of input field
    	     $username = $_REQUEST['username']; 
    	     $email = $_REQUEST['email']; 
    	     $password = $_REQUEST['password']; 
    	     $sql = "SELECT * FROM details where email = '$email'";
        	     $result = $conn->query($sql);
                 if ($result->num_rows == 1)
                    {
                  echo "<script type ='text/javascript'> alert ('This email is already registered')</script>";
        		}
    
                 else{
    	        $sql1 = "INSERT INTO details(username,email,password) VALUES('$username','$email','$password')";            
                    if ($conn->query($sql1) === TRUE) 
    		  {
    		    header('Location:php_loginform.php');
    		  }
    	        else{
    	          echo "Error creating database: " . $conn->error  
                        }
                      }  
    	}
    }
    
    ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: