亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶 while 循環的 PHP 表單

帶 while 循環的 PHP 表單

PHP
森欄 2023-06-24 17:15:24
我在生成可以打印基于 SQL 查詢的表單的頁面時遇到了一些麻煩。在這種情況下,我需要執行一個查詢來列出表中名稱為 Porcac1x 的所有行。內容需要是顯示當前變量值并且可以更新的輸入字段。這正是我陷入困境的地方。如何創建一個基于 php while 循環的可變表單?使用附加的代碼,我可以列出內容并顯示所有變量,但我在創建表單操作來更新值時遇到麻煩。我想澄清的是,我并不關心安全性,因為代碼將在本地環境中運行,我是唯一有權訪問的人。這是當前輸出但是保存按鈕當然不起作用<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><form action="" method="post">        <?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);
查看完整描述

1 回答

?
慕碼人8056858

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>


查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號