伙计们我是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       </th>
<th>Department   </th>
<th>Class   </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的代码??
答案 0 :(得分:1)
如果您基于以下内容,它将检查您的数据库中是否存在注册号。
这是基本示例。修改以满足您的需求。
<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");
}