插入重复数据

时间:2019-05-27 13:27:16

标签: php

当我插入数据时,我的选择查询不起作用,并且数据直接插入。我的搜索查询无法正常工作,但直接插入了数据。请帮助我。如何启动我的Serach查询,请检查代码。

session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])=="")
    {   
    header("Location: index.php"); 
    }
    else{
if(isset($_POST['submit']))
{
$classname=$_POST['classname'];
$classnamenumeric=$_POST['classnamenumeric']; 
$section=$_POST['section'];
$q="SELECT * FROM tblclasses Where (ClassName='$classname' AND Section='$section');";
      $res=mysqli_query($dbh,$q);
      if (mysqli_num_rows($res) > 0) {
        $row = mysqli_fetch_assoc($res);
        if($classname==$row['ClassName'] &&  $section==$row['Section'])
        {
         echo '<script>alert("already exists")</script>';
        }

}else{
$sql="INSERT INTO  tblclasses(ClassName,ClassNameNumeric,Section) VALUES(:classname,:classnamenumeric,:section)";
$query = $dbh->prepare($sql);
$query->bindParam(':classname',$classname,PDO::PARAM_STR);
$query->bindParam(':classnamenumeric',$classnamenumeric,PDO::PARAM_STR);
$query->bindParam(':section',$section,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
$msg="Class Created successfully";
}
else 
{
$error="Something went wrong. Please try again";
}
}
}
?>```

0 个答案:

没有答案