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
https://forums.phpfreaks.com/topic/224286-insert-adds-two-identical-lines/
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.

More information would be needed if wanted help.

 

The code you posted is incomplete and just a bunch of echoed "text" and partial select statements.

 

Can't even tell what or where $var would be anywhere in there.

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());
        
             } } }    ?>

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

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.