Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中完成验证?_Php_Mysql_Validation - Fatal编程技术网

如何在php中完成验证?

如何在php中完成验证?,php,mysql,validation,Php,Mysql,Validation,我只需要在完成此验证时得到帮助,php会检查用户提交表单时是否存在任何错误: $errors = array(); if (!$getcourseid){ $errors[] = "You must enter in Course's ID"; }else if (!$getcoursename){ $errors[] = "You must enter in Course's Name"; }else if (!$getduration)

我只需要在完成此验证时得到帮助,php会检查用户提交表单时是否存在任何错误:

$errors = array();


      if (!$getcourseid){
          $errors[] = "You must enter in Course's ID";
  }else if (!$getcoursename){
      $errors[] = "You must enter in Course's Name";
  }else if (!$getduration){
          $errors[] = "You must select Course's Duration";
      }     

      if(!$errors) {

     $insertsql = "
    INSERT INTO Course
        (CourseNo, CourseName, Duration)
      VALUES
        (?, ?, ?)
    ";
    if (!$insert = $mysqli->prepare($insertsql)) {
      // Handle errors with prepare operation here
    }                                           

    $insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);

    $insert->execute();

    if ($insert->errno) {
      // Handle query error here
    }

    $insert->close();

     // don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();  

          }

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td></td>
  <td id='errormsg'>$errormsg</td>
  </tr>
  <tr>
  <td>Course ID:</td>
  <td><input type='text' name='courseid' value='$getcourseid' /></td>
  </tr>
  <tr>
  <td>Course Name:</td>
  <td><input type='text' id='nameofcourse' name='coursename' value='$getcoursename' /></td>
  </tr>
  <tr>
  <td>Duration (Years):</td>
  <td>{$durationHTML}</td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Create Course' name='createbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;

逻辑是这样的,假设您有一系列错误,您可以这样做

if(empty($errormsg)) {
   //Go Ahead
} elseif(!empty($errormsg)) {
  //if(isset($errormsg['index'])) {
     echo 'Error'; 
  }
}
详细说明

假设你有3个错误,比如

$errors[0] = 'Username is invalid';
$errors[1] = 'Password is invalid';
$errors[2] = 'Third error';

if(empty($errors)) {
   //Go Ahead
} else {
    if(isset($errors[0])) {
       echo $errors[0]; 
    } elseif (isset($errors[1])) {
       echo $errors[1];
    } elseif (isset($errors[1])) {
       echo $errors[1]; 
    }
}
尝试使用jQuery:

test.php中的

...
$data['status'] = 1; // Status of the operation (1: Ok, 0: Error)
$data['msg'] = $msg; // Error message or success
header('Content-Type: application/json');
echo json_encode($data);
在test.js中:

$("#send").click(function () {
    $.post('test.php', $('#yourForm').serialize(), function(data){
        if(data.status == 1){
            $('#result').show().addClass('success').html(data.msg);
        }
        else{
            $('#result').show().addClass('error').html(data.msg);
        }
    }, "json");
    return false;
});
<form method="post" id="yourForm">
...
<input id="send" type="button"/>
<span id="result"></span>
</form>
.success{
    color: blue;
}
.error{
    color: red;
}
test.html中的

$("#send").click(function () {
    $.post('test.php', $('#yourForm').serialize(), function(data){
        if(data.status == 1){
            $('#result').show().addClass('success').html(data.msg);
        }
        else{
            $('#result').show().addClass('error').html(data.msg);
        }
    }, "json");
    return false;
});
<form method="post" id="yourForm">
...
<input id="send" type="button"/>
<span id="result"></span>
</form>
.success{
    color: blue;
}
.error{
    color: red;
}
希望这能有所帮助


尊敬。

您好,如果您使用更新我的代码,这样我就可以看到我需要为它做什么,因为我仍然感到困惑,那么该逻辑会将每个错误存储在
$errormsg
变量中吗?该回显“Error”应该是
发生了错误…
消息,还是应该是输出的错误数组中的个人错误?在页面顶部,我还有
$errormsg=(isset($errormsg))$errormsg:“”所以我猜我不需要你评论的isset?@user1881090编辑了我的答案,希望你现在明白我现在明白了,只需要在我在页面顶部的if语句中询问我是否需要更改$errors[]。。。到$errors[1]…..,然后是$errors[2]等等。如果不指定索引号,它会自动索引0,1,2,3,您还可以指定特定的索引名,如
$error['inval_username'
而不是
$error[0]这没有任何意义哦不知道,这很酷。最后一个小问题,错误消息
哪里发生了错误…
go?我已经记下了你的答案