Jump to content

Check to see if a Variable Exists in an SQL Database


cjbeck71081

Recommended Posts

I am working on a project.  I want to safegaurd against a user entering a number into the database multiple times.  So what i want to do is run a query and check the users entry against the database, and then return an error if it exists.  Here is how i thought it would work

 

The script would take the Text Field posted and asign it a variable

 

$var = $POST_['textfld'];

Then i thought it might work if i were to say

 

$var2 = mysql_query("SELECT item FROM table WHERE var = '$var'");

 

Then i could have a statement similar to

 

if { $var = $var2

      echo "Number already entered into Database";

}

else echo "Thank You";

 

I thought that would be a good way to make it work, but its not working so far, any suggestions?

 

hey there cjbeck71081,

 

this should be what your looking for:

$var = $POST_['textfld'];

$result = mysql_query("SELECT item FROM table WHERE var = '$var'");

if (!$result) {

    echo 'Could not run query: ' . mysql_error();

    exit;

}

while ($row = mysql_fetch_assoc($result)) {

    $var2 = $row["textfld"];

}

mysql_free_result($result);

 

-----------------------------------

 

the issue is your $var2 is a resullt of the query and not the actual data.

for more info check here:

http://us.php.net/manual/en/function.mysql-fetch-assoc.php

 

good luck!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.