du521521521 2015-06-03 11:55
浏览 13
已采纳

Php:任何用户输入字段和提交按钮仅更新最后一个字段

Forgive me if i mess this up...first time posting.

Question :

I want it to iterate through the database and then:

  • assign a note
  • status(radio button)
  • submit button

For every user listed in table.

PBM : anytime I try to to make a change to any of the users it only updates the last user on the table and inconsistently updates the notes.

I just want it to update the 1 specific user that is in that row on the table.

If there is a more efficient or prepackaged way of doing this I am all ears. I would still like to figure out what i am doing wrong. I know that this is currently sql vulnerable code but for the sake of simplicity for me, inputs are not sanitized.

    <head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css"
</head>
<h1>In Out Board</h1>
<?php
$conn = new mysqli('xxxx', 'xxx', 'xxx', 'xxxxx');

if ($conn->connect_error) {
    die("connection failed: " . $conn->connect_error);
}

if (isset($_GET['submit'])) {
    $idkey = $_GET['idkey'];
    $Status = $_GET['status'];
    $Notes = $_GET['notes'];
    $query = "update InOutUsers set status='$Status', Notes='$Notes'where idkey='$idkey'";
    $result = mysqli_query($conn, $query) or die(mysqli_error());
}

$sql = "SELECT Name, Status, Notes, idkey FROM InOutUsers";
$result = $conn->query($sql);
echo '<table><thead><form>';
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>' . $row["Name"] . '</td>';
        echo '<td>' . $row["Status"] . '</td>';
        echo '<td>' . $row["Notes"] . '</td>';
        echo "<input type='hidden' name='idkey' value='{$row['idkey']}' />";
        echo '
        <td style="width: 100px"><input type="radio" name="status" value="In" >In
            <input type="radio" name="status" value="Out" >Out </td>';
        echo '<td>Status: <input type="text" name="notes" placeholder="Where are you going?"></td>';
        echo '<td>';
        echo "<input type='submit' name='submit' value='update'/>";
        echo '</td>';


        echo '</tr>';
        echo '<br>';
    }
}
echo '</form></thead></table>';

Here is my sql:

CREATE TABLE IF NOT EXISTS `InOutUsers` (
  `Name` text NOT NULL,
  `Status` varchar(3) NOT NULL,
  `Notes` text,
  `idkey` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

I fairly new to programming and to using stack exchange so feel free to advise me of anything that I am doing wrong. I fear its something simple but i cannot wrap my head around it.

  • 写回答

1条回答 默认 最新

  • dongzhun4898 2015-06-03 13:06
    关注

    Firstly I would like to welcome you to Stackoverflow and the world of programming. Please find below the working code

    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"
    </head>
    <h1>In Out Board</h1>
    <?php
    $conn = new mysqli('xxxx', 'xxxx', 'xxxx', 'xxxx');
    
    if ($conn->connect_error) {
        die("connection failed: " . $conn->connect_error);
    }
    
    if (isset($_GET['submit'])) {
        $idkey = $_GET['idkey'];
        $Status = $_GET['status'];
        $Notes = $_GET['notes'];
        $query = "update InOutUsers set status='$Status', Notes='$Notes'where idkey='$idkey'";
        $result = mysqli_query($conn, $query) or die(mysqli_error());
    }
    
    $sql = "SELECT Name, Status, Notes, idkey FROM InOutUsers";
    $result = $conn->query($sql);
    echo '<table><thead>';
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo '<form><tr>';
            echo '<td>' . $row["Name"] . '</td>';
            echo '<td>' . $row["Status"] . '</td>';
            echo '<td>' . $row["Notes"] . '</td>';
            echo "<input type='hidden' name='idkey' value='{$row['idkey']}' />";
            echo '
            <td style="width: 100px"><input type="radio" name="status" value="In" >In
                <input type="radio" name="status" value="Out" >Out </td>';
            echo '<td>Status: <input type="text" name="notes" placeholder="Where are you going?"></td>';
            echo '<td>';
            echo "<input type='submit' name='submit' value='update'/>";
            echo '</td>';
    
    
            echo '</tr></form>';
            echo '<br>';
        }
    }
    echo '</thead></table>';
    

    The main issue in your code was, you were having only single form and you were printing all records hence the IDkey value keeps on update with the last record idkey value as a result only last record was being updated

    I have added <form> tag in the Loop which will create different forms and will update a specific record.

    Just add your Mysql Credentials and code should work fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?