我有数据库在mysql中保存我的数据。 我的表中已有数据(调用字),我想在此表中插入新数据,但在我想检查此数据是否已存在之前。 我有将数据插入表的函数,但是我需要sql查询来检查数据是否不存在? 在我的表格中的字号'字样'是:word,num,hit,instoplist。 我用PHP编写代码
谢谢,
这是我的代码:(插入表函数)
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;
}
}
答案 0 :(得分:0)
在插入数据之前,根据您的要求对列进行选择查询,如:
$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
答案 1 :(得分:0)
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;
}
}
答案 2 :(得分:-1)
$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";
}