Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.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
Javascript 使用mysql表进行表单验证_Javascript_Php_Mysql - Fatal编程技术网

Javascript 使用mysql表进行表单验证

Javascript 使用mysql表进行表单验证,javascript,php,mysql,Javascript,Php,Mysql,伙计们,我是php新手,在验证文本框方面遇到了麻烦。我必须创建一个学生数据库及其能够插入/删除/查看/更新选项的接口。假设现在我试图从数据库中删除一名学生,我应该输入要删除的注册号。在这种情况下,如果我输入了一个不在数据库中的注册号,那么文件也会重定向到成功页面。我已经对登记号码的非零输入进行了验证。现在我的问题是,如果我想删除一个不在数据库中的学生,它应该被重定向到一个错误页面。这应该使用php、javascript和mysql来完成。我该怎么做 这是我的删除学生代码: <!DOCTYP

伙计们,我是php新手,在验证文本框方面遇到了麻烦。我必须创建一个学生数据库及其能够插入/删除/查看/更新选项的接口。假设现在我试图从数据库中删除一名学生,我应该输入要删除的注册号。在这种情况下,如果我输入了一个不在数据库中的注册号,那么文件也会重定向到成功页面。我已经对登记号码的非零输入进行了验证。现在我的问题是,如果我想删除一个不在数据库中的学生,它应该被重定向到一个错误页面。这应该使用php、javascript和mysql来完成。我该怎么做

这是我的删除学生代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Students Database</title>
<meta author="" content="">
<link rel="stylesheet" type="text/css" href="view.css" media="all">
</head>
<body id="main_body" >
    <img id="top" src="top.png" alt="">
    <div id="form_container">
    <h1><a>Students Database</a></h1>
    <form name="admin4" class="appnitro"  method="post" action="delete1.php">
    <div class="form_description">
    <center><h2>Students Database</h2></center>
    <p><center><font size='3'>
    <?php
    $con=mysqli_connect("localhost","admin","123456","mop");
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM student");
    echo "<table border='1'>
    <tr>
    <th>Register No</th>
    <th>Name &nbsp &nbsp &nbsp </th>
    <th>Department &nbsp </th>
    <th>Class &nbsp </th>
    </tr>";
    while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['RegNo'] . "</td>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Department'] . "</td>";
    echo "<td>" . $row['Class'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    mysqli_close($con);
    ?>
    </center></font></p>
    </div>  
    <b>Enter Register Number <font color='red'>*</font> </b> <input type="text" name="regno"><p>
    <ul >
    <center><li class="buttons">
    <input type="hidden" name="form_id" value="768845" />
    <input id="saveForm" class="button_text" type="submit" name="submit" value="Delete" /></center>
    </li>
    </ul>
    </form> 
    </div>
    <img id="bottom" src="bottom.png" alt="">
</body>
</html>

学生数据库
学生数据库
学生数据库

输入注册号*
以上为接口代码,以下为加工件代码:

<?php
$con=mysqli_connect("localhost","admin","123456","mop");
if ($_POST['regno'] == '')
    {
    header("location:admin4_2.php");
    }
    else
    {
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$sql1="DELETE from student where regno = ".intval($_POST["regno"]);
if (!mysqli_query($con,$sql1))
  {
  die('Error: ' . mysqli_error($con));
  }
  else
  {
    header("location:admin6_1.php");
  }
  }
mysqli_close($con);
?>


如你所见,我这样做是为了检查非零输入。是否可以使用表格检查有效的寄存器号?有谁能详细解释一下如何使用这些代码,因为我是php新手,这是我的第一个项目。我不介意使用javascript或php。如果正在使用javascript,是否有人也可以向我展示javascript代码???

如果您基于以下内容,它将检查您的数据库中是否存在注册号

这是一个基本的例子。修改以满足您的需要

HTML表单

输入注册号*
PHP(如果_存在。PHP)
这是令人困惑的>>>“现在我的问题是,如果我想删除一个不在数据库中的学生,它应该弹出一个错误页面”如果你想检查一个用户是否存在,你可以使用这样的
如果(mysqli_num_rows($query)>0){echo“user exists”}
对不起,伙计们!!我的意思是我应该被重定向到一个页面,上面写着inavlid注册号!!如果($\u POST['regno']='')
,这不是你现在使用的
吗?我已经验证了它是否为非零输入,先生,我现在想检查输入的寄存器号是否在表中。如果不存在,我希望被重定向到错误页面,否则将被重定向到成功页面
<form method="post" action="if_exists.php">
<b>Enter Register Number <font color='red'>*</font> </b> <input type="text" name="regno"><p>
<ul>
<center><li class="buttons">
<input id="saveForm" class="button_text" type="submit" name="submit" value="Check if Exists" /></center>
</li>
</ul>
</form>
<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";

$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
  die('Connection failed [' . $db->connect_error . ']');
}

$regno=mysqli_real_escape_string($db,$_POST['regno']);

// $query = $db->query("SELECT regno FROM student WHERE regno='$regno'");
$query = $db->query("SELECT * FROM student WHERE regno='$regno'");

if(mysqli_num_rows($query) > 0){
    echo "Registration number already exists.";
}else{
// echo "Sorry";
header("Location: redirection_page.php");
}