在php中删除时重新加载插入页面

时间:2017-03-22 04:23:51

标签: php jquery html mysql

这里我要删除数据库记录,当我删除记录时,记录删除成功但是它重新加载insert_search页面显示结果并且删除的结果不会去,所以我需要输入每次在url中使用insert_search获取更新结果时,请告诉我需要重新加载页面以及如何实现它,请提前感谢...

<?php
$user     = "root";
$server   = "localhost";
$password = "";
$db       = "coedsproddb1";
$dbconn   = mysql_connect($server, $user, $password);
mysql_select_db($db, $dbconn);
?>
  <html>

  <head>
    <title>Insert</title>
    <link rel="stylesheet" href="css/jquery-ui.css">
    <script src="js/jquery-1.12.4.js"></script>
    <script src="js/jquery-ui.js"></script>
    <style>
      #display {
        color: red;
        font-size: 12px;
        text-align: center;
      }

      .logo {
        padding: 5px;
        float: left;
      }

      header {
        background-color: #074e7c;
        height: 60px;
        width: 100%;
        text-align: center;
        color: white;
        font-size: 40px;
      }

      #wrap {
        text-align: center;
      }

    </style>
  </head>

  <body>
    <header><img src="images/ipoint.png" class="logo" /> USER REGISTRATION</header>
    <div class="container">
      <h1 style="text-align:center">ADDING THE USER DETAILS</h1>
      <form name="useradd" id="useradd" action="#" method="post">
        <table align='center' border='1'>
          <tr>
            <td>
              <label for="userName">UserName</label>
            </td>
            <td>
              <input id="userName" name="userName" type="text" />
            </td>
          </tr>
          <tr>
            <td>
              <label for="userEmail">Email</label>
            </td>
            <td>
              <input id="userEmail" name="userEmail" type="text" />
            </td>
          </tr>
          <tr>
            <td>
              <label for="userPassword">password</label>
            </td>
            <td>
              <input id="userPassword" name="userPassword" type="password" />
            </td>
          </tr>
        </table>
        <br>
        <div id="wrap">
          <input type="submit" name="add" value="add" id="add">
          <input type="submit" name="update" value="update" id="update">
        </div>
      </form>
      <div id="display">
      </div>
    </div>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#add").click(function(e) {
          var userName = $("#userName").val();
          var userEmail = $("#userEmail").val();
          var userPassword = $("#userPassword").val();
          var dataString = 'userName=' + userName + '&userEmail=' + userEmail + '&userPassword=' + userPassword;
          alert(dataString);
          if (userName == "" || userEmail == "" || userPassword == "") {
            document.getElementById("display").innerHTML = "Please Enter The Fields";
          } else if (!validate1($.trim(userName))) {
            document.getElementById("display").innerHTML = "Please Enter The Valid UserName";
            document.getElementById("display").focus();
          } else if (!ValidateEmail($.trim(userEmail))) {
            document.getElementById("display").innerHTML = "Please Enter The Valid Emailid";
            document.getElementById("display").focus();
          } else {
            $.ajax({
              type: "POST",
              url: "insert.php",
              data: dataString,
              cache: false,
              success: function(result) {
                //alert("submitted"+result);
                $('#display').html(result);
              },
              error: function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
              }
            });
          }
          e.preventDefault();
        });

        function validate1(userName) {
          var u = userName;
          var filter = /^[a-zA-Z0-9]+$/;
          if (filter.test(u)) {
            return true;
          } else {
            return false;
          }
        }

        function ValidateEmail(userEmail) {
          var e = userEmail;
          var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
          if (filter.test(e)) {
            return true;
          } else {
            return false;
          }
        }
      });

    </script>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#update").click(function(e) {
          alert("hi");
          var userName = $("#userName").val();
          var userEmail = $("#userEmail").val();
          var userPassword = $("#userPassword").val();
          var dataString = 'userName=' + userName + '&userEmail=' + userEmail + '&userPassword=' + userPassword;
          alert(dataString);
          if (userEmail == "" || userPassword == "") {
            document.getElementById("display").innerHTML = "Please Enter The Fields";
          } else {
            $.ajax({
              type: "POST",
              url: "user_update.php",
              data: dataString,
              cache: false,
              success: function(result) {
                //alert("submitted"+result);
                $('#display').html(result);
              },
              error: function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
              }
            });
          }
          e.preventDefault();
        });
      });

    </script>
  </body>

  </html>

