I have a table, displayied with an echo, where, for each row I'd like to have a button to change a "status" field (I want to update just one field). I am trying to insert a form button into my echo but I don't get anything.
This is my echo code:
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['fullname'] . '</td>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['country'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['website'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo "<td>" . $row['status'] . " </td>";
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
updatestatus.php
// check if the form has been submitted.
if (isset($_POST['submit'])) {
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id'])) {
// get form data, making sure it is valid
$id = $_POST['id'];
$status = $_POST['status'];
if ($status == 0) {
$status == 1;
mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
or die(mysql_error());
} else {
$status == 0;
mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
or die(mysql_error());
}
header("Location: view.php");
}
}
So, if my status field is = 0, I want to change it = 1 and update into my database, and vice versa.
Thanks for your attention
Your form isn't sending anything to updatestatus.php, you can add the data to POST in a hidden form like so:
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
This is the simple fix to your code above, there are still some security flaws and efficiency methods others have mentioned in the comments.
Check this PHP code
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['fullname'] . '</td>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['country'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['website'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo "<td>" . $row['status'] . " </td>";
echo "<td>";
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
echo "</td>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
updatestatus.php
// check if the form has been submitted.
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$status = $_POST['status'];
if ($status == 0){
$status == 1;
mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='".$id."'")
or die(mysql_error());
} else {
$status == 0;
mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='$id'")
or die(mysql_error());
}
header("Location: view.php");
}
}
Related
There is an error in my php that I cannot solve. Everything is working including 'insert' but not update but I don't know why. If you can help me it would be great. Thank you!
i cannot make update on PHP (Mysqli)
<?php
include("conn.php");
echo "<meta charset='utf-8'>";
?>
<?php
$id = $_GET['edi'];
$showrecs = "SELECT ID, English, Georgian FROM register WHERE ID='$id'";
$result = $conn->query($showrecs);
if(isset($_POST['update'])){
$Updatee = "UPDATE register SET English='".$_POST['English']."', Georgian='".$_POST['Georgian']."' WHERE ID='$id'";
$res=mysqli_query($conn, $Updatee);
if($res==1){
echo "წარმატებით განხორციელდა რედაქტირება.";
echo "<br>";
echo "<a href='view.php'>ჩანაწერების ნახვა</a>";
}
if($res==0){
echo "არაფერიც არ მოხდა";
}
exit();
} elseif ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form method=POST>";
echo "<tr>";
echo "<td>" . "<input type='text' name='id' value=" . $row['ID'] ." disabled>". "</td>";
echo "<td>" . "<input type='text' name='English' value=" . $row['English'] .">". " </td>";
echo "<td>" . "<input type='text' name='Georgian' value=" . $row['Georgian'] .">". " </td>";
echo "<td>" . "<input type='submit' name='update' value='შეცვლა'>" . " </td>";
echo "</form>";
echo "</br>";
echo "</br>";
}
} else {
echo "Error;";
}
$conn->close();
?>
I'm stuck in the delete function, I wonder why my delete button is not functioning, and I already edited my code.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$semester = ($_POST["semester"]);
$level = ($_POST["level"]);
}
?>
Here is the form method:
<form method="post" action="<?php echo($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
Here is to display the data in table form, and SELECT * is functioning
$sql = mysqli_query ($connection, "SELECT * FROM subject");
echo " <table>
<th>Semester</th>
<th>Level</th>
</tr>";
while($record = mysqli_fetch_assoc ($sql)){
echo "<tr>";
echo "<td>" . $record['semester'] . "</td>";
echo "<td>" . $record['level'] . "</td>";
echo "<td>" . "<input type=submit name=delete value=Delete>" . "</td>";
echo "</tr>";
}
This is the delete button code
if (isset($_POST['delete']))
{
$delete = mysqli_query ($connection, "DELETE FROM subject WHERE semester = '($_POST[semester])'");
}
Try this :
while($record = mysqli_fetch_assoc ($sql)){
echo "<tr>";
echo '<form action="mypage.php" method="post">';
echo "<td>" . $record['semester'] . "</td>";
echo "<td>" . $record['level'] . "</td>";
echo "<td>" . $record['course'] . "</td>";
echo "<td>" . $record['subject'] . "</td>";
echo "<td>" . $record['section'] . "</td>";
// And add field form hidden
echo '<input type="hidden" name="semester" value="'.$record['semester'].'">';
echo "<td>" . '<input type="submit" name="delete" value="Delete">' . "</td>";
echo "</form>";
echo "</tr>";
}
if (isset($_POST['delete']) && isset($_POST['semester']))
{
$stmt = $connection->prepare('DELETE FROM subject WHERE semester = ?');
// if $_POST['semester'] is integer else see http://php.net/manual/en/mysqli-stmt.bind-param.php
$stmt->bind_param('i', $_POST['semester']);
$stmt->execute();
}
I'm trying to design a control page for my website that allows admins to delete requests to use a 3D printer at my school. The page displays a query of the schedule from the MySQL database and puts a delete button next to every entry. However, I would like to link the information in each entry to its respective delete button and have no idea how to do that. You can see the page itself here: http://kepler.covenant.edu/~techclub/PHP/manage%20printer%20schedule.php and this is my code for displaying the information
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<th>Control</th>";
echo "<th>Student ID</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Date and Time</th>";
echo "<th>Description</th>";
echo "</tr>";
$key = 1;
while($row = $result->fetch_array()) {
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<tr>";
echo "<td><input name='".$row[5]."'type='submit' value='Delete' ></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No upcoming prints found!";
}
// free result set memory
$result->close();
}
I can't figure out how to link the information in a row with the delete button that I put in the row to send to the delete request.php program (which I have no code for yet)
you should put hidden field that hold each record id.
As below
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<input name='recored_id' type='hidden' value='".$row['id']."' >";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
Now from your request.php page you should do something like this
$recored_id = (int) $_POST['recored_id'];
$sql = "DELETE FROM table_name WHERE recored_id='$recored_id'";
Don't forget to give this code some security validation
all the best,
You're going to need some hidden inputs, like so:
while($row = $result->fetch_array()) {
echo "<tr>";
echo '<td><form method="POST" name="deleterequest" action = "deleterequest.php">';
echo "<input type='hidden' name='val0' value='" . $row[0] . "'>";
echo "<input type='hidden' name='val1' value='" . $row[1] . "'>";
echo "<input type='hidden' name='val2' value='" . $row[2] . "'>";
echo "<input type='hidden' name='val3' value='" . $row[3] . "'>";
echo "<input type='hidden' name='val4' value='" . $row[4] . "'>";
echo "<input name='".$row[5]."'type='submit' value='Delete' >";
echo "</form></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
You should change the name of these inputs though
I have a PHP script that generates some form on the page I have. I also have a table and submit button within the form which all works fine and the data is sent to the next page as intended. Now on the next page I have a PHP code that reads the $_POST variable. Here is my code:
echo "<form name=\"myForm\" id=\"myForm\" method=\"post\" action=\"\">";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = mysql_fetch_array($qryRes))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><input name=\"aName" . $row['id'] . "\" id=\"aName" . $row['id'] . "\" type=\"text\" value=\"" . $row['aName'] . "\" /></td>";
echo "<td><select name=\"gID" . $row['id'] . "\">";
while($gRow = mysql_fetch_array($grpsQry))
{
if($gRow['id'] == $row['groupID'])
echo "<option id=\"" . $gRow['id'] . "\" selected=\"selected\">" . $gRow['gName'] . "</option>";
else
echo "<option id=\"" . $gRow['id'] . "\">" . $gRow['gName'] . "</option>";
}
echo "</select></td>";
echo "</tr>";
mysql_data_seek( $grpsQry, 0 );
}
echo "</table>";
echo "<input type=\"submit\" name=\"submitForm\" value=\"Save\" />";
echo "</form>";
Then comes my PHP Code to read the $_POST:
if(isset($_POST['submitForm']))
{
print_r($_POST);
foreach($_POST as $x)
echo $x . "<br />";
}
Everything works fine, except that I need to read only the text field in the foreach loop as I will generate a query on them and will compare the values I will have with the value returned from the List ..
As yuo can see, the textfield is named aName then the id of the user. I need to read this only. But I will use it to generate a query which will be then compared to the value of the List gID.
try this:
foreach($_POST as $key => $value){
if(substr($key,0,5) == 'aName')
echo $value . "<br />";
}
//query to check if part id number exists in table ATTEND where service id = ...
$result2 = mysql_query("SELECT * FROM attend WHERE SIDno='$SIDno' and ServiceID='$id");
//if exists $ok = true;
if (mysql_num_rows($result2)>0) {
$ok == true;
}
echo "<tr bgcolor=$bgcolor>";
echo "<td><a name=$row1[0] id=$row1[0]>$row1[0]</td>";
echo "<td>" . $row1[1] . "</td>";
echo "<td>" . $row1[5] . "</td>";
echo "<td>" . $row1[2] . "</td>";
echo "<td>" . $row1[3] . "</td>";
echo "<td><input type='checkbox' name='checkbox[]' value=" . $row1[0];
if ($ok == true) {
echo 'disabled="disabled" checked="checked"';
}
echo "></td>";
echo "<input type='hidden' name='ServiceID' value=" . $id . ">";
echo "<input type='hidden' name='Year' value=" . $Year . ">";
echo "<input type='hidden' name='Stype' value='Recollection'>";
echo "</tr>";
}
}
echo "<tr>
<td colspan='5' align='right' bgcolor='#FFFFFF'><input name='SUBMIT1' type='submit' id='SUBMIT'value='SUBMIT'></td>
</tr>";
How can implement that on the next load if the value of the checkbox is already available in the database it will now be checked. but if it is not yet existing i can check it and save it to the database.
I'm guessing the problem lies in this line:
if (mysql_num_rows($result2)>0) {
$ok == true;
}
It should be:
if (mysql_num_rows($result2)>0) {
$ok = true;
}
In the first snippet you are just testing if $ok is equal to true, while in the second example an actual assignment to the variable is performed.
Remember:
= != ==