如何使用mysql数据库验证get方法表单

dauxcl2d  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(407)

我想通过mysql数据库检查udise代码是否已经存在…我的代码如下。。。如何验证此字段?请帮忙

  1. <?php
  2. include('logprocess.php') ?>
  3. <html>
  4. <head>
  5. <title>Login Form </title>
  6. <link rel="stylesheet" href="stylelogin.css">
  7. </head>
  8. <body>
  9. </body>
  10. <form action="regnew.php" method="GET" id="register_form">
  11. <h1>---- </br>
  12. <br>
  13. LOGIN</br></h1>
  14. <div>
  15. <input type="number" placeholder="Enter Your UDISE Code "maxlength="11" id="udise" name="udise">
  16. </div>
  17. <br>
  18. <div>
  19. <button type="submit" name="register" id="reg_btn">Enter</button>
  20. </div>
  21. </br>
  22. <span class="footer"></span>
  23. </form>
  24. </body>
  25. </html>

我的php代码如下。。我已将其保存为不同的文件,并使用include选项调用该文件。但什么都没用

  1. <?php
  2. // Grab User submitted information
  3. // Connect to the database
  4. $con = mysql_connect("localhost","root","");
  5. // Make sure we connected successfully
  6. if(! $con)
  7. {
  8. die('Connection Failed'.mysql_error());
  9. }
  10. $udise = "";
  11. // Select the database to use
  12. $db = mysql_select_db("workspace",$con);
  13. if (isset($_POST['register'])) {
  14. $sql_udise = "SELECT * FROM udise WHERE schcd='$udise'" or die (mysql_error());
  15. $res_udise = mysqli_query($db, $sql_udise) or die(mysql_error());
  16. $row = mysqli_fetch_assoc($res_udise);
  17. $fields = array( 'udise');
  18. $error = false; //No errors yet
  19. foreach($fields AS $fieldname) { //Loop trough each field
  20. if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
  21. echo '<script language="javascript">
  22. alert("Pls Enter All Fields");
  23. </script>'; //Display error with field
  24. $error = true; //Yup there are errors
  25. }
  26. }
  27. if(!$error) {
  28. if($row["udise"]==$udise ){
  29. echo '<script language="javascript">
  30. top.location.href = "updatewp.php"; //the page to redirect
  31. </script>';
  32. }else {
  33. echo '<script language="javascript">
  34. alert("Sorry... UDISE Mismatch pls try again ");
  35. href = "login.php"; //the page to redirect
  36. </script>';}
  37. {
  38. echo '<script language="javascript">
  39. href = "login.php"; //the page to redirect
  40. </script>';
  41. }
  42. mysql_close($con);
  43. }}
  44. ?>

如何将此添加到登录表单验证

jhkqcmku

jhkqcmku1#

试试这个代码。

  1. <html><head><title>Login Form </title><link rel="stylesheet" href="stylelogin.css"></head><body></body><form method="GET" id="register_form"><h1>---- </br><br> LOGIN</br</h1><div>
  2. <input type="number" placeholder="Enter Your UDISE Code "maxlength="11" id="udise" name="udise">
  3. </div>
  4. <br>
  5. <div>
  6. <button type="submit" name="register" id="reg_btn">Enter</button>
  7. </div>
  8. </br><span class="footer"></span></form><?php if(isset($_GET['register'])){$udise = $_GET['udise'];
  9. //connect to the database $servername = 'localhost';
  10. $username = 'root';
  11. $password = '';
  12. $db = 'yourdb';
  13. $conn = new mysqli($servername, $username, $password, $db); // Check connection if ($conn->connect_error) {
  14. die("Connection failed: " . $conn->connect_error);}
  15. $check=mysqli_query($conn,"select * from udise where udise='$udise'");
  16. $checkrows=mysqli_num_rows($check); if($checkrows>0) {
  17. echo '<div style="color: #a94442;
  18. background-color: #f2dede;
  19. border-color: #ebccd1;padding: 15px;
  20. margin-bottom: 20px;
  21. border: 1px solid transparent;
  22. border-radius: 4px;">
  23. already exist </div>'; } else {
  24. //insert results from the form input
  25. $query = "INSERT INTO udise(udise) VALUES('$udise')";
  26. $result = mysqli_query($conn, $query) or die('Error querying database.');
  27. mysqli_close($conn);
  28. echo '<div style="color: #3c763d;
  29. background-color: #dff0d8;
  30. border-color: #d6e9c6;padding: 15px;
  31. margin-bottom: 20px;
  32. border: 1px solid transparent;
  33. border-radius: 4px;">
  34. POSTED </div>' ;
  35. }}?> </body> </html>
展开查看全部
e37o9pze

e37o9pze2#

试试这个代码。

  1. <?php session_start();
  2. //Your Mysql Config
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "yourdb";
  7. //Create New Database Connection
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9. //Check Connection
  10. if($conn->connect_error) {
  11. die("Connection Failed: ". $conn->connect_error);
  12. }
  13. if(isset($_GET['login']))
  14. {
  15. $sql = "SELECT * FROM udise WHERE udise='".$_GET['udise']."'";
  16. $result = $conn->query($sql);
  17. if ($result->num_rows > 0) {
  18. // output data of each row
  19. while($row = $result->fetch_assoc()) {
  20. $extra="profile.php";
  21. $_SESSION['login']=$_GET['udise'];
  22. $_SESSION['id']=$num['id'];
  23. echo "<script>window.location.href='".$extra."'</script>";
  24. exit();
  25. }
  26. } else {
  27. $_SESSION['action1']="*Invalid usercode";
  28. $extra="log.php";
  29. echo "<script>window.location.href='".$extra."'</script>";
  30. exit();
  31. }
  32. $conn->close();
  33. }
  34. ?>
  35. <form method="GET">
  36. <input type="number" placeholder="Enter Your UDISE Code "maxlength="11" id="udise" name="udise">
  37. <p style="color:#F00; padding-top:20px;" align="center"><?php echo $_SESSION['action1'];?><?php echo $_SESSION['action1']="";?></p>
  38. <input class="small button search-btn" type="submit" value="Log In" name="login">
  39. </form>
展开查看全部

相关问题