insert.php

  <html>

  <head>
    <title>Insertion</title>
  </head>

  <body>
    <div id="display">
      <?php
include('db.php');
$userName     = mysql_real_escape_string($_POST['userName']);
$userEmail    = mysql_real_escape_string($_POST['userEmail']);
$userPassword = mysql_real_escape_string($_POST['userPassword']);
$regDate      = date("Y-m-d");
function generateCode($characters)
{
    $possible = '23456789abcdefghjkmnpqrstuvwxyz';
    $code     = '';
    $i        = 0;
    while ($i < $characters) {
        $code .= substr($possible, mt_rand(0, strlen($possible) - 1), 1);
        $i++;
    }
    return $code;
}
$registration_key = generateCode(10);
$str              = "insert into coeds_user(userName,userEmail,userPassword,regDate,registration_key) values('$userName','$userEmail','$userPassword','$regDate','$registration_key')";
echo $str;
$query = mysql_query($str);
if ($query) {
    $display = "Success";
} else {
    $display = "Failed";
}
$string = "select * from coeds_user";
$query2 = mysql_query($string);
$display .= "<table border='1'>";
$display .= "<tr><th>UserId</th><th>UserName</th><th>UserEmail</th><th>UserPassword</th><th>RegDate</th><th>RegistrationKey</th></tr>";
while ($result = mysql_fetch_array($query2)) {
    $display .= "<tr>";
    $display .= "<td>" . $result['userId'] . "</td>";
    $display .= "<td>" . $result['userName'] . "</td>";
    $display .= "<td>" . $result['userEmail'] . "</td>";
    $display .= "<td>" . $result['userPassword'] . "</td>";
    $display .= "<td>" . $result['regDate'] . "</td>";
    $display .= "<td>" . $result['registration_key'] . "</td>";
    $display .= "<td><a href='user_update.php?user_Id=" . $result['userId'] . "'>Edit</a></td>";
    $display .= "<td><a href='user_delete.php?user_Id=" . $result['userId'] . "'>Delete</a></td>";
    $display .= "</tr>";
}
$display .= "</table>";
location . reload();
echo $display;
?>
    </div>
  </body>

  </html>

user_delete.php

  <html>

  <head>
    <title>Deletion</title>
    <link rel="stylesheet" href="css/jquery-ui.css">
    <script src="js/jquery-1.12.4.js"></script>
    <script src="js/jquery-ui.js"></script>
    <style>
      #display {
        color: red;
        font-size: 12px;
        text-align: center;
      }

      .logo {
        padding: 5px;
        float: left;
      }

      header {
        background-color: #074e7c;
        height: 60px;
        width: 100%;
        text-align: center;
        color: white;
        font-size: 40px;
      }

      #wrap {
        text-align: center;
      }

    </style>
  </head>

  <body>
    <header> <img src="images/ipoint.png" class="logo" /> USER REGISTRATION</header>
    <div class="container">
      <h1 style="text-align:center">DELETE WINDOW</h1>
      <div id="display">
        <?php
include('db.php');
if (isset($_GET['user_Id'])) {
    $userid = $_GET['user_Id'];
}
?>
          <form action="user_delete.php" name="user_delete" method="post">
            <input type="hidden" name="user_Id" id="user_Id" value="<?php
if (isset($userid))
    echo $userid;
?>">
            <?php
