我正在建立一个网站,将笔记本电脑和客户信息保存在单独的表格中。客户表的结构是;
ID,
Name,
Manager,
Email
笔记本电脑表的结构是;
ID,
Asset_No,
Make,
Model,
RAM,
CPU,
MAC_Address,
Serial_Number,
Product_Number,
User_ID,
Location,
Assigned,
Faulty,
Decommissioned,
History
其中 customer.ID == laptops.user_id
我有一个单独的页面用于显示笔记本电脑数据,从更新数据。
在我的更新页面上,我希望能够有一个文本字段,将 customer.name 和 customer.ID 数据提取到 2 个单独的字段中,这样如果我想更新分配的用户,我可以使用下拉字段填充用户名,这将实时更新 customer.ID 的值,以便我可以更新laptops.user_id 字段。 更新 snip1
这是我的更新页面“update.php”
<?php
include 'functions.php';
$pdo = pdo_connect_mysql();
$con = mysqli_connect("localhost","root","","appollo");
$sql = "SELECT * FROM `category`";
$sql2 = "SELECT (Name) FROM bexstaff WHERE Name = ? ";
$sql3 = "Select Name FROM Customer ORDER BY Name Asc";
$sql4 = "Select ID FROM Customer ";
$all_categories = mysqli_query($con,$sql3);
$getIDs = mysqli_query($con,$sql4);
$msg = '';
if (isset($_GET['Asset_No'])) {
if (!empty($_POST)) {
// This part is similar to the create.php, but instead we update a record and not insert
$Asset_No = isset($_POST['Asset_No']) ? $_POST['Asset_No'] : '';
$Make = isset($_POST['Make']) ? $_POST['Make'] : '';
$Model = isset($_POST['Model']) ? $_POST['Model'] : '';
$RAM = isset($_POST['RAM']) ? $_POST['RAM'] : '';
$CPU = isset($_POST['CPU']) ? $_POST['CPU'] : '';
$MAC_Address = isset($_POST['MAC_Address']) ? $_POST['MAC_Address'] : '';
$Serial_Number = isset($_POST['Serial_Number']) ? $_POST['Serial_Number'] : '';
$Product_Number = isset($_POST['Product_Number']) ? $_POST['Product_Number'] : '';
$User = isset($_POST['User_ID']) ? $_POST['User_ID'] : '';
$UserID = isset($_POST['ID']) ? $_POST['ID'] : '';
$Location = isset($_POST['Location']) ? $_POST['Location'] : '';
$Assigned = isset($_POST['Assigned']) ? $_POST['Assigned'] : '';
$Faulty = isset($_POST['Faulty']) ? $_POST['Faulty'] : '';
$Decommissioned = isset($_POST['Decommissioned']) ? $_POST['Decommissioned'] : '';
$History = isset($_POST['History']) ? $_POST['History'] : '';
$stmt = $pdo->prepare('UPDATE Laptops SET Asset_No = ?, Make = ?, Model = ?, RAM = ?, CPU = ?, MAC_Address = ?, Serial_Number = ?, Product_Number = ?, User_ID = ?, Location = ?, Assigned = ?, Faulty = ?, Decommissioned = ?, History = ? WHERE Asset_No = ?');
$stmt->execute([$Asset_No, $Make, $Model, $RAM, $CPU, $MAC_Address, $Serial_Number, $Product_Number, $UserID, $Location, $Assigned, $Faulty, $Decommissioned, $History, $_GET['Asset_No']]);
$msg = 'Updated Successfully!';
}
$stmt = $pdo->prepare('SELECT `Asset_No`, `Make`, `Model`, `RAM`, `CPU`, `MAC_Address`, `Serial_Number`, `Product_Number`, `User_ID`, `Location`, `Assigned`, `Faulty`, `Decommissioned`, `History` FROM `laptops`s WHERE Asset_No = ?');
$stmt->execute([$_GET['Asset_No']]);
$laptops = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$laptops) {
exit('Contact doesn\'t exist with that ID!');
}
} else {
exit('No ID specified!');
}
?>
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Glory:wght@300&display=swap" rel="stylesheet">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/jquery/3.1.0/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function () {
$('select[name="User"]').change(function(){
var ID = $('option:selected', this).attr('ID');
$("#ID").val(ID);
});
</script>
<?=template_header('Read')?>
<?php function get_id($con){
$output = '';
$functionsql = "SELECT * FROM customer";
$functionresult = mysqli_query($con, $functionsql);
while ($row = mysqli_fetch_array($functionresult)){
$output .= '<option value="'.$row["ID"].'">'.$row["Name"].'</option>';
}
return $output;
}
function show_id($con){
$output2 = '';
$functionsql2 = "SELECT ID FROM customer WHERE Name = '' ";
$functionresult2 = mysqli_query($con, $functionsql2);
while ($row = mysqli_fetch_array($functionresult2)){
$output2 .= ''.$row["ID"].'';
}
return $output2;
}
?>
<div class="content update">
<h2>Update Asset <?=$laptops['Asset_No']?></h2>
<form action="update.php?Asset_No=<?=$laptops['Asset_No']?>" method="post">
<label>Laptop Number</label><input type="Asset_No" name="Asset_No" value="<?=$laptops['Asset_No']?>" id="Asset_No" readonly>
<label>Make</label><input type="Make" name="Make" value="<?=$laptops['Make']?>" id="Make" readonly>
<label>Model</label><input type="Model" name="Model" value="<?=$laptops['Model']?>" id="Model" readonly>
<label>RAM</label><input type="RAM" name="RAM" value="<?=$laptops['RAM']?>" id="RAM">
<label>CPU</label><input type="CPU" name="CPU" value="<?=$laptops['CPU']?>" id="CPU" readonly>
<label>MAC Address</label><input type="MAC_Address" name="MAC_Address" value="<?=$laptops['MAC_Address']?>" id="MAC_Address" >
<label>Serial Number</label><input type="Serial_Number" name="Serial_Number" value="<?=$laptops['Serial_Number']?>" id="Serial_Number" readonly>
<label>Product Number</label><input type="Product_Number" name="Product_Number" value="<?=$laptops['Product_Number']?>" id="Product_Number" readonly>
<label>User</label>
<select name="User" id="User">
<?php
?>
<option value="<?php echo get_id($con);
?>">
<?php
?>
</option>
<?php
?>
</select>
<label>ID</label>
<input type="ID" name="ID" value='<?php echo show_id($con)?>' id="ID" >
<label>Location</label><input type="Location" name="Location" value="<?=$laptops['Location']?>" id="Location" >
<label>Assigned</label>
<select name="Assigned">
<option <?php if($laptops['Assigned']=="Assigned") echo "selected"; ?> value="Assigned">Assigned</option>
<option <?php if($laptops['Assigned']=="Returned") echo "selected"; ?> value="Returned">Returned</option>
</select>
<label>Faulty</label>
<select name="Faulty">
<option <?php if($laptops['Faulty']=="Faulty") echo "selected"; ?> value="Faulty">Faulty</option>
<option <?php if($laptops['Faulty']=="Working") echo "selected"; ?> value="Working">Working</option>
</select>
<label>Decommissioned</label>
<select name="Decommissioned">
<option <?php if($laptops['Decommissioned']=="Decommissioned") echo "selected"; ?> value="Decommissioned">Decommissioned</option>
<option <?php if($laptops['Decommissioned']=="Functioning") echo "selected"; ?> value="Functioning">Functioning</option>
</select>
<label>History</label><input type="History" name="History" value="<?=$laptops['History']?>" id="History" rows="4">
<input type="submit" value="Update">
</form>
<?php if ($msg):
?>
<p><?=$msg?></p>
<?php endif; ?>
</div>
<?=template_footer()?>
这是我的笔记本电脑显示页面代码“laptops.php”
<?php
include 'functions.php';
$pdo = pdo_connect_mysql();
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$records_per_page = 5;
if (isset($_GET['search'])) {
$cmd = $pdo->prepare('SELECT *
FROM laptops
WHERE Asset_No LIKE ?
OR Faulty LIKE ?
OR Decommissioned LIKE ?
OR Assigned LIKE ?
OR User LIKE ?
OR MAC_Address LIKE ?
ORDER BY Asset_No ASC');
$cmd->execute(["%".$_GET['search']."%", "%".$_GET['search']."%", "%".$_GET['search']."%", "%".$_GET['search']."%", "%".$_GET['search']."%", "%".$_GET['search']."%"]);
$laptop = $cmd->fetchAll(PDO::FETCH_ASSOC);
} else {
$stmt = $pdo->prepare('SELECT * FROM laptops ORDER BY Asset_No');
$stmt->execute();
$laptop = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_laptops = $pdo->query('SELECT COUNT(*) FROM laptops')->fetchColumn();
}
?>
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Glory:wght@300&display=swap" rel="stylesheet">
</head>
<?=template_header('Read')?>
<div class="content read">
<h2>Laptops</h2>
<a href="create.php" class="create-contact">Add Device</a>
<form action="" method="GET">
<input id="search" name="search" type="text" placeholder="Type here">
<input id="submit" type="submit" value="Search">
</form>
<br></br>
<table>
<thead>
<tr>
<td>Asset_No</td>
<td>Make</td>
<td>Model</td>
<td>RAM</td>
<td>CPU</td>
<td>MAC_Address</td>
<td>Serial_Number</td>
<td>Product_Number</td>
<td>User</td>
<td>Location</td>
<td>Assigned</td>
<td>Faulty</td>
<td>Decommissioned</td>
<td>History</td>
<td></td>
</tr>
</thead>
<tbody>
<?php foreach ($laptop as $laptops): ?>
<tr>
<td><?=$laptops['Asset_No']?></td>
<td><?=$laptops['Make']?></td>
<td><?=$laptops['Model']?></td>
<td><?=$laptops['RAM']?></td>
<td><?=$laptops['CPU']?></td>
<td><?=$laptops['MAC_Address']?></td>
<td><?=$laptops['Serial_Number']?></td>
<td><?=$laptops['Product_Number']?></td>
<td><?=$laptops['User_ID']?></td>
<td><?=$laptops['Location']?></td>
<td><?=$laptops['Assigned']?></td>
<td><?=$laptops['Faulty']?></td>
<td><?=$laptops['Decommissioned']?></td>
<td><?=$laptops['History']?></td>
<td class="actions">
<a href="update.php?Asset_No=<?=$laptops['Asset_No']?>" class="edit"><i class="fas fa-pen fa-xs"></i></a>
<!-- <a href="delete.php?Asset_No=<?=$laptops['Asset_No']?>" class="trash"><i class="fas fa-trash fa-xs"></i></a> -->
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if ($page > 1): ?>
<a href="read.php?page=<?=$page-1?>"><i class="fas fa-angle-double-left fa-sm"></i></a>
<?php endif; ?>
<?php if ($page*$records_per_page < $num_laptops): ?>
<a href="read.php?page=<?=$page+1?>"><i class="fas fa-angle-double-right fa-sm"></i></a>
<?php endif; ?>
</div>-->
</div>
<?=template_footer()?>