这是我的php代码。如何使用PHP检查这3个参数(用户,agwera,vis)是否存在于MySQL中?如果已经存在,则不要再添加第二次。 +想要echo "Already in Exists<br/>";
如何使用PHP检查数据是否已存在于MySQL中
<?
sleep(10);
define ('DBHOST', 'yyy');
define ('DBNAME', 'xxxx');
define ('DBUSER', 'xxxx');
define ('DBPASS', 'xxxxx');
mysql_pconnect(DBHOST, DBUSER, DBPASS) or die('DB');
mysql_select_db(DBNAME) or die ('USER');
mysql_query('SET NAMES utf8');
header('Content-Type: text/html; charset=utf-8');
error_reporting (0);
$act = isset($_GET['act']) ? $_GET['act'] : '';
switch($act)
{
case 'send':
$destination = $_POST['adress'];
$signature = $_POST['text'];
$text = "$signature ";
if (empty($_POST["text"]) or strlen($_POST["text"])==0){
@header("location:error5.php");
break;
}
$str = $_POST['text'];
if (strlen($str)>160){
@header("location:error4.php");
break;
}
if (!preg_match('/^[0-9]{9}$/',$destination))
{
@header("location:error1.php");
break;
}
$str2 = $_POST['sender'];
if (strlen($str2)>11){
@header("location:error2.php");
break;
}
if (empty($_POST["sender"]) or strlen($_POST["sender"])==0){
@header("location:error3.php");
break;
}
else
{
$password = 'zzzzzz';
$destination = $_POST['adress'];
$nomeri = "995$destination";
$nomeri2 = $_POST['sender'];
$source = $nomeri2;
$signature = $_POST['text'];
$text = "$signature ";
$content =
'key='.rawurlencode($password).
'&destination='.rawurlencode($nomeri).
'&sender='.rawurlencode($source).
'&content='.rawurlencode($text);
$a = file_get_contents('http://example.com/api/send.aspx?'.$content);
@header("location:ok.php");
mysql_query("INSERT `gagzavnili` SET
`user` = '".$source."',
`agwera` = '".$text."',
`vis` = '".$destination."',
`time_upload` = '".time()."',
`time` = '".time()."'") or die(mysql_error());
$file_id = mysql_insert_id();
}
}
?>
编写一个select查询来查找带有值(user,agwera,vis)的行,并检查结果集的行数是否大于0,那么它表示已经存在。并看到一些教程 –
你想看看'INSERT IGNORE':http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql – BenOfTheNorth