include('db.php');
$s      = "delete from coeds_user where userId=$userid";
$query3 = mysql_query($s);
if ($query3) {
    $display = "Delete Is Successful";
} else {
    $display = "Delete Is Unsuccessful";
}
$string = "select * from coeds_user";
$query5 = mysql_query($string);
$display .= "<table border='1'>";
$display .= "<tr><th>UserId</th><th>UserName</th><th>UserEmail</th><th>UserPassword</th><th>RegistrationDate</th><th>RegistrationKey</th></tr>";
while ($res1 = mysql_fetch_array($query5)) {
    $display .= "<tr>";
    $display .= "<td>" . $res1['userId'] . "</td>";
    $display .= "<td>" . $res1['userName'] . "</td>";
    $display .= "<td>" . $res1['userEmail'] . "</td>";
    $display .= "<td>" . $res1['userPassword'] . "</td>";
    $display .= "<td>" . $res1['regDate'] . "</td>";
    $display .= "<td>" . $res1['registration_key'] . "</td>";
}
echo $display;
?>
      </div>
      </form>
    </div>
  </body>

  </html>

user_update.php

  <html>

  <head>
    <title>Updation</title>
  </head>

  <body>
    <div id="display">
      <?php
include('db.php');
if (isset($_GET['user_Id'])) {
    $userid = $_GET['user_Id'];
    echo $userid;
    $s = "select * from coeds_user where userId=$userid";
    echo $s;
    $query1 = mysql_query($s);
    $res    = mysql_fetch_array($query1);
?>
        <input type="hidden" name="userPassword" id="userPassword">
        <input type="hidden" name="userEmail" id="userEmail">
        <form action="user_update.php" name="user_update" method="post">
          <input type="hidden" name="user_Id" id="userId" value="<?php
    if (isset($userid))
        echo $userid;
?>">
          <table align='center' border='1'>
            <tr>
              <td>
                <label for="userName">UserName</label>
              </td>
              <td>
                <input id="userName" name="userName" type="text" value="<?php
    echo $res['userName'];
?> " />
              </td>
            </tr>
            <tr>
              <td>
                <label for="userEmail">Email</label>
              </td>
              <td>
                <input id="userEmail" name="userEmail" type="text" value="<?php
    echo $res['userEmail'];
?> " />
              </td>
            </tr>
            <tr>
              <td>
                <label for="userPassword">password</label>
              </td>
              <td>
                <input id="userPassword" name="userPassword" type="password" value="<?php
    echo $res['userPassword'];
?> " />
              </td>
            </tr>
          </table>
          <input type="submit" name="modify" id="modify" value="modify">
          <?php
}
include('db.php');
if (isset($_POST['user_Id'])) {
    $userid = $_POST['user_Id'];
    echo $userid;
}
if (isset($_POST['modify'])) {
    echo $userid;
    $userName     = mysql_real_escape_string($_POST['userName']);
    $userEmail    = mysql_real_escape_string($_POST['userEmail']);
    $userPassword = mysql_real_escape_string($_POST['userPassword']);
    $string       = "update coeds_user set userName='$userName',userEmail='$userEmail', userPassword='$userPassword' where userId=$userid";
    echo $string;
    $query = mysql_query($string);
    if ($query) {
        $display = "Update Successful";
    } else {
        $display = "Update Failed";
    }
    $s     = "select * from coeds_user";
    $query = mysql_query($s);
    $display .= "<table border='1'>";
    $display .= "<tr><th>UserId</th><th>UserName</th><th>UserEmail</th><th>UserPassword</th><th>RegDate</th><th>RegistrationKey</th></tr>";
    while ($res = mysql_fetch_array($query)) {
        $display .= "<tr>";
        $display .= "<td>" . $res['userId'] . "</td>";
        $display .= "<td>" . $res['userName'] . "</td>";
        $display .= "<td>" . $res['userEmail'] . "</td>";
        $display .= "<td>" . $res['userPassword'] . "</td>";
        $display .= "<td>" . $res['regDate'] . "</td>";
        $display .= "<td>" . $res['registration_key'] . "</td>";
        $display .= "</tr>";
    }
    $display .= "</table>";
    echo $display;
}
?>
    </div>
  </body>
  </form>

  </html>

1 个答案:

答案 0 :(得分:0)

使用

echo "<script>location.replace('user_delete.php');</script>";

而不是标头位置。 希望这会有所帮助。