通过PHP向mysql表添加编辑功能
我目前正在从事一个项目,但不幸的是,我现在需要向通过php从mysql获取的表内容添加一个编辑功能 假设我有一张桌子:通过PHP向mysql表添加编辑功能,php,mysql,Php,Mysql,我目前正在从事一个项目,但不幸的是,我现在需要向通过php从mysql获取的表内容添加一个编辑功能 假设我有一张桌子: id名称城市 1克里斯·米兰 2玛雅都灵 我希望增加一项功能,用户可以直接编辑特定行的内容,因此类似于 id名称城市 1克里斯米兰编辑部 2玛雅都灵编辑 我查阅了很多教程,但无法理解它们的过程 这是我的密码: <?php require_once 'welcome.php'; ?> <?php echo "<table style='width: 600
id名称城市
1克里斯·米兰
2玛雅都灵
我希望增加一项功能,用户可以直接编辑特定行的内容,因此类似于
id名称城市
1克里斯米兰编辑部
2玛雅都灵编辑
我查阅了很多教程,但无法理解它们的过程 这是我的密码:
<?php
require_once 'welcome.php';
?>
<?php
echo "<table style='width: 600px; border: solid 1px black; padding: 10px'>";
echo "<tr><th>Id</th><th>Codice Cliente</th><th>Data</th><th>Tipo di Trattamento</th><th>Note Trattamento</th><th>Costo Trattamento</th><th>Offerta</th><th>Rivendita</th><th>Note rivendita</th><th>Prezzo</th><th>Action</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width: 190px; border: 1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "e";
$password = "0";
$dbname = "e";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$codice_cliente = filter_var($_POST["codice_cliente"], FILTER_SANITIZE_NUMBER_INT);
$data = $_POST["data"];
$user_id = $_POST["user_id"];
$edit= $_POST["edit"];
$stmt = $conn->prepare("SELECT id, codice_cliente, data, trattamento, note_trattamento, costo_trattamento, offerta, rivendita, note_rivendita, prezzo FROM Dati_Clienti WHERE user_id='Beauty' AND (codice_cliente='$codice_cliente' OR data='$data')");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v ;
}
}
catch(PDOException $e) {
echo " ". $e->getMessage();
}
$conn = null;
echo "</table>";
?>
为了在mysql中编辑记录,我们使用UPDATE
语句
例如:
# Update the name for record 1
UPDATE Persons
SET name='Jane Doe'
WHERE id=1;
因为您使用的是PHP,所以您可能知道如何执行查询。所以理论上你只需要以下几点
示例代码
身份证件
名称
城市
要编辑:
<?php
/**
* edit.php
*/
// If the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$stmt = "UPDATE Persons
SET name='".$_POST['name']."',
city='".$_POST['city']."'
WHERE id='".$_POST['id']."'
LIMIT 1
";
$database->execute($stmt);
echo "Data saved";
exit;
}
// Get the data for the chosen record to edit
$personId = $_GET['id'];
$stmt = "SELECT
*
FROM Persons
WHERE id='".$personId."'
";
// Again, get your data
$person = $database->execute();
?>
<!-- Your form -->
<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" role="form">
Name: <input type="text" name="name" value="<?= $person['name']; ?>"><br />
City: <input type="text" name="city" value="<?= $person['city']; ?>"><br />
<input type="hidden" name="id" value="<?= $person['id']; ?>">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
如果有人遇到这种情况,这里有一个我必须开发的快速代码,它工作得非常完美
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "un";
$password = "up";
$dbname = "dbn";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$codice_cliente=$_POST["codice_cliente"];
$data=$_POST["data"];
$sql = "SELECT id, codice_cliente, data, trattamento, note_trattamento, costo_trattamento, offerta, rivendita, note_rivendita, prezzo FROM Dati_Clienti WHERE (codice_cliente='$codice_cliente' OR data='$data')AND user_id='Beauty'";
$result = $conn->query($sql);
if (empty($codice_cliente) && empty($data)){
echo ("<br/><font color='red'><center> Enter either cliente code or date!</center></font>");
} else {
if ($result->num_rows > 0) {
echo "<table><tr><th>Codice Cliente</th><th>Data</th><th>Trattamento</th><th>Note</th><th>Costo Trattamento(€)</th><th>Offerta</th><th>Rivendita</th><th>Note</th><th>Prezzo(€)</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["codice_cliente"]. "</td><td>" . $row["data"]. "</td><td>" . $row["trattamento"]. "</td><td>" . $row["note_trattamento"]. "</td><td>" . $row["costo_trattamento"]. "</td><td>"
. $row["offerta"]. "</td><td>" . $row["rivendita"]. "</td><td>" . $row["prezzo"]. "</td><td><font ><a style='color:#7dbbdb;text-decoration:none' href='edit.php?edit=$row[id]'><center>modifica</center></a></font></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
}
$conn->close();
?>
</body>
</html>}
表,th,td{
边框:1px纯黑;
}
有什么不符合你的要求?没有提到这一点,或者有任何错误。你没有为此发布html/表单,所以我们不知道这是否可以。嗨,funk,事情是在表中添加编辑选项,我可以通过创建一个编辑表单,然后制作一个php端来执行此操作来实现这一点,但是直接将此添加到表中是个问题,我以前没有这样做过,希望你现在就了解我Hiya!好的,下面有一个答案,您可以尝试,但要注意,您的代码对sql注入是开放的。你最好使用一个准备好的语句。好的,谢谢pete,我现在就试试,非常感谢Mate。我已经在我的确切项目中修改了代码,但它现在只显示了一个空白页,我将在这里粘贴代码,'可能我忘了指出查询是根据用户提供的详细信息执行的,(即日期和代码)ID codice_客户数据策略尝试使用ini_集(“显示错误”,“打开”)启用错误报告代码>和错误报告(E_ALL)代码>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "un";
$password = "up";
$dbname = "dbn";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$codice_cliente=$_POST["codice_cliente"];
$data=$_POST["data"];
$sql = "SELECT id, codice_cliente, data, trattamento, note_trattamento, costo_trattamento, offerta, rivendita, note_rivendita, prezzo FROM Dati_Clienti WHERE (codice_cliente='$codice_cliente' OR data='$data')AND user_id='Beauty'";
$result = $conn->query($sql);
if (empty($codice_cliente) && empty($data)){
echo ("<br/><font color='red'><center> Enter either cliente code or date!</center></font>");
} else {
if ($result->num_rows > 0) {
echo "<table><tr><th>Codice Cliente</th><th>Data</th><th>Trattamento</th><th>Note</th><th>Costo Trattamento(€)</th><th>Offerta</th><th>Rivendita</th><th>Note</th><th>Prezzo(€)</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["codice_cliente"]. "</td><td>" . $row["data"]. "</td><td>" . $row["trattamento"]. "</td><td>" . $row["note_trattamento"]. "</td><td>" . $row["costo_trattamento"]. "</td><td>"
. $row["offerta"]. "</td><td>" . $row["rivendita"]. "</td><td>" . $row["prezzo"]. "</td><td><font ><a style='color:#7dbbdb;text-decoration:none' href='edit.php?edit=$row[id]'><center>modifica</center></a></font></td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
}
$conn->close();
?>
</body>
</html>}
<?php
$mysql_host = "localhost";
$mysql_username = "c";
$mysql_password = "c";
$mysql_database = "c";
$mysqli = new mysqli($mysql_host, $mysql_username, $mysql_password,
$mysql_database);
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$codice_cliente=$_POST['codice_cliente'];
$data=$_POST['data'];
if (empty($codice_cliente) && empty($data)){
echo ("<br/><font color='red'><center> Enter either cliente code or
date!</center></font>");
} else {
$result=$mysqli->query("SELECT id, codice_cliente, data, trattamento,
note_trattamento, costo_trattamento, offerta, rivendita,
note_rivendita, prezzo FROM Dati_Clienti WHERE
(codice_cliente='$codice_cliente' OR data='$data')AND
user_id='Beauty'");
foreach ($result as $row) {
echo ("<p>");
foreach ($row as $field) {
echo ($field." ");
}
echo "<a href='edit.php?edit=$row[id]'>edit</a></p>";
}
}
echo "</table>";*/
?>