gpt4 book ai didi

php - 如何使用 PHP 检查数据是否已存在于 MySQL 中

转载 作者:行者123 更新时间:2023-11-29 12:54:50 24 4
gpt4 key购买 nike

这是我的 PHP 代码。如何用PHP检查MySQL中是否存在这3个参数(user、agwera、vis)?如果已经存在,请不要第二次添加。 + 想要 echo "Already in Exists<br/>";

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



}
}
?>

最佳答案

$data = array('user', 'agwera', 'vis');

foreach( $data as $val ) {
$res = mysql_query("SELECT * FROM `table_name` WHERE `column_name` = '$val'");
$count = mysql_num_rows($res);

if( $count > 0 ) {
echo "<script>alert('Already Exists')</script>";
} else {
//insert query for $val...
}
}

关于php - 如何使用 PHP 检查数据是否已存在于 MySQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com