csash7 csash7 - 7 months ago 9
SQL Question

INSERT INTO doesnt work php

I'm new to php.I'm trying to build a signup webpage in which if email entered doesn't exist it should insert the values entered.The code works fine and it returns successful when a new mail is entered.But the problem is when I check my database the new values are not inserted.Is there any mistake in my code?
Thanks in advance.

<?php
session_start();
if(isset($_POST['signup'])){
include_once("db.php");

$email=strip_tags($_POST['emailid']);
$username=strip_tags($_POST['username']);
$password=strip_tags($_POST['password']);

if($email==NULL || $username== NULL || $password==NULL){

print "Missing one of the fields";
}
else{

$email=stripslashes($email);
$username=stripslashes($username);
$password=stripslashes($password);

$email=mysqli_real_escape_string($db,$email);
$username=mysqli_real_escape_string($db,$username);
$password=mysqli_real_escape_string($db,$password);

$query = "SELECT * FROM user WHERE email='$email'";
$result = mysqli_query($db,$query);

if($result && mysqli_num_rows($result) > 0 )
{
echo "Account already exists.Please login";
}
else{
$sql="INSERT INTO user (ID,email,username,password) VALUES
(NULL,'$email','$username','$password')";
if($sql)
{
echo "Account created successfully.";

}
else
{
echo "Error";

}
}
}
}
?>

Answer

You are not executing the insert query, it should look like:

$sql="INSERT INTO user (ID,email,username,password) VALUES   
(NULL,'$email','$username','$password')";
$sql= mysqli_query($db,$sql); ///You are missing this