如何检查数据库中是否存在字符串,如果是,则重做

时间:2014-09-09 07:31:20

标签: php mysql

所以我创建了一个粘贴系统,当我插入数据库时​​,我需要检查唯一代码是否已经存在,如果是,请生成一个新代码,到目前为止我的代码:

 <?php

if (isset($_POST["snip"]) && !empty($_POST["snip"])) {

mysql_connect("localhost", "root", "")or die("cannot connect"); 
mysql_select_db("ezsnip")or die("cannot select DB");



  $snip = $_POST['snip'];
  $userIP = $_SERVER['REMOTE_ADDR'];
  $language = $_POST['language'];
  $userID = '1';
  $length = 4;
  $rand = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);

  $snip = mysql_real_escape_string($snip);
  $userIP = mysql_real_escape_string($userIP);
  $rand = mysql_real_escape_string($rand);
  $language = mysql_real_escape_string($language);


$sql="INSERT INTO snips(ID, Snip, Language, UserID, UniqueID, IP)VALUES('', '$snip', '$language', '$userID', '$rand', '$userIP')";
$result=mysql_query($sql);
mysql_close();
if($result){
//Good to go
} else {
//Error
    }
} else {
 echo 'no';
}
?> 

插入安全的方式是什么?我是新手,所有信息都会有所帮助。

0 个答案:

没有答案