phppup Posted May 24, 2020 Share Posted May 24, 2020 I believe this is a PHP issue and NOT an HTML question based on the source of my frustrations. I am populating a table with data from a database. if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["comment"]. "<br>"; echo "<input type='submit' name='submit' />" ; each row has a submit button for comments pertaining to the specific ID so that comments can be edited with an UPDATE statement. I have a success message that displays at the top of the table after the db is updated with new comment text. I would like have the success message displayed next to the associated button that was clicked. So far, I have used this if(isset($submitted) { echo "SUBMITTED DATA"; } but it displays the SUCCESS message next to every button that exists for submitting updates. How can I restrict this so that the message is ONLY displayed at the effected button? Quote Link to comment Share on other sites More sharing options...
gw1500se Posted May 24, 2020 Share Posted May 24, 2020 Don't give the same name to every button. Create an array of buttons then check which one was selected when you process the submit. i=0; while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["comment"]. "<br>"; echo "<input type='submit' name='button[i++]' />" ; Quote Link to comment Share on other sites More sharing options...
phppup Posted May 25, 2020 Author Share Posted May 25, 2020 (edited) i currently have the submit name corresponding to the $row["id"] so I'm not sure if venturing away from that course is going to resolve the issue. The problem seems to be more with using the write IF statement to segregate the selected button. Edited May 25, 2020 by phppup Quote Link to comment Share on other sites More sharing options...
requinix Posted May 25, 2020 Share Posted May 25, 2020 21 minutes ago, phppup said: i currently have the submit name corresponding to the $row["id"] Not according to the code you posted. 21 minutes ago, phppup said: The problem seems to be more with using the write IF statement to segregate the selected button. Either your code is wrong and gw1500se is right, or I don't know because you haven't posted your real code. Quote Link to comment Share on other sites More sharing options...
phppup Posted May 25, 2020 Author Share Posted May 25, 2020 Updated with same issue: echo "<td> <form method='POST' action=''>"; echo "<textarea name='comment' cols='30' rows='5'> " . $row['comments'] . " </textarea></td>"; echo "<td> <input type='submit' name='submit' value='Save text' />"; //echo "<td> <input type='submit' name=' " . $row['id'] . " ' value='Save text' />"; //tried but didn't work echo " <input type='hidden' name='IDs' value=' " . $row['id'] . " '> "; echo "</form></td>"; Either way, I get a success message at every button in every row when I want a message ONLY at the clicked button. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 25, 2020 Share Posted May 25, 2020 I don't see where you output the success message. Quote Link to comment Share on other sites More sharing options...
phppup Posted May 25, 2020 Author Share Posted May 25, 2020 The success message that displays at the top of the table is part of my UPDATE statement which runs separately within the script before this SELECT display. I have tried several variations to get a message next to the submit button. Here is a current effort that displays the same message at each iteration of a row. if(isset($IDs) { echo "Variable is set.<br>".$IDs; } The above is displaying the message at all rows while confirming that $IDs is, in fact, grabbing the correct row ID number. Also confirmed in my SUCCESS message. if(isset($IDs) && $IDs == 3) { echo "Variable is set.<br>".$IDs; } This addition confirms again, that the correct value of $IDs is being passed, as the message is ONLY displayed when the submit button on row 3, for ID 3, is clicked. Yet the message is displayed at every row (when it is, in fact, displayed after selecting the submit button #3. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 25, 2020 Share Posted May 25, 2020 Okay. Here's what you need to do: You need to show us the full version of the code that does not work. Talking about the stuff displaying at the top of the table doesn't help. Showing single lines of code doesn't help. Reiterating what works and what does not work doesn't help. Make sure your code is written so that it tries to do what you want it to do, then post the whole thing so we can tell you what's wrong with it. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted May 25, 2020 Share Posted May 25, 2020 Personally I would just populate the table and if you want people to edit the comments use a HTML anchor tag: You can then either redirect the edit to another HTML page and/or use some form of Javascript/Ajax on the anchor tag. That's what I did with my small blog that I wrote for my website: <?php foreach ($journal as $cms) { ?> <div class="article"> <h2><?= $cms->heading; ?> <span class="subheading">by <?= $cms->author ?> on <?= $cms->date_added ?></span></h2> <a class="myLightBox" href="<?= $cms->image_path; ?>" title="Picture Gallery" data-picture="<?= $counter ?>" data-exif="<?php if (!is_null($cms->Model)) { echo $cms->Model . " --- " . $cms->FocalLength . " --- " . $cms->Aperture . " --- " . $cms->ISO . " --- " . $cms->ExposureTime; } ?>"><img class="editPic" src="<?= $cms->thumb_path; ?>" alt="Picture for Journal Entry"></a> <hr> <p><?php echo nl2br($cms->content); ?></p> <a class="btn3" href="edit.php?article_id=><?= $cms->id; ?>">Edit</a> <a class="btn3" href="delete_entry.php?id=<?= $cms->id; ?>" data-confirm="Do you really want to delete this item?">Delete</a> <hr> </div> <?php $counter += 1; } ?> I just find it cleaner and easier to understand. Quote Link to comment Share on other sites More sharing options...
phppup Posted May 25, 2020 Author Share Posted May 25, 2020 $sql = "UPDATE $table SET comment='$comment' WHERE id=IDs"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully for $IDs"; //appears at top of table and confirms $IDs value is active } else { echo "Error updating record: " . mysqli_error($conn); } $sql = "SELECT * FROM $table "; if($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_assoc($result)) { echo "<td>id: " . $row['id']. "</td>" ; echo "<td>Name: " . $row['firstname']. "</td>" ; echo "<td>other: " . $row['other']. "</td>" ; echo "<td> <form method='POST' action=' '>"; echo "<textarea name='comment' > " . $row['comment'] . " </textarea></td>"; echo "<td> <input type='submit' name='submit' value='Save text' />"; //echo "<td> <input type='submit' name=' " . $row['id'] . " ' value='Save text' />"; //tried but didn't work echo " <input type='hidden' name='IDs' value=' " . $row['id'] . " '> "; //if(isset($IDs) { echo "Variable is set.<br>".$IDs; } //limits effect to ONLY clicking submit button #3 but displays for ALL rows if(isset($IDs) && $IDs == 3) { echo "Variable is set ".$IDs; } } echo "</form></td>"; } Quote Link to comment Share on other sites More sharing options...
Barand Posted May 25, 2020 Share Posted May 25, 2020 Is that really your code? An insert query that won't work and an html table with no rows. Quote Link to comment Share on other sites More sharing options...
phppup Posted May 25, 2020 Author Share Posted May 25, 2020 (edited) Sorry, lost tiny pieces in effort to cull personal comments. $sql = "UPDATE $table SET comment='$comment' WHERE id=$IDs"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully for $IDs"; //appears at top of table and confirms $IDs value is active } else { echo "Error updating record: " . mysqli_error($conn); } $sql = "SELECT * FROM $table "; if($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($result) > 0){ echo "<table>"; while($row = mysqli_fetch_assoc($result)) { echo "<tr>"; echo "<td>id: " . $row['id']. "</td>" ; echo "<td>Name: " . $row['firstname']. "</td>" ; echo "<td>other: " . $row['other']. "</td>" ; echo "<td> <form method='POST' action=' '>"; echo "<textarea name='comment' > " . $row['comment'] . " </textarea></td>"; echo "<td> <input type='submit' name='submit' value='Save text' />"; //echo "<td> <input type='submit' name=' " . $row['id'] . " ' value='Save text' />"; //tried but didn't work echo " <input type='hidden' name='IDs' value=' " . $row['id'] . " '> "; //if(isset($IDs) { echo "Variable is set.<br>".$IDs; } //limits effect to ONLY appearing when clicking submit button #3 but displays for ALL rows if(isset($IDs) && $IDs == 3) { echo "Variable is set ".$IDs; } } echo "</form></td>"; echo "</tr>"; } echo "</table>"; Edited May 25, 2020 by phppup Quote Link to comment Share on other sites More sharing options...
Barand Posted May 25, 2020 Share Posted May 25, 2020 Reorganised it a bit <?php $table = 'puptest'; $msg = ''; $updatedID = $_POST['IDs'] ?? 0; if ($_SERVER['REQUEST_METHOD']=='POST') { $sql = "UPDATE $table SET comment=? WHERE id=?"; $stmt = $conn->prepare($sql); $stmt->bind_param ('si', $_POST['comment'], $_POST['IDs']); if ($stmt->execute()) { $msg = '<br>SUCCESS'; } else { $msg = '<br>FAILED'; } } $sql = "SELECT id , firstname , other , comment FROM $table "; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table border='1'> <tr><th>ID</th> <th>Name</th> <th>Other</th> <th>Comment</th> <th> </th> </tr>"; while ($row = $result->fetch_assoc()) { $message = $updatedID == $row['id'] ? $msg : ''; // is this the row that was updated? echo "<tr> <form method='POST'> <td>{$row['id']}</td> <td>{$row['firstname']}</td> <td>{$row['other']}</td> <td><textarea name='comment' cols='30' rows='5'>{$row['comment']}</textarea></td> <td> <input type='submit' name='submit' value='Save text' /> <input type='hidden' name='IDs' value='{$row['id']}'> $message </td> </form> </tr>"; } echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
phppup Posted May 25, 2020 Author Share Posted May 25, 2020 Thanks. I use Procedural, but will try to adapt your modifications. I think I see your logic here although these two lines have me a little confused (regarding their Procedural translations). $updatedID = $_POST['IDs'] ?? 0; $message = $updatedID == $row['id'] ? $msg : ''; // is this the row that was updated? Also, can I use prepared statements with Procedural method. Looked a few times and didn't find a clear confirmation. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 25, 2020 Share Posted May 25, 2020 (edited) 1 hour ago, phppup said: $updatedID = $_POST['IDs'] ?? 0; Could be written as $updatedID = isset($_POST['IDs']) ? $_POST['IDs'] : 0; Which, in turn, could be written as if (isset($_POST['IDs']) { $updatedID = $_POST['IDs']; } else { $updatedID = 0; } A "ternary" expression is a conditional assignment $myvar = condition ? <value if condition is true> : <value if condition is false> 1 hour ago, phppup said: Also, can I use prepared statements with Procedural method. Looked a few times and didn't find a clear confirmation. https://www.php.net/manual/en/mysqli.prepare.php Edited May 25, 2020 by Barand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.