[PHP&MYSQL]3.新增資料到資料表

  • 1972
  • 0
  • php
  • 2023-02-11

文、意如

先做表單

insertlist.php

<?php
include 'header.php'; //引入表頭
?>
<html>

<head>
    <title>新增資料</title>
   
</head>
<?php
$servername = "localhost"; //伺服器ip或本地端localhost
$username = "root"; //登入帳號
$password = ""; //密碼
$dbname = "mydb"; //資料表名稱

//建立連線
$conn = new mysqli($servername, $username, $password, $dbname);

//確認連線成功或失敗
if ($conn->connect_error) {
    die("連線失敗" . $conn->connect_error);
}
//echo "連線成功";
?>
<body>
    <div style="text-align:center;width:100%;height:50px;">
        <div style="width:70%;height:20px;margin:0 auto;">
        <h1 class="margin_top50">清單</h1>
        <div class="text-left">
            新增管理者資料
        </div>
            <form action="dbinsertlist.php" method="post">
                姓名:<input type="text" name="adm_name"><br>
                帳號:<input type="text" name="adm_account"><br>
                <input type="submit">
            </form>
            
        </div>
    </div>
    </div>
    </div>


</body>

</html>

送出表單

dbinsertlist.php

<?php
include 'header.php'; //引入表頭
?>
<html>

<head>
    <title>新增資料</title>
  
</head>
<?php
$servername = "localhost"; //伺服器ip或本地端localhost
$username = "root"; //登入帳號
$password = ""; //密碼
$dbname = "mydb"; //資料表名稱

//建立連線
$conn = new mysqli($servername, $username, $password, $dbname);

//確認連線成功或失敗
if ($conn->connect_error) {
    die("連線失敗" . $conn->connect_error);
}
//echo "連線成功";

$adm_name=$_POST["adm_name"];
$adm_account=$_POST["adm_account"];

$sql = "INSERT INTO admin_manager (adm_name, adm_account, create_user,update_user) VALUES ('".$adm_name."', '".$adm_account."', 'system', 'system')";
if ($conn->query($sql) === TRUE) {
    echo "新增成功";
    header("location:list.php");
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }


  //關閉資料庫
  $conn->close();


?>

輸入資料

 

提交後回清單頁

Yiru@Studio - 關於我 - 意如