(MYSQL)只有在表中不存在记录时才能插入记录

时间:2017-10-21 03:29:25

标签: php mysql select insert

嗨,也许这个问题已经得到解答,但没有任何帮助。

我使用Mysql并且我想在表中插入一些数据,但我想在插入之前检查重复的记录。

我对如何实现这一点非常困惑。

我需要检查" RFC == $ Expediente AND Diagonal == 10或Diagonal == 20"并显示类似"记录已存在",  或者如果记录不存在则插入所有数据

这是我的剧本:

<?php
$Person_name=$_POST['Person_name'];
$Expediente=$_POST['Expediente'];
$Sexo=$_POST['Sexo'];
$Edad=$_POST['Edad'];
$Domicilio=$_POST['Domicilio'];
$numero=$_POST['Numero'];
$colonia=$_POST['Colonia'];
$Ciudad=$_POST['Ciudad'];
$Parentesco=$_POST['Parentesco'];
$ClinicaAds=$_POST['ClinicaAds'];
$Dependencia=$_POST['Dependencia'];
$Entidad=$_POST['Entidad'];
$Foraneo=isset($_POST['Foraneo']) ? 1 : 0;

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "issste";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "  INSERT INTO paciente (RFC, Nombre, Sexo, FechaNacimiento, Calle, NumeroDomiciliario, Colonia, Ciudad, Dependencia, ClinicaAdscrip, Entidad, Foraneo, Diagonal)  
                SELECT '$Expediente', '$Person_name', '$Sexo', '$Edad', '$Domicilio', '$numero', '$colonia', '$Ciudad', '$Dependencia', '$ClinicaAds', '$Entidad', '$Foraneo', '$Parentesco' FROM paciente  
                WHERE NOT EXIST(  
                 SELECT RFC, Diagonal FROM paciente WHERE RFC = '$Expediente' AND Diagonal='10' OR Diagonal='20') LIMIT 1 ";  

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

当我运行我的程序时,我收到此错误

&#34;你的sql语法有错误,请查看与你的mysql服务器版本对应的手册&#34;

1 个答案:

答案 0 :(得分:0)

修改了您的查询,请尝试以下操作:

enctype="multipart/form-data"