0

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.

Falt4rm
  • 915
  • 6
  • 21
Koel
  • 3
  • 1
  • You are creating multiple input type with same name. You should do like [this](http://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array) – jagad89 Jun 03 '15 at 12:03
  • @jagad89 are you saying that i should replace the while loop with the foreach? – Koel Jun 03 '15 at 13:01

1 Answers1

0

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

Akshay Khale
  • 8,151
  • 8
  • 50
  • 58
  • Thanks! I knew it had to be something simple. Sometimes you just stare at the code for too long and can't see it. – Koel Jun 03 '15 at 13:22