开发者

mysql compare table values

开发者 https://www.devze.com 2022-12-15 12:42 出处:网络
I am creating a poll in php. I开发者_Python百科 have a table with an id column. When a user submits a choice, I want the php to first query the database and check whether the table \"id\" has the id

I am creating a poll in php. I开发者_Python百科 have a table with an id column.

When a user submits a choice, I want the php to first query the database and check whether the table "id" has the id of the current user.

I am pretty new at mysql, can someone help me with the query?


Try this:

$q = "SELECT id FROM table WHERE id = '".mysql_real_escape_string($_POST['user_id'])."'";

$r = mysql_query($q);

if(mysql_num_rows($r) > 0) {
    echo "User ID was found in table";
} else {
    echo "User ID was not found in table";
}


$qryResult = mysql_query("SELECT userid FROM idtable WHERE userid='$theIdOfUser'");
if (mysql_num_rows($qryResult) == 0)
{
// add a new vote
}
else
{
// notify the user that double voting is prohibited
}

Try not to use names like "id" to avoid conflicts with reserved words and related complications and need to use quotes

0

精彩评论

暂无评论...
验证码 换一张
取 消