Jump to content

[SOLVED] error in your SQL syntax - can anyone see whats wrong????


daveh33

Recommended Posts

<?php
session_start();
include("dbconnect.php");
$username = $_POST['UserName'];
if (!$username) {
echo "You Must enter a username<br>";
$problem = "error";
} else {
$query = mysql_query("SELECT * from TABLENAME WHERE username ='$username'") or die(mysql_error());
$result = mysql_query($query);
$row = mysql_fetch_array($query) or die(mysql_error());
$numrows = mysql_num_rows($query);
echo $numrows;
$dbusername = $row['username'];
}
if ($dbusername) {
echo "Sorry that username is not available, please go back and try again.<br>";
$problem = "error"; 
} else {
$password = $_POST['UserPassword'];
if (!$password) {
echo "You Must enter a password<br>";
$problem = "error";
}
$dob = $_POST['UserDob'];
if (!$dob) {
echo "You Must enter Your date of birth<br>";
$problem = "error";
}
$email = $_POST['UserEmail'];
$mobile = $_POST['UserMobile'];
if (!$mobile) {
echo "You Must enter your mobile number<br>";
$problem = "error";
}
if ($problem=="error") {
echo "<br><b>Your registration was not sucessful</b><br>";
} else {
mysql_query("INSERT INTO registration (username, password, age, mobile, email) VALUES ('$username', '$password', '$dob', '$mobile',  '$email')") or die(mysql_error());
echo "<p>Registration sucessful, welcome to our site $username!";
}
}
?>

I get the error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1

 

Can anyone see whats wrong?? ??? ??? ???

Link to comment
Share on other sites

Your performing a mysql_query() twice.

 

Change this

$query = mysql_query("SELECT * from TABLENAME WHERE username ='$username'") or die(mysql_error());
$result = mysql_query($query);

 

To this

$query = "SELECT * from TABLENAME WHERE username ='$username'";
$result = mysql_query($query)or die(mysql_error());

Link to comment
Share on other sites

Ah yes - that was silly of me.

 

When I change that I get the below error : -

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in processregistration.php on line 11

 

What is wrong with line 11 of the code

  $row = mysql_fetch_array($query) or die(mysql_error());

Link to comment
Share on other sites

You had quite a few errors in your code. See if this works for you.

 

<?php
session_start();
include("dbconnect.php");

if (!isset($_POST['UserName'])) {

    echo "You Must enter a username<br>";
    $problem = "error";
    
} else {
    
    $username = $_POST['UserName'];
    $query = "SELECT * from TABLENAME WHERE username ='$username'";
    $result = mysql_query($query)or die(mysql_error());
    
    if (mysql_num_rows($result) > 0) {
        
        echo "Sorry that username is not available, please go back and try again.<br>";
        $problem = "error";
        
    } else {
        
        if (!isset($_POST['UserPassword'])) {
            echo "You Must enter a password<br>";
            $problem = "error";
        }
        
        if (!isset($_POST['UserDob'])) {
            echo "You Must enter Your date of birth<br>";
            $problem = "error";
        }
        
        if (!isset($_POST['UserMobile'])) {
            echo "You Must enter your mobile number<br>";
            $problem = "error";
        }
        
        if ($problem=="error") {
            echo "<br><b>Your registration was not sucessful</b><br>";
            
        } else {
            
            $dob = $_POST['UserDob'];
            $password = $_POST['UserPassword'];
            $email = $_POST['UserEmail'];
            $mobile = $_POST['UserMobile'];
            
            mysql_query("INSERT INTO registration (username, password, age, mobile, email)
            VALUES ('$username', '$password', '$dob', '$mobile',  '$email')") or die(mysql_error());
            
            echo "<p>Registration sucessful, welcome to our site $username!";
        }
    }
}
    
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.