簡體   English   中英

將表中更新的數據插入到另一個表中

[英]Inserting Updated data in a table to another table

我有一個名為“employees”的表,它有一個 emp_id、名字、姓氏、年齡、薪水、電子郵件、密碼、地址和電話號碼。這個表有記錄。 我有另一個名為 requests 的表,它是空的並且具有相同的信息,包括 req_id。 如果我更新了員工表中的任何數據,我希望記錄僅插入到請求表中。 這就是我到目前為止所做的,但它似乎不起作用。

代碼:

編輯信息.php

<html>

<body>
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";

$conn =  new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

mysqli_select_db($conn, 'project');

$sql = "SELECT * FROM employees";
$records = mysqli_query($conn, $sql);

?>
<table>
    <tr>
        <th> LastName </th>
        <th> FirstName </th>
        <th> Age </th>
        <th> Salary </th>
        <th> Email </th>
        <th> Password </th>
        <th> Address </th>
        <th> Telephone </th>

    </tr>
    <?php


     while($row = mysqli_fetch_array($records))
     {
        echo "<tr><form action=update.php method=post>";
        echo "<td><input type=text name=lname value='".$row['LastName']."'></td>";
        echo "<td><input type=text name=fname value='".$row['FirstName']."'></td>";
        echo "<td><input type=text name=age value='".$row['Age']."'></td>";
        echo "<td><input type=text name=salary value='".$row['salary']."'></td>";
        echo "<td><input type=text name=email value='".$row['email']."'></td>";
        echo "<td><input type=password name=pass value='".$row['emp_password']."'></td>";
        echo "<td><input type=text name=address value='".$row['address']."'></td>";
        echo "<td><input type=text name=telephone value='".$row['telephone']."'></td>";
        echo "<input type= hidden name=id value='".$row['Emp_ID']."'>";
        echo "<td><input type=submit>";
        echo "</form></tr>";

     }


    ?>

</table>
</body>
</html>

更新.php

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";

$conn =  new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

mysqli_select_db($conn, 'project');

//UPDATE QUERY

$sql = "UPDATE requests SET LastName='$_POST[lname]', FirstName='$_POST[fname]', Age='$_POST[age]', salary='$_POST[salary]', 
email='$_POST[email]', emp_password='$_POST[pass]', address='$_POST[address]', telephone ='$_POST[telephone]'
WHERE Emp_ID=$_POST[id]";


if(mysqli_query($conn, $sql))
    header("refresh:1; url= editInfo.php");
else
    echo "not updated";


?>

首先,創建一個文件“db.php”,其中包含所有頁面的數據庫連接,第二個使用語句准備查詢(防止 sql 注入):

編輯信息

$sql = $conn->query("SELECT * FROM employees");

?>
<table>
    <tr>
        <th> LastName </th>
        <th> FirstName </th>
        <th> Age </th>
        <th> Salary </th>
        <th> Email </th>
        <th> Password </th>
        <th> Address </th>
        <th> Telephone </th>

    </tr>
    <?php


     while($row = mysqli_fetch_array($sql))
     {
       //ALL ROWS
     }

更新.php

<?php

require 'db.php';
$lastname = $_POST['lname'];
$firstname = $_POST['fname'];
$age = $_POST['age'];
$salary = $_POST['salary'];
$email = $_POST['email'];
$emp_password = $_POST['pass'];
$address = $_POST['address'];
$phone = $_POST['telephone'];
$empid = $_POST['id'];

//UPDATE QUERY

$sql = $conn->prepare("UPDATE requests SET LastName=?, FirstName=?, Age=?, salary=?, email=?, emp_password=?, address=?, telephone =?    WHERE Emp_ID=?");
$sql->bind_param('sssssssss', $lastname, $firstname, $age, $salary, $email, $emp_password, $address, $phone, $empid);

if ($sql->execute()) {
    header("refresh:1; url= editInfo.php");
} else {
    echo "not updated";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM