if row exists

I’m trying to stop dublicate entries in a database. I have

[php]

$sql_check = mysql_query(“SELECT * FROM links WHERE site=’$url’”);
$check_result = mysql_num_rows($sql_check);

if($check_result !==“0”){
echo “The URL you have entered is already present!”;
exit;
}

[/php]

But as soon at the page loads its givving the echo even though its encapsulated in

[php]

if (!$submit || !$title || !$url || !$description || !$email || !$userrecipURL)
{

[/php]

if i change

[php]

if($check_result !==“0”)

to

if($check_result >“0”) // The check_result script doesn’t run

[/php]

I’ve also tried it without the quotes and it still doesnt work.
Any suggestions

u are comparing 2 diffrent types with !== witch should always return true:
mysql_num_rows returns an int http://php.net/mysql_num_rows
“0” is a string http://php.net/types
!== is compating the type first if they are difftent it will return true (witch is the case here), only if they are the same it’ll compate the values.
http://php.net/operators.comparison

this makes all thats inside executed as soon as at least on of these vars is not filled!!!

i guess u need[php]if ($submit && $title && $url && $description && $email && $userrecipURL)
{
[/php]

maybe because of the first prob: that part is only execuded if vars are missing. and if vars are missing the select will never return a row.

plz to use if($check_result > 0) or if($check_result != 0)

i’ve tried

[php] if ($submit && $title && $url && $description && $email && $userrecipURL) { [/php]

This is having the opposite effect, you can now save blank fields

As for

[php] if($check_result >"0") [/php

I’ve tried

[php] if($check_result >"0")

if($check_result >0)

if($check_result !=0)

[/php

But it still gives the same results. Its not checking the $url against the site filed in the database

could u post a little more of that script. as al i know till now is that ur script looks like this:
[php]

if (!$submit || !$title || !$url || !$description || !$email || !$userrecipURL)
{

$sql_check = mysql_query(“SELECT * FROM links WHERE site=’$url’”);
$check_result = mysql_num_rows($sql_check);

if($check_result !==“0”){
echo “The URL you have entered is already present!”;
exit;
}

}

[/php]
and if that was the case my answer would have been right

Here is the script without verifying if a link exists

[php]

<?

if (!$submit || !$title || !$url || !$description || !$email || !$userrecipURL)
{

?>

Your listing will appear in the following category:

<? if (count ($arPath) > 0) { for ($i = 0; $i < count ($arPath); $i++) { array_push ($arSubPath, $arPath[$i]); if ($i == 0) { $parent = 0; } else { $parent = $arIds[count ($arIds) - 1]; echo ' | '; } $thisCat = mysql_query ("SELECT id, name FROM link_cats WHERE name LIKE '$arPath[$i]' AND parent = $parent "); if (mysql_num_rows ($thisCat) > 0) { $row = mysql_fetch_assoc ($thisCat); array_push ($arIds, $row['id']); $subPath = implode ('/', $arSubPath); ?>
	<span class="book_tab_head2"><?php echo $row['name'] ?></span>
   <?
  }
}

echo "n";

}
?>

<?php

if ($listID)
{
?>
<?php
}

if ($row[‘id’])
{
?><?php
}
else
{
?><?php
}

?>


<? if ($submit && !$fName) { ?>
   <tr><td valign="top" class="search_input_err">Please enter your Firstname:</td>
   <td><input name="fName" type="text" id="sName" style="width: 400px;"/></td></tr>
<? } else { ?>
   <tr><td valign="top" class="lis_tab_cont">Firstname:</td>
   <td><input name="fName" type="text" class="search_input" id="fName" style="width: 400px;" value="<? echo stripslashes ($fName); ?>" /></td></tr>
<? } ?> <? if ($submit && !$title) { ?>
   <tr><td valign="top" class="search_input_err">Please enter your Surname:</td>
   <td><input name="sName" type="text" id="sName" style="width: 400px;"/></td></tr>
<? } else { ?>
   <tr><td valign="top" class="lis_tab_cont">Surname:</td>
   <td><input name="sName" type="text" class="search_input" id="sName" style="width: 400px;" value="<? echo stripslashes ($sName); ?>" /></td></tr>
<? } ?> <? if ($submit && !$title) { ?>
   <tr><td valign="top" class="search_input_err">Please enter the website Title:</td>
   <td><input type="text" name="title" id="title" value="" style="width: 400px;" /></td></tr>
<? } else { ?>
   <tr><td valign="top" class="lis_tab_cont">Title:</td>
   <td><input name="title" type="text" class="search_input" id="title" style="width: 400px;" value="<? echo stripslashes ($title); ?>" /></td></tr>
<? } ?> <? if ($submit && !$url) { ?>
   <tr><td valign="top" class="search_input_err">Please enter a URL:</td>
   <td><input type="text" name="url" id="url" value="http://" style="width: 400px;"/></td></tr>
<? } else { if (!$url) { $url = 'http://'; } ?>
    <tr><td valign="top" class="lis_tab_cont">URL:</td>

    <td><input name="url" type="text" class="search_input" id="url" style="width: 400px;" value="<? echo stripslashes ($url); ?>" /></td></tr>
<? } ?> <? if ($submit && !$description) { ?>
    <tr><td valign="top" class="search_input_err">Please enter a Description:</td>
    <td><textarea name="description" id="description" style="width: 400px;"></textarea></td></tr>
<? } else { ?>
    <tr><td valign="top" class="lis_tab_cont">Description:</td>
    <td><textarea name="description" class="search_input" id="description" style="width: 400px;"><? echo stripslashes ($description); ?></textarea></td></tr>
<? } ?> <? if ($submit && !$email) { ?>
   <tr><td valign="top" class="search_input_err">Please enter your Email Address:</td>
   <td><input type="text" name="email" id="email" value="" style="width: 400px" /></td></tr>
<? } else { ?>
    <tr><td valign="top" class="lis_tab_cont">Email Address:</td>
    <td><input name="email" type="text" class="search_input" id="email" style="width: 400px;" value="<? echo stripslashes ($email); ?>" /></td></tr>
<? } ?> <? if ($submit && !$userrecipURL) { ?>
<td><input name="userrecipURL" type="text" class="search_input_err" id="userrecipURL" style="width: 400px;" value="" /></td></tr>
<? } else { ?>
 <tr><td valign="top" class="lis_tab_cont">Reciprocal Link Location:</td>

<td><input name="userrecipURL" type="text" class="search_input" id="userrecipURL" style="width: 400px;" value="<? echo stripslashes ($userrecipURL); ?>" /></td></tr>
<? } ?>
 <tr><td colspan="2" align="center"><p style="margin:20px"><input name="submit" type="submit" class="search_input" style="font-family: Trebuchet MS; font-size: 8pt" value="Save Listing" />
 </p></td></tr>
Please enter the Reciprocal Link Location:
<? } else if ($listID)
 {

    $listDate = date ("Y-m-d");

    $queryStr  = "UPDATE listings SET fName = '$fName',  sName = '$sName', cat = '$catID', email = '$email', title = '$title', description = '$description', site = '$url', recip_url = '$userrecipURL)', added = '$listDate', `check` = '0' WHERE id = '$listID'";

    if (mysql_query ($queryStr))
    {
    ?>
        <p><span class="lis_tab_head">Your have successfully edited this link.</span><br />
          <br />
        <a href="../links.php" class="countries"><span class="countries">Back</span></a></p>
    <?
    }
    else
    {
      echo 'Error: ', mysql_errno (), ': ', mysql_error ();
    }
 }
 else
 {
 

    $listDate = date ("Y-m-d");

    $queryStr  = "INSERT INTO links (fName, sName, cat, email, title, description, site, recip_url, added, `check`) VALUES 
	('$fName', '$sName', '$catID', '$email', '$title', '$description', '$url','$userrecipURL', '$listDate', '0')";



    if (mysql_query ($queryStr))
    {
        $listID = mysql_insert_id ();

        $parent = $catID;
        while (isset ($parent))
        {
            mysql_query ("UPDATE link_cats SET children = children + 1 WHERE id = $parent ");

            $parentExists = mysql_query ("SELECT parent FROM link_cats WHERE id = $parent ");
            if (mysql_num_rows ($parentExists) > 0)
            {
                $parentID = mysql_fetch_assoc ($parentExists);

                $parent = $parentID['parent'];
            }
            else
            {
                unset ($parent);
            }
        }

        ?>
        <p class="lis_tab_head">Your listing has been saved successfully.<br />
          <br />
        <a href="../links.php" class="countries"><span class="countries">Back to Link Index</span></a></p>
        <?
    }
    else
    {
      echo 'Error: ', mysql_errno (), ': ', mysql_error ();
    }
 }

 mysql_close ($connection);

 ?>
[/php]

if u want the url’s to be unique i would allready make the databade take control over it.

make the field site having an index “UNIQUE”

and then use the mysql_errno to display the right errormessage
[php] $queryStr = “INSERT INTO links (fName, sName, cat, email, title, description, site, recip_url, added, check) VALUES
(’$fName’, ‘$sName’, ‘$catID’, ‘$email’, ‘$title’, ‘$description’, ‘$url’,’$userrecipURL’, ‘$listDate’, ‘0’)”;

    if (mysql_query ($queryStr))
    {
      ...
    }
    elseif (mysql_errno()==1169)
    {
      echo "The URL you have entered is already present!";
    } 
    else
    {
      echo 'Error: ', mysql_errno (), ': ', mysql_error ();
    } 

[/php]

Is that all I had to do

:oops: :oops: :oops:

After all that it took 2 minutes to change the field from text to varchar and make it unique.

Sponsor our Newsletter | Privacy Policy | Terms of Service