Select from Database or Fail PHP/MySQL

Hey all,

So I am working on a code and I am trying to update multiple rows of a table with a form. I have it working other than if a new value is added with the form. For instance, if it is a names table and someone is trying to update the row for Fred but Fred doesn’t exist on the table, I want to add a row for Fred. I thought I could use a Select * From Names Where name = ‘Fred’, execute it with mysql_query and if it doesn’t return anything then add the row. If anyone has any ideas I would greatly appreciate it.

Thanks!

Check if mysql_connect returns value or false,
Check if mysql_select_db returns value or false,
Check if SELECT sentence are correct, print sentence before mysql_query and check it in mysql commands.
Check if result of mysql_query are empty or are ===false , if it’s false, there is an error.

If don’t works, try to post an error or code or something of project.

for ($i=1;$i<=$number;$i++)
{
    $name = $_POST["name" . $i];
    $query = "SELECT * FROM People WHERE name='$name';";
    $result = mysql_query($query);
    if ($result == 0)
    {
        echo "False";
    }
    else
    {
        echo "True";
    }
    echo "</br>";
}

This is where I’m having issues. The select query seems to be correct, I just can’t seem to figure out what the ‘mysql_query’ returns with a Select statement. This code echoes False twice when I attempt to enter two names, one that is in the database and one that is not.

Nevermind, I figured it out. Read about a million online tutorials on mysql_query and mysql_fetch_array.

Sponsor our Newsletter | Privacy Policy | Terms of Service