0

I want to check some data if ti's remain on database. Example: in my form there are two form field. 1) url 2) name. So i want to check if session user have already this 'url' and 'name' in the database then it's must be show error message. Here is just few mysql code..

<?php
$url        = mysql_real_escape_string(trim($_POST['url']));
$name       = mysql_real_escape_string(trim($_POST['name']));
$uname  = $_SESSION['uname'];

$check_add  = mysql_query("SELECT * FROM add_fav WHERE url ='$url' AND name= '$name' 
AND uname = '$uname' ");

$num        =  mysql_num_rows($check_add); 

if($num > 0)
            $err[] = "Already exist";
?>

But It's doesn't check. May be it's my query problem.
Thanks in Advance:)

Umme Habiba Kanta
  • 173
  • 4
  • 4
  • 9
  • Welcome to Stack Overflow! You are not doing any error checking in your query, so it's no wonder nothing comes up and your script breaks if the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Jan 15 '12 at 18:13
  • are you showing just a fragment of your code, or have you really forgotten to do a mysql_connect? – bjelli Jan 15 '12 at 18:15
  • @bjelli It's already connected. – Umme Habiba Kanta Jan 15 '12 at 18:22

2 Answers2

0

Try and debug your script to make sure you actually have values for your select statement... echo out your variables to make sure they have data... check it right before you do your select statement, if they are coming up empty make sure that you instantiate the db connection before running mysql_real_escape_string or it won't work...

Jeff Wooden
  • 5,339
  • 2
  • 19
  • 24
0

Have you tried if you have an open connection with mysql ?

Have you tried this query directly in MySQL (like in PhpMyAdmin ?)

You should use PDO instead of mysql_*, it's way better an simplier.

Here's an example, that, in code, should work (it then depend if your table is like what you show us):

<?php
$oPdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'login', password');

// here's your query :
$oStmt = $oPdo->prepare ('SELECT * FROM add_fav WHERE url = :url AND name= :name AND uname = :uname');

$oStmt->execute (array (
    'url' => trim($_POST['url']),
    'name' => trim($_POST['name']),
    'uname' => $_SESSION['uname']
));

if (count($oStmt->fetchAll(PDO::FETCH_ASSOC)) > 0)
    $err[] = "Already exist";
?>
Cyril N.
  • 38,875
  • 36
  • 142
  • 243