Jump to content

INSERT adds two identical lines


User_not_loser

Recommended Posts

Hello, Experts! Need you advice.

 

Here is the code:

 

<?php

if ($var == "")  {
        echo "text";
   }
     if ($var == "*")  {
        echo "text"; }
   else if ($var != "")
   {
        
$query = "SELECT * FROM bd WHERE ....."; 
$data = mysql_query($query) or die(mysql_error());
       $anymatches=mysql_num_rows($data);
        if ($anymatches != 0) {
       echo "text";
        while($row = mysql_fetch_array($data))
        {
            echo "text";      
        }    
         } 
        if ($anymatches == 0) {      
         $query2 = "SELECT * FROM bd WHERE....."; 
      $data2 = mysql_query($query2) or die(mysql_error());
       $anymatches2=mysql_num_rows($data2);
       if ($anymatches2 != 0) {             
echo "text"
       else
       {
              
          mysql_query("INSERT INTO notfound (notfound) VALUES ('$var')") or die(mysql_error());    
           } }  } ?>

 

The last line adds two identical lines to my table. Do I run it twice? Thanks in advance!

Link to comment
Share on other sites

Try this

$query = "SELECT * FROM bd WHERE ....."; 
$data = mysql_query($query) or die(mysql_error());
$anymatches=mysql_num_rows($data);
if ($anymatches != 0) {
echo "text";
    while($row = mysql_fetch_array($data))
{
    	echo "text";      
    }    
} 
if ($anymatches == 0) {      
$query2 = "SELECT * FROM bd WHERE....."; 
$data2 = mysql_query($query2) or die(mysql_error());
$anymatches2=mysql_num_rows($data2);
if ($anymatches2 != 0) 
{             
	echo "text";
}
    else
    {
    	mysql_query("INSERT INTO notfound (notfound) VALUES ('$var')") or die(mysql_error());    
    } 
}

Looked like your {}'s where messed up.

Link to comment
Share on other sites

Here is the whole thing:

 

<?php

if ($var == "")  {
        echo "<center>Empty</center>";
   }

    else if ($var != "")   {
  
$query = "SELECT * FROM bd1 WHERE word LIKE '$var'"; 
$data = mysql_query($query) or die(mysql_error());
       $anymatches=mysql_num_rows($data);
        if ($anymatches != 0) {
        while($row = mysql_fetch_array($data))
        {
            echo "{$row['word']}";
                      
        } }
        if ($anymatches == 0) {      
         $query2 = "SELECT * FROM bd2 WHERE word LIKE '$var'"; 
      $data2 = mysql_query($query2) or die(mysql_error());
       $anymatches2=mysql_num_rows($data2);
       if ($anymatches2 != 0) {             
         while($row = mysql_fetch_array($data2))
        {
            echo "{$row['word']}";
                      
        }  }
        else 
       {
              mysql_query("INSERT INTO newwords (notfound) VALUES ('$var')") or die(mysql_error());
        
             } } }    ?>

Link to comment
Share on other sites

Ok, I've gone over it again And came up with this

<?php

if (empty($var)) echo "<center>Empty</center>";
else
{
//Search bd1 for var.
$query = "SELECT * FROM bd1 WHERE `word`='$var'"; 
$data = mysql_query($query) or die(mysql_error());
    if (1>mysql_num_rows($data)) 
    {      
    	//If there's no results from bd1 search bd2
    	$query = "SELECT * FROM bd2 WHERE `word`='$var'"; 
      	$data = mysql_query($query) or die(mysql_error());
    }
    if (0<mysql_num_rows($data))
    {
    	//If there where results found in either bd1 or bd2, print them out.             
       while($row = mysql_fetch_array($data2)) echo "{$row['word']}";
    }
    //Else insert a log into the new words table.
else mysql_query("INSERT INTO `newwords (notfound)` VALUES ('$var')") or die(mysql_error());	
//Not sure if your table is called 'newwords (notfound)' or just 'newwords' Maybe just use this?
// else mysql_query("INSERT INTO `newwords` VALUES ('$var')") or die(mysql_error());	
}    
?>

I condensed it a bit to get rid of braces,

Assuming you have a html form to search for a word ($var) if you want $var to match what's in the database EXACTLY you should be using

WHERE `word`='$var'

in your querys.

If you want to "search" for var. for example you enter "ttocs" in the form it will match the row with "ttocskcaj" use

WHERE `word`LIKE '%$var%'

Note the % wildcards.

Hope this helps :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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