Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends

I have design a signup page but i cant insert data to database I used ajax mechanism with php script!! pls give me solution i cant find error in my code
ajaxresponseText returns "signup success" but no data inserts database
PHP
<?php
//Ajax calls this registration code to execute
if(isset($_POST["u"])){
        //CONNECT TO THE DATABASE
        include_once("db_conx.php");
//GATHER THE POSTED DATA INTO LOCAL VARIALES

$u = preg_replace('#[^a-z0-9]#i','',$_POST['u']);
$e = mysqli_real_escape_string($db_conx,$_POST['e']);
$p = $_POST['p'];
$g = preg_replace('#[^a-z]#','',$_POST['g']);
$c = preg_replace('#[^a-z}]#','',$_POST['c']);

//Get user Ip
$ip = preg_replace('#[^1-9.]#','', getenv('REMOTE ADDR'));
//Duplicate data checks for username and email
$sql = "SELECT id FROM users WHERE username='$u'LIMIT 1";
if ($u_check = mysqli_query($db_conx,$sql))
{
     mysqli_num_rows($u_check);
}

//---------------------------------------------
$sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";

if($e_check = mysqli_query($db_conx,$sql))
{
         mysqli_num_rows($e_check);
}
//From Data Error Handling

if($u== ""|| $e == "" || $p == "" || $g == ""  || $c == "")
{

echo "The form submission is missing values.";
exit();
}
else if ($u_check > 0)
{
echo " The username you entered is already taken";
exit();
}
else if ($e_check > 0)

{
echo "Email Address is already in use";

exit();
}
else if(strlen($u)< 3 || strlen($u) > 16)
{
 echo " username must be between 3 and 16 characters";
exit();

}
    else if(is_numeric($u[0]))
    {
        echo 'UserName cannot begin with a number';
    exit();
    }

else
{

    //hash the password and apply your own mysterious unique salt
    crypt($p);

    include_once("randStrGen.php");
    $p_hash = randStrGen(20)."$p".randStrGen(20);
    //Add use info into the database
    echo"$u $e $p_hash $g $c $ip";
    $sql = "INSERT INTO users(username,email,password,gender,country,ip,signup,lastlogin,notescheck) VALUES('$u','$e','$p_hash','$g','$c','$ip,now(),now(),now())";
    $query = mysqli_query($db_conx,$sql);

    $uid = mysqli_insert_id($db_conx);
    //Establish their row in the useroptions table

    //$sql = "INSERT INTO useroptions(id,username,background)VALUES('$uid','$u','original')";
    //$query = mysqli_query($db_conx,$sql);

    // Creat directory to hold each user's files(pics,mp3s,etc)
    if(!file_exists("user/$u")){
        mkdir("user/$u",0755);
    }
//email the user their activation link
$to = "$e";
$from = "noreply@maxdudes.com";
$subject = 'maxdudes Account Activation';

//$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>yoursitename Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.maxdudes.com"></a>maxdudes Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.maxdudes.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>yoursitename Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.yoursitename.com"><img src="http://www.yoursitename.com/images/logo.png" width="36" height="30" alt="yoursitename" style="border:none; float:left;"></a>yoursitename Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.yoursitename.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
    $headers = "From: $from\n";
       $headers .= "MIME-Version: 1.0\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1\n";
//mail($to, $subject, $message, $headers);

echo "signup_success";
exit();
}

exit();
    }

?>
Posted
Updated 14-Sep-13 3:49am
v2
Comments
The Myth 14-Sep-13 8:54am    
It's been awhile since I did any mysql coding. A few questions to narrow things down:
- I assume the select statement works? So you know your connection string is working?
- I see two inserts. Do you know which SQL insert is not working? Do you know the actual SQL string or variables it is trying to insert? That way you can test them against the database directly to verify they work without the PHP code.
- Did you verify through the firefox's web console that there is no javascript errors or through HTTPWatch to see the ajax response that you are getting back from the server? Once you know this, you can test this directly outside of the page itself.

Once you can narrow down which piece is having the issue, it will be a lot easier to solve. I noticed you're using a lot of onclick, onfocus, onmousedown events in html, you should try to avoid these and have all that logic in the javascript by using jquery's onclick events instead. Also you're using XMLHttpRequest directly which probably isn't cross-browser compatible. Again, I would use JQuery for this logic. Have you tried this in multiple browsers? It might work in some and not others if the issue is ajax related.
Md Jamaluddin Saiyed 14-Sep-13 9:45am    
ok sir I tried to narrow down coding!! Thanks for your suggestions ! I learning jquery so I am not accurate in it !!
Killzone DeathMan 18-Sep-13 7:05am    
The insert query work?
"INSERT INTO users(username,email,password,gender,country,ip,signup,lastlogin,notescheck) VALUES('$u','$e','$p_hash','$g','$c','$ip,now(),now(),now())"

should your query not be:
"INSERT INTO users(username,email,password,gender,country,ip,signup,lastlogin,notescheck) VALUES('$u','$e','$p_hash','$g','$c','$ip',now(),now(),now())"

I mean sql sintax error, near "$ip"!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900