1 回答
TA贡献1803条经验 获得超6个赞
要使其发挥作用,您将需要数组符号。(或每行 1 个表格)
这是每行 1 个表单元素的解决方案:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="$1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>test page</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "root";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `id`, `title`, `amount` FROM `expenses` WHERE name='Porcac1x';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<form method="post">';
echo "ID: <input type='text' name='id' value='".$row["id"]."'> Title: <input type='text' name='title' value='".$row["title"]."'> Amount: <input type='text' name='amount' value='".$row["amount"]."'> <button type='submit' name='save'>Save</button><br>";
echo '</form>';
}
} else {
echo "0 results";
}
if(isset($_POST['save'])){
$myID = $_POST["id"];//??? < Issue
$myTitle = $_POST["title"];//??? < Issue
$myAmount = $_POST["amount"]; //??? < Issue
echo $myID;
echo $myTitle;
echo $myAmount;
$sqlUpdate = "UPDATE expenses SET title='$myTitle', amount ='$myAmount' WHERE id='$myID';";
echo $sqlUpdate;
if ($conn->multi_query($sqlUpdate) === TRUE) {
echo "Record updated successfully";
$risposta= "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
$risposta= "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报