使用用户界面PHP更新mySQL表



[Sample Look]

我正在尝试制作一个界面,在这里您可以编辑/添加/删除mySQL数据库的字段。这就是它在视觉上的样子,我在客户端的所有功能都在工作。

我的问题是:如何将任何编辑/添加/删除传递到服务器端?我将为我的JSFiddle添加一个链接。下面的代码将显示我目前如何出色地使用该表。

<?php
$servername = "localhost";
$username = "lalalal";
$password = "lalalal";
$link = mysqli_connect("localhost", "lalala", "lalala", "lalala");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sqlStart = "SELECT `Name`, `EXT`, `Returning Time`, `Returning Date`, `Out`, `Reset`, `Booked` FROM `lalala`";
if($result = mysqli_query($link, $sqlStart)){
if(mysqli_num_rows($result) > 0){
echo "<table id = contactTable>";
echo "<tr id = row1>";
echo "<th id = sortTable onclick=sortTable(0)>Name &#8597;</th>";
echo "<th style = width:100px;>EXT</th>";
echo "<th style = width:300px;>Returning Time</th>";
echo "<th style = width:300px;>Returning Date</th>";
echo "<th style = width:70px;>Out</th>";
echo "<th style = width:100px;>Reset</th>";
echo "<th style = width:600px;>Booked</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
$currentCheck = $row['Out'];
if ($currentCheck == 0) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['EXT'] . "</td>";
$currentTime = $row['Returning Time'];
if ($currentTime == 0) {
echo "<td> <form> <input type = 'time', id = 'timePickChange'> </form> </td>";
} else {
echo "<td> <form> <input type = 'time', id = 'timePickChange' value =" . $currentTime . "> </form> </td>";
}

$currentDate = $row['Returning Date'];
echo "<td> <form> <input type = 'date', id = 'datePickChange' value =" . $currentDate . "> </form> </td>";
echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)'> </form> </td>";
echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";

} else if ($currentCheck == 1) {
echo "<tr style = 'background-color: #E2E9FD'>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['EXT'] . "</td>";
$currentTime = $row['Returning Time'];
echo "<td> <form> <input type = 'time', id = timePickChange disabled> </form> </td>";
$currentDate = $row['Returning Date'];
echo "<td> <form> <input type = 'date', id = datePickChange disabled> </form> </td>";
echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)' checked> </form> </td>";
echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";
}
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sqlStart. " . mysqli_error($link);
}
?>

根据您的数据验证模型,您可能需要在将输入值发布到后端之前控制客户端。

AFAIK,你已经在客户端添加/编辑/删除了你的联系人,所以如果我理解正确,当你的用户应该点击编辑/删除&确认,这将是对用户在浏览器中所做操作的确认,除了可能需要专用按钮/行(或任何其他可绑定事件(之外,这并没有太大变化。

对于这些操作,您可以进行批量删除/编辑,这可以通过在JS中过滤掉所有修改/删除的数据,并以字符串数组的形式使用Ajax/jQuery将其发送到后端PHP来轻松完成。至于插入操作,您可以通过执行POST操作,在将它们添加到表的同时提交它们。

它可以用这样的东西来完成:

$.ajax({
method: "PUT",
url: "some.php",
data: JSON.stringify(myUpdatedDataInAnArray) 
// you might need to stringify your array to ensure format ? 
})
.done(function( msg ) {
alert( "Data Updated: " + msg );
});

在您的后端php中,您可以监听具有以下内容的POST/PUT/DELETE方法:

if (isset($_POST['add'])){
do your thing
}
if (isset($_PUT['updated'])){
//Since you're sending a stringified array, you must parse it with 
$myArray = json_decode($_PUT['updated']);
do your thing
}
if (isset($_DELETE['deleted'])){
do your thing
}

我之所以说Ajax,是因为使用传统的POST/PUT/DELETE表单会导致刷新页面。

以下是一些有用的参考文献:

  • JS JSON Stringify和JSON Parse
  • PHP:JSON DECODE和JSON Encode
  • Ajax文档
  • Ajax示例

最新更新