如何回应从php中的查询生成的空条目禁止消息?

时间:2015-03-18 10:08:14

标签: php mysqli

我有一个表单,在提交时会检查此查询 - >

if(isset($_POST['update']) && !empty($_POST['name']) && !empty($_POST['reg_name']))

我想回复一条消息“请填写所有必填字段。”如果没有填写必填字段。

简而言之,它应该突出显示未填写的字段名称。

完整代码:

include ('database/abcd.php');

if ($con->connect_error) 
{
    die("Connection failed: " . $con->connect_error);
} 

if(isset($_POST['update']))

{

$error = array();
if(empty($_POST['name']))
    $error[] = 'Please fill name field';
if(empty($_POST['reg_name']))
    $error[] = 'Pleae fill reg_name field';
if(count($error) < 1)
{
    $name = $_POST['name'];
    $reg_name = $_POST['reg_name'];
    $established = $_POST['established'];
    $industry = $_POST['industry'];
    $about = $_POST['about'];
    $website = $_POST['website'];
    $mail =  $_POST['mail'];
    $phone = $_POST['phone'];
    $address =  $_POST['address'];
    $city = $_POST['city'];
    $facebook = $_POST['facebook'];
    $wiki = $_POST['wiki'];
    $twitter = $_POST['twitter'];
    $google = $_POST['google'];
    $member_username = $_SESSION['username'];

    $process="INSERT INTO notifications (member_username, process, icon, class) VALUES ('$_POST[member_username]','$_POST[process]','$_POST[icon]','$_POST[class]')";

    if (!mysqli_query($con,$process))
    {
      die('Error: ' . mysqli_error($con));
    }


    $sql = "UPDATE `company_meta` SET `name` = '$name', reg_name = '$reg_name', wiki = '$wiki', established = '$established', industry = '$industry', about = '$about', website = '$website', mail = '$mail', phone = '$phone', address = '$address', city = '$city', facebook = '$facebook', twitter = '$twitter', google = '$google' WHERE `member_username` = '$member_username'";

    if ($con->query($sql)) 
    {
        header('Location: edit.php');           
    } 
}

else
{  
    $errors = implode(',' $error); 
  echo $errors;   
}

    $con->close();
}

2 个答案:

答案 0 :(得分:0)

我认为您传递的名称或reg_name是首先检查。可能是名称或reg_name可以内容空格,以便它不显示消息,否则上面的代码工作正常..

答案 1 :(得分:-1)

if(isset($_POST['update'])) // This first check whether it is an update call
{
   $error = array();  // Here we initialize an array so that we can put the messages in it.

   if(empty($_POST['name'])) // If the name field is empty, push a message in $error array.
    $error[] = 'Please fill name field';

   if(empty($_POST['reg_name'])) // Same as above field
    $error[] = 'Pleae fill reg_name field';

   if(count($error) < 1) // Now this checks if the $error array has no value. If it has value it means that either or both of the above fields are empty and the else block will be executed.
   {
      // Submit your form
   }
   else
   {
      $errors = implode(',' $error); 
      echo $errors;
   }
}
else
{
    // Update not triggered.
}