php - if else statement problem in phpmysql

650

Could you help me with my script.

I have made an phpmysql script in which username and email is taken from a form and run it through the database. If the username and email exists in the database it will show 'already exist' other wise it will add to the database.

This is my script.The script is working (not showing any errors) but in the if else statement there is some problem (I think a syntax problem), it only showsalready member exist even if it is not in database. I am notable to identify the problem. I am a newbe in phpmysql

if(isset($_POST['submit'])) {
$name=mysql_real_escape_string($_GET['name']);
$tes=mysql_real_escape_string($tes);
print $name;
print $tes;

$sel=mysql_query("SELECT * FROM member WHERE email='$tes' AND usrename='$name'");
if(!isset($sel)) {
 echo('There is already a member');
}
else
{

//insert

 $sql = "INSERT INTO member SET username='$name',email='$tes', date=CURDATE()";
 if (@mysql_query($sql)) {
 echo('<p>You have been added to database.</p>');
 } 
 }
 }
564

Answer

Solution:

instead of

if(!isset($sel))

you can to use

if(mysql_num_rows($sel) > 0)

check mysql_num_rows

716

Answer

Solution:

On your mysql_query line there is a spelling error: AND usrename should be AND username

335

Answer

Solution:

you need to add some code..

<?php    
$sel=mysql_query("SELECT * FROM member WHERE email='".$tes."' AND usrename="'.$name.'");

$data = mysql_fetch_array($sel)

if($data[0]['email']!=''){

    echo 'not in database..';

}

else{

    echo 'in database....';
}
?>
942

Answer

Solution:

try this solution

        <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    $mysqli = new mysqli("localhost", "root", "", "articles");

    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

    /* Select queries return a resultset */
    $con = 1;
    $sql = sprintf("SELECT * FROM users WHERE user_name='%s'", mysql_real_escape_string($user));    

    if ($result = $mysqli->query($sql)) {
        printf("Select returned %d rows.\n", $result->num_rows);
        if($result->num_rows == 0) {
            //TODO :: insert new record
        }
        else {
            //TODO:: USER already exists
        }

        /* free result set */
        $result->close();
    }


    $mysqli->close();
    ?>
380

Answer

Solution:

login in core php

<?php
$usename = $_POST['username'];
$pass = $_POST['password'];

$rowcount = mysqli_num_rows($result);
if($rowcount > 0)
{
  echo "Usename already exits";
}
else
{
  $sql = mysql_query('INSERT INTO `table_name`');
  echo (!empty($sql)) ? "User added." : "Error occured.";
}
?>

People are also looking for solutions to the problem: Explode string php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.