简体   繁体   中英

insert value to table if row doesnt exist

I have database that hold my data in mysql. I have already data in my table (call words) and I want to insert new data to this table but before I want to check if this data not already exist. I have function that insert the data to table but I need sql query that will check if the data not exist? the colum in my table 'words' are :word , num , hit , instoplist. I write the code in PHP

Thanks,

this is my code:(insert to table function)

function insert($myWords)
    {
        global $conn;
        $temp1 = $value['document'];
        $temp2 = $value['word'];

          $sql = "INSERT INTO words (word,num,hit,instoplist) VALUES";
             foreach ($myWords as $key => $value) {
                $word = $value['word'];
                $number = $value['document'];
                $hit = $value['hit'];
                $stop = $value['stopList'];        
                $sql .= "('$word', '$number', '$hit','$stop'),";                 
            }
            $sql = rtrim($sql,','); //to remove last comma
            if($conn->query($sql)!== TRUE)
            {
                        echo "error". $conn->error;
            }
    }

Before inserting the data make a select query on a column which is unique as per your requirement like:

$chkExist = "select id from table where col_name = '".$value."'";
$res = $conn->query($chkExist);

// Now check if there is some record in $res than stop the entry otherwise insert it
function insert($myWords)
{
    global $conn;
    $temp1 = $value['document'];
    $temp2 = $value['word'];

      $sql = "INSERT INTO words (word,num,hit,instoplist) VALUES";
         foreach ($myWords as $key => $value) {
           $sql2 "SELECT * FROM words WHERE word = '".$value['word']."' OR num = '".$value['document']."'"; //other data if you want
           $resultat=mysql_query($query);
           if($resultat==""){
              $word = $value['word'];
              $number = $value['document'];
              $hit = $value['hit'];
              $stop = $value['stopList'];        
              $sql .= "('$word', '$number', '$hit','$stop'),";  
           }               
        }
        $sql = rtrim($sql,','); //to remove last comma
        if($conn->query($sql)!== TRUE)
        {
                    echo "error". $conn->error;
        }
}
 $sel="select * from words where word = '$word' AND document = '$document' AND hit = '$hit' AND stopList ='$stopList'";
  $qry=mysqli_query($sel);
  $num=mysqli_num_rows($qry);
 if($num==0){
     $sql = "INSERT INTO words (word,num,hit,instoplist) VALUES";
         foreach ($myWords as $key => $value) {
            $word = $value['word'];
            $number = $value['document'];
            $hit = $value['hit'];
            $stop = $value['stopList'];        
            $sql .= "('$word', '$number', '$hit','$stop'),";                 
        }
        $sql = rtrim($sql,','); 
  }else{
      echo "Already Exist";
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM