我創建了一個顯示頁面,php其中所有用戶記錄都顯示在表格中,并且有洗掉和編輯按鈕來管理用戶,對于洗掉選項我設法做到了,但從昨天開始嘗試編輯按鈕已經有幾個小時了。我希望將所有用戶的資料(全名、電子郵件、密碼...城市)從display.php頁面傳遞update.php到updateform.html了解如何連接這 3 個檔案以成功更新用戶詳細資訊。
以下是檔案:
顯示.php
<!DOCTYPE html>
<html>
<head>
<style>
table,
td,
th {
border: 1.5px solid #d9d9d9;
border-collapse: collapse;
}
table {
width: 100%;
}
</style>
</head>
<body>
</div>
<div class="container">
<?php
$conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");
$sql = "SELECT * FROM users" or die("<script>alert('Connection Failed.')</script>");
$result = mysqli_query($conn, $sql);
?>
<div class="row justify-contect-center">
<table class="table" border-size="1">
<thead>
<tr>
<th>Username</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
<th>Age</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Work Duration</th>
<th>IG Account</th>
<th>State</th>
<th>Postcode</th>
<th>City</th>
<th>Action</th>
</tr>
</thead>
<?php
//while($row = $result->fetch_assoc());
while ($row = $result->fetch_assoc()) :
?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['full_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['password'] ?></td>
<td><?php echo $row['age'] ?></td>
<td><?php echo $row['gender'] ?></td>
<td><?php echo $row['phone_number'] ?></td>
<td><?php echo $row['work_duration'] ?></td>
<td><?php echo $row['ig_account'] ?></td>
<td><?php echo $row['state'] ?></td>
<td><?php echo $row['postcode'] ?></td>
<td><?php echo $row['city'] ?></td>
<td>
<a href="updateform.html?edit=<?php if(isset($username)){
$conn->query("Select * from users where username='$username'");
} ?>" class="btn btn-info">Edit</a>
<a href="delete.php?delete=<?php echo $row['username']; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php
function pre_r($array)
{
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
</body>
</html>
updateform.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Update Information</title>
</head>
<body>
<div class="container">
<form action="update.php" method="POST" class="login-email">
<p class="login-text" style="font-size: 2rem; font-weight: 800;">Update Information</p>
<div class="input-group" style="margin-bottom: 40px;">Full Name:
<input type="text" name="full_name" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Email:
<input type="email" name="email" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Password:
<input type="password" name="password" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Confirm Password:
<input type="password" name="cpassword" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Age:
<input type="text" name="age" value="" required>
</div>
<div style="margin-top: 40px; margin-bottom: 20px;">Gender:
<label for="f-option" class="l-radio" value="">
<input type="radio" name="gender" tabindex="1" value="Male" style="margin-left:10px;">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female" style="margin-left:20px;">
<span> Female</span>
</div>
<div class="input-group" style="margin-bottom: 40px;">Phone Number:
<input type="text" name="phone_number" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Work Duration:
<input type="text" name="work_duration" value="" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">IG Account:
<input type="text" name="ig_account" value="" required>
</div>
<div class="form-group" style="margin-bottom: 10px;">
<label>State:</label>
<select name="state" class="form-control" value="" required>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
<div class="input-group" style="margin-bottom: 40px;">Postcode:
<input type="text" name="postcode" value="" required>
</div>
<div class="input-group" style="margin-bottom: 50px;">City:
<input type="text" name="city" value="" required>
</div>
<div class="input-group" style="margin-top:40px;">
<button name="update" class="btn">Update</button>
</div>
</form>
</div>
</body>
</html>
更新.php
<?php
$conn = mysqli_connect("localhost", "root", "", "finalproject") or die("<script>alert('Connection Failed.')</script>");
if (isset($_POST['update'])) {
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
$phone_number = $_POST['phone_number'];
$work_duration = $_POST['work_duration'];
$ig_account = $_POST['ig_account'];
$state = $_POST["state"];
$postcode = $_POST['postcode'];
$city = $_POST['city'];
$gender = $_POST['gender'];
$age = $_POST['age'];
}
?>
uj5u.com熱心網友回復:
我可以看到你的代碼有很多問題,所以我不遺余力地為你重新編碼,甚至為你做了更新部分,希望你從中學到一些技巧。根據您的代碼,我假設您對游戲相當陌生...我添加了額外的檔案以使您的生活從長遠來看更輕松。
*一些提示
- 您不需要使用 ?> 在檔案末尾關閉 php
<?=$username?>是一樣的<? echo $username; ?>。養眼,簡單。- 盡量不要使用表格,我沒有弄亂你的但檢查 flex 表格或使用 div
- 不要在 html 表格中使用邊框大小...盡量不要在 html 中的任何內容中使用樣式...始終使用 css。類被多次使用 .classname 和 ID 只被使用一次 #idname*
_config.php 將您的資料庫和其他設定放在此檔案中以包含到您的所有頁面中,這樣您就不會浪費時間一遍又一遍地撰寫相同的內容
<?php
$db = new mysqli('localhost', 'user', 'pass', 'riseofwar_v1');
if ($db->connect_errno > 0) {
die('Unable to connect to database [' . $db->connect_error . ']');
}
_funcs.php 我給你我的基本庫,用來清理你的輸入欄位
<?php
include('_config.php');
// BASIC FUNCTIONS; Carry this library to all your future projects too!
function abc ($input){ return preg_match('/^[A-Z] $/i', $input); }
function abcSpc ($input){ return preg_match('/^[a-z][a-z\ ]*$/i', $input); }
function abcNum ($input){ return preg_match('/^[A-Z0-9] $/i', $input); }
function abcNumSpc ($input){ return preg_match('/^[A-Z0-9\ ] $/i', $input); }
function abcNumU ($input){ return preg_match('/^[A-Z0-9_-] $/i', $input); }
function abcNumD ($input){ return preg_match('/^[A-Z0-9-] $/i', $input); }
function num ($input){ if(strlen($input) > 24){ $input=0; }if(!preg_match('/^[0-9] $/', $input)){ $input=0; } return $input; }
function numDot ($input){ if(strlen($input) > 24){ $input=0; }if(!preg_match('/^[0-9.] $/', $input)){ $input=0; } return $input; }
function numU ($input){ return preg_match('/^[0-9_-] $/i', $input); }
function is_odd($num){ return($num & 1); }
function email ($input){ return filter_var($input, FILTER_VALIDATE_EMAIL); }
function phone ($input){ $phone = preg_replace('/[^0-9]/', '', $input); if(strlen($phone) < 10 || !num($phone)) { $input=false; } return $input; }
function is_url($input){ return preg_match('/^(http|https):\/\/[a-z0-9] ([\-\.]{1}[a-z0-9] )*\.[a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i',$input); }
function is_uri ($input){ return preg_match('/^[a-z0-9-] $/i', $input); }
function findIt($find,$string){ return preg_match("/$find/i","$string"); }
_html.php你會喜歡這個...修改您創建的每個 html 頁面的頁眉和頁腳...這是您包含在所有現有和新頁面中的檔案。
<?php
include('_funcs.php');
// Simple function you can call on every page so you don't have to edit each file every time you change something in the headers
function head($title){?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<title><?=$title?></title>
</head>
<body>
<?
}
// Same as the above function, just the footer
function foot(){?>
</body>
</html>
<? }
顯示.php
<?php
include('_html.php');
head('Viewing Users');
?>
<div class="container">
<div class="row justify-contect-center">
<table class="table" border-size="1">
<thead>
<tr>
<th>Username</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
<th>Age</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Work Duration</th>
<th>IG Account</th>
<th>State</th>
<th>Postcode</th>
<th>City</th>
<th>Action</th>
</tr>
</thead>
<?php
$result = $db->query("SELECT * FROM users ORDER BY id LIMIT 25;");
while ($row = $result->fetch_assoc()) {?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['full_name'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['password'] ?></td>
<td><?php echo $row['age'] ?></td>
<td><?php echo $row['gender'] ?></td>
<td><?php echo $row['phone_number'] ?></td>
<td><?php echo $row['work_duration'] ?></td>
<td><?php echo $row['ig_account'] ?></td>
<td><?php echo $row['state'] ?></td>
<td><?php echo $row['postcode'] ?></td>
<td><?php echo $row['city'] ?></td>
<td>
<a href="edit.php?id=<?=$row['id']?>" class="btn btn-info">Edit</a>
<a href="delete.php?delete=<?php echo $row['username']; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?}?>
</table>
</div>
<? foot();
編輯.php
<?php
include('_html.php');
$id = num($_GET['id']) ?: false; // Check the ID in the URL to make sure it's a number
$user = $db->query("SELECT * FROM users WHERE id='$id'")->fetch_assoc();
if (isset($_POST['update'])) {
// CHECK VALUES BEFORE YOU UPDATE THEM
$user['full_name'] = abcSpc($_POST['full_name']) ? $_POST['full_name'] : $user['full_name'];
$user['email'] = email($_POST['email']) ? $_POST['email'] : $user['email'];
$user['password'] = abcNum($_POST['password']) ? $_POST['password'] : $user['password'];
$user['password'] = $user['password'] != $user['cpassword'] ? $user['password'] : $_POST['password'];
$user['phone_number'] = phone($_POST['phone_number']) ? $_POST['phone_number'] : $user['phone_number'];
$user['work_duration'] = num($_POST['work_duration']) ? $_POST['work_duration'] : $user['work_duration'];
$user['ig_account'] = abcSpc($_POST['ig_account']) ? $_POST['ig_account'] : $user['ig_account'];
$user['state'] = num($_POST["state"]) ? $_POST['state'] : $user['state'];
$user['postcode'] = abc($_POST['postcode']) ? $_POST['postcode'] : $user['postcode'];
$user['city'] = num($_POST['city']) ? $_POST['city'] : $user['city'];
$user['gender'] = num($_POST['gender']) ? $_POST['gender'] : $user['gender'];
$user['age'] = num($_POST['age']) ? $_POST['age'] : $user['age'];
$msg = 'Updated! <a href="display.php">View all users again</a>';
$db->query("UPDATE users SET full_name=$user[full_name], email=$user[email], password=$user[password], phone_number=$user[phone_number], work_duration=$user[work_duration], ig_account=$user[ig_account], state=$user[state], postcode=$user[postcode], city=$user[city], gender=$user[gender], age=$user[age] WHERE id='$_SESSION[id]';");
}
head('Update Information');
?>
<?=$msg?>
<div class="container">
<form method="POST" class="login-email">
<p class="login-text" style="font-size: 2rem; font-weight: 800;">Update Information</p>
<div class="input-group" style="margin-bottom: 40px;">Full Name:
<input type="text" name="full_name" value="<?=$user['full_name']?>" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Email:
<input type="email" name="email" value="<?=$user['email']?>" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Password:
<input type="password" name="password" value="">
</div>
<div class="input-group" style="margin-bottom: 40px;">Confirm Password:
<input type="password" name="cpassword" value="">
</div>
<div class="input-group" style="margin-bottom: 40px;">Age:
<input type="text" name="age" value="<?=$user['age']?>" required>
</div>
<div style="margin-top: 40px; margin-bottom: 20px;">Gender:
<label for="f-option" class="l-radio" value="">
<input type="radio" name="gender" checked tabindex="1" value="Male" style="margin-left:10px;">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female" style="margin-left:20px;">
<span> Female</span>
</div>
<div class="input-group" style="margin-bottom: 40px;">Phone Number:
<input type="text" name="phone_number" value="<?=$user['phone_number']?>" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">Work Duration:
<input type="text" name="work_duration" value="<?=$user['work_duration']?>" required>
</div>
<div class="input-group" style="margin-bottom: 40px;">IG Account:
<input type="text" name="ig_account" value="<?=$user['ig_account']?>" required>
</div>
<div class="form-group" style="margin-bottom: 10px;">
<label>State:</label>
<select name="state" class="form-control" required>
<option value="1" <?=$state==1?'checked':''?>>Option 1</option>
<option value="2" <?=$state==2?'checked':''?>>Option 2</option>
<option value="3" <?=$state==3?'checked':''?>>Option 3</option>
</select>
</div>
<div class="input-group" style="margin-bottom: 40px;">Postcode:
<input type="text" name="postcode" value="<?=$user['postcode']?>" required>
</div>
<div class="input-group" style="margin-bottom: 50px;">City:
<input type="text" name="city" value="<?=$user['city']?>" required>
</div>
<div class="input-group" style="margin-top:40px;">
<button name="update" class="btn">Update</button>
</div>
</form>
</div>
<? foot();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/467566.html
