如果id文本字段的值不在表单中,如何显示错误消息?

时间:2017-05-01 06:03:16

标签: php mysqli

代码:

if(isset($_POST["add"]))
{
extract($_POST);

$facilities = $_POST['facilities'];
$chk=""; 

foreach($facilities as $chk1)  
 {  
    $chk .= $chk1.",";  
 } 

$exam = $_POST['exam'];
$chks=""; 

foreach($exam as $chked)  
 {  
    $chks .= $chked.",";  
 } 

  $filename = $_FILES['college_image']['name'];
  $path = "college_banner/";
  $move=move_uploaded_file($_FILES['college_image']['tmp_name'],$path.$_FILES['college_image']['name']);

  $filename2 = $_FILES['logo']['name'];
  $path2 = "college_logo/";
  $move=move_uploaded_file($_FILES['logo']['tmp_name'],$path2.$_FILES['logo']['name']);

 echo "insert into all_colleges(college_name,establish,approve,affiliated,address,website,about_us,city,courses,logo,college_image,phone,field,tag,video1,video2,facilities,courses_fee,short_name,state,exams,form_fee,college_commission,client_commission,form_type)values('$name','$establish','$approve','$affiliated','$address','$website','$about_us','$city','$course','$filename2','$filename','$phone2','$field','$tag','$video1','$video2','$chk','$fee','$short','$state','$chks','$form_fee','$college_commission','$client_commission','$form_type')";
$result = mysqli_query($link,$sql2);
if($result == true)
{
  $msg .= "<h5 style='color:green;'>Successfull</h5>";
}
else
{
  $msg .= "<h5 style='color:red;'>Error</h5>";
}
}

在数据库中我有一个auto_increment id,即college_id但是当我在college_id上​​删除auto_increment时,它会在点击添加按钮而不是错误时显示成功消息。那么,我该如何解决这个问题?

谢谢

1 个答案:

答案 0 :(得分:0)

与您college_id无关,只要确定它是AUTO_INCREMENT,那么它必须是PRIMARY KEY

在SQL查询中,您没有为$sql2变量分配查询,请使用

echo $sql2 = "insert into all_colleges ...";
//---^ assign query in $sql2
$result = mysqli_query($link,$sql2); // <-- you are passing $sql2

如果不使用循环来检查单个语句中的选中值,可以使用implode()还有一件事,例如,

$chk= = implode(',',$_POST['facilities']);