drzrdc1766788 2014-01-26 04:21
浏览 49
已采纳

使用mysql表进行表单验证

guys i am new to php and have a trouble about validation of a text box. i have to create a student database and its interface capable of insert/delete/view/update option. say now am trying to delete a student from the database i should enter the register number to delete. in this case, if i enter a register number not in the database then too the file redirects to success page. i have made validation for non-zero entry of register number. now my problem is if i want to delete a student who is not in the database it should be redirected to a error page. this should be done using php, javascript and mysql. how am to do this?

here is my delete student code:

<!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>

the above is the interface code and here is the processing part code:

<?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);
?>

as u see i have done it to check for non-zero entry. is it possible to check with table for valid register number? can anyone explain me how to do in detail with the code because am new to php and this is my first project. i dont mind using javascript or php for it. if javascript is being used can anyone also show me the code of javascript too??

  • 写回答

1条回答 默认 最新

  • dongne1560 2014-01-26 15:23
    关注

    If you base yourself on the following, it will check if a registration number exists in your database.

    This is a basic example. Modify to suit your needs.

    HTML form

    <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 (if_exists.php)

    <?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");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题