0

hey i have this code that always evaluates to true but it should only in the case of there being a value in the database i have selected, maybe im doing this wrong or maybe its something else but i cant seem to figure out the problem :( any assistence is greatly appreciated.

//submission code, inserting data into mysql database

$mySqlDate = date('Y-m-d');
$mySqlTime = date("g:i a");

if(isset($_GET['add']))
{
    $newnum = $_GET['newnum'];
    $firstname = $_GET['firstname'];
    $lastname = $_GET['lastname'];
}

if(strlen($newnum) == $min_length)
{
    $newnum = mysqli_real_escape_string($con, $_GET['newnum']);
    $firstname = mysqli_real_escape_string($con, $_GET['firstname']);
    $lastname = mysqli_real_escape_string($con, $_GET['lastname']);

    $check = mysqli_query($con, "SELECT count(*) FROM usernumdata WHERE numb = '".$newnum."'") or die();
    $row = mysqli_fetch_row($check);

    if ($row[0] > 0)
    {
        $sql="INSERT INTO usernumdata (numb) VALUES ('".$newnum."')";

        if (!mysqli_query($con,$sql)) {
            die('Error: ' . mysqli_error($con));
        }   
        echo "<div style='text-align: center'> <font size='6' color='green'>Successfully Added New Number</font> <div>";
    }
    else
    {
        echo "<div style='text-align: center'> <font size='6' color='red'>Number Already Exists In Do Not Call List</font> <div>";
    }
    /*
    $sql="INSERT INTO usernumdata (numb, firstname, lastname, date, time) VALUES ('".$newnum."','".$firstname."','".$lastname."','".$mySqlDate."','".$mySqlTime."')";

    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    echo "<div style='position:fixed; bottom:65; left:37%'> <font size='6' color='green'>Successfully Added New Entry</font> <div>";
    */
}

else{
    $length = "Remember Phone Number Must be ".$min_length. " Digits Long";
    echo "<div style='position:fixed; bottom:50; left:31%;'> <font size='6' color='blue'>".$length."</font> <div>";
}

as you can see from the code above im trying to set it up to do a double test which is probably bad coding pratice but i needs to output different messages in case of either, and what i was trying to do was see if the value was in the database already before inserting a new one but for some reason it always evaluates to true? help

2
Contributors
4
Replies
21
Views
3 Years
Discussion Span
Last Post by berserk
0

that looks like it could work but my question is how would you use the if statement to run a check and give feedback to the user to let them know whther or not the number is there, also can you give an example cause this looks a little complicated, sorry im kind of new to the whole php/html/mysql thing so simple explanations are appreciated, thanks.

1

I changed your if condition at line 22 , if row count == 0 then means record does not exists with number, so show insert when $row[0]==0 , otherwise show "already exits" message

   if ($row[0] == 0)
0

Thank you so much urtrivedi, this is exactly what i needed. I bow to your wisdom cause i still dont fully understand but i am reading more into it as well as something on creating a login page so i will go from there. Thanks again for all the help you gave me. it was very much appreciated

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.