always last database line is deleted when deleting from php page - php

Im fetching data from database table when i m trying to delete a particular record no matter what i click last record get deleted what m i doing wrong, this is code m using after submit/click del button
// DELETE
if(isset($_POST['del']))
{
require'conn.php';
$delete_id = $_POST['del_id'];
print_r($_POST);
die;
$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);
$row=mysqli_affected_rows($conn);
if($row==1)
{
echo "<h1>".' sucess ! record was deleted' ."</h1>";
}
else
{
echo "<h1>".' record was not deleted '."</h1>";
}
mysqli_close($conn);
}
include'fetchtable.php';
and this is my table structure and del button code
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
}
?>
</form>

As Abhik Chakraborty says, you need a form for each row, or an other logic;
one solution is to put the <form ...>...</form> within the loop:
this is - AFAIK - not correct, in does not conform to HTML std. and works only with some browsers, bcause <table> and <form> ar mixed in wrong order, I use it only as egsample to show you problem
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo "<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">"
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
echo "</form>"
I would preferre to have only one hidden field in the form und to set the value of that field with onClick event of the submit button.
only the last lines again:
...
echo '<td> <input type="submit" name="del" value="delete" onclick="form.row_id.value='$the id$';"/>'
echo '<td> <input type="submit" name="edit" value="edit" onclick="form.row_id.value='$the id$';"/>'
echo"</tr>";
}
?>
<input ID='row_id' type="hidden" name="del_id" value="no set till now" />'
</form>

You need to have multiple for each action and so you need to have the code as
while (mysqli_stmt_fetch($stmt)){
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">' ;
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo "</tr>";
echo "</form>";
}
Also your code is not safe, you need to use mysqli_real_escape_string() for your POST data. or prepared statement

Related

Get checked checkboxes in PHP

I can't get the input of checkboxes in PHP.
Here is my code:
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' id='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
#this is the part that probably isn't correct.
if(!empty($_POST['check_list'])){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
I would like to get the amount of checkboxes checked.
The checkboxes are created in a loop with the input of a database.
Even if this would work, how would I get the id of all of the checked checkboxes?
It seems you are not using the form to submit. Place your table inside the form
<form action="" method="post">
<?php
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' value='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
You can get the post values
if($_POST){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
Simple form with checkboxes:-
<form name="" action="" method="post">
<input type="checkbox" name="gender[]" value="Male" />Male
<input type="checkbox" name="gender[]" value="Female" />Female
<input type="submit" name="submit" value="Submit" />
</form>
The PHP code to get the selected:-=
if(isset($_POST['gender'])){
$options = $_POST['gender'];
echo implode(',', $options);
}
If you want to pass the id you can do it like
<input type="checkbox" name="gender[2]" value="Male" />Male
<input type="checkbox" name="gender[3]" value="Female" />Female
You can loop through each option
foreach($options as $key => $value){
echo $key.'---'.$value;
}
//$key is the id sepcified, $values is the seected value

passing while loop value to another form

In the code bellow, I'm able to fetch values from the database. Now the retrieved values need to be passed to another form.
PHP:
<?php
require('administrator/connect-db.php');
$www_root = 'http://localhost/secure/cem/administrator/profile/';
$qry = mysql_query("SELECT * from dealer_package_details");
if(!$qry){echo mysql_error();}else{
while($row = mysql_fetch_array($qry)){
echo "<div class='col-sm-4 sm-margin-b-50'>";
echo "<form action='test.php'>";
echo "<div class='margin-b-20'>";
echo "<div class='wow zoomIn' data-wow-duration='.3' data-wow-delay='.1s'>";
echo '<img class="img-responsive" name="pport" src="', $www_root, '/', $row['pport'], '" alt="', $row['pport'], '"/>';
echo "</div>";
echo "</div>";
echo "<h3><a href='#' name='name_of_the_product'>" . $row['package_id'].$row['name_of_the_product']."</a></h3>";
echo "<input type='submit' name='submit' value='book now'>";
echo "</div>";
}
}
?>
The following code describes the second form where the values need to be passed.
HTML:
<form method="POST" action="insrt.php">
<input name="name_of_the_product" type="text" class="form-control name_of_the_product" value="<?php echo $_POST["name_of_the_product"]; ?>" readonly/>
<input name="package_id" type="text" class="form-control package_id" value="<?php echo $_POST["package_id"]; ?>" readonly/>
<input type="submit" name="submit" value="Save">
I suggest to use input hidden.
PHP:
...
echo "<input type='hidden' name='description' value='".$row['description]."' />";
...
HTML:
...
<input name="description" type="text" value="<?php echo $_POST['description']; ?>">
...

storing value from radio button to database with looping

I am trying now to get the value of every radio button to store in a database.
The trick is I'm using while loop to display the question and the choices.
Here is my code in displaying the questions and choices.
class questions{
function __construct(){
$db= new dbConnect();
}
public function fetchRandom()
{
$qr=mysql_query("SELECT * FROM questionaires INNER JOIN choices ON questionaires.q_id=choices.q_id WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires) ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>";
while($row=mysql_fetch_array($qr))
{
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
$ans_arr=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans_arr);
echo "<input type='radio' name='ans".$c."' value='".$ans_arr[0]."'>".$ans_arr[0]."</input><br>";
echo "<input type='radio' name='ans".$c."' value='".$ans_arr[1]."'>".$ans_arr[1]."</input><br>";
echo "<input type='radio' name='ans".$c."' value='".$ans_arr[2]."'>".$ans_arr[2]."</input><br>";
echo "<input type='radio' name='ans".$c."' value='".$ans_arr[3]."'>".$ans_arr[3]."</input><br>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
}
How can I store the answer of the user to database. Little help will be appreciated. Because I don't know how to code it from scratch. Thank you.
This is simple code try to code something like this.
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
<table>
<tr>
<td>Question 1</td>
<td><input type="radio" name="ques[ques1][ans1]" value="ans1" checked> Ans1
<input type="radio" name="ques[ques1][ans1]" value="ans2"> Ans2
<input type="radio" name="ques[ques1][ans1]" value="ans3"> Ans3
<input type="radio" name="ques[ques1][ans1]" value="ans4"> Ans4
</td>
</tr>
<tr>
<td>Question 1</td>
<td><input type="radio" name="ques[ques2][ans2]" value="ans1" checked> Ans1
<input type="radio" name="ques[ques2][ans2]" value="ans2"> Ans2
<input type="radio" name="ques[ques2][ans2]" value="ans3"> Ans3
<input type="radio" name="ques[ques2][ans2]" value="ans4"> Ans4
</td>
</table>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$data = $_POST['ques'];
foreach ($data as $key => $ans) {
echo 'Question :'. $key .' '. 'Answer :'. array_values($ans)[0].'<br/>';
}
}
Hope this will help you :).

Deleting a row in a database using php

I want to delete a row in a database that I have added but when i try to click the delete button in my index.php it says record deleted but when I click ok, it does not delete the record, here are my codes:
index.php:
$con = mysql_connect("localhost","pma");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("class_schedule", $con);
$result = mysql_query("SELECT * FROM schedule");
?>
<center>
<table border="1" width="800">
<tr>
<td colspan='8'><input type="text" width='300'/>
<input type="button" value="Search" onclick="location='search.php'" />
<input type="button" value="View All" onclick="window.location.href=''"/>
<input type="button" value="Add" onclick="location='add.php'"/></td>
</tr></table>
<?php
echo "<center><table border='1' width='800'>
<tr>
<td><center>Time</td>
<td><center>Subject</td>
<td><center>Course</td>
<td><center>Section</td>
<td><center>Day</td>
<td><center>Room</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['day'] . "</td>";
echo "<td>" . $row['room'] . "</td>";
?>
<td><input type="submit" value="Edit" name="edit" />
<form action="delete.php" method="post">
<input type="submit" value="Delete" name="delete" onclick="location='delete.php'" />
</td>
</form>
<?php
echo "</tr>";
}
echo "</table></center>";
mysql_close($con);
?>
And this is my delete.php:
<?php
require "connect.php";
$deltime=$_POST["deltime"];
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");
mysql_close($con);
?>
<div>
<p align="center"><b>Record Deleted</b><br/>
<form method="post">
<center><input type="submit" name="submit" value="Ok" formaction="index.php" /></center>
</form>
</p></div></form>
What am I missing, and what should I remove and add?
your id was not getting in delete.php file so just remove your delete button and replace with this code:
<td><a href="delete.php? id=<?php echo $row['id'];?>">Delete</td>
Modify the following line
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");
As
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'", $con);
Try to send the date through one link to the delete.php
<td><a href = 'delete.php'?date=<?php echo $row['time'];?>>DELETE</a></td>

MySQL data not fully being shown in html table. Data gets cut off and leaves half the field not the full

Here's the Code.
$result = mysql_query("SELECT * FROM situations");
echo "<table border='1'>
<tr>
<th>Case#</th>
<th>Cop(s)</th>
<th>Code</th>
<th>Vehicle/Person</th>
<th>Location</th>
<th>Division(s)</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<form action=situations.php method=post>";
echo "<tr>";
echo "<td>" . $row['Case#'] . " </td>";
echo "<td>" . "<input type=text name=cop value=" . $row['Cops']. " </td>";
echo "<td>" . "<input type=text name=sector value=". $row['Code'] . " </td>";
echo "<td>" . "<input type=text name=vehicle value=" . $row['Vehicle'] ." </td>";
echo "<td>" . "<input type=text name=location value=" .$row['Location']. " </td>";
echo "<td>" . "<input type=text name=division value=" .$row['Division']. " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" .$row['Case #'] . " </td>";
echo "<td>" . "<input type=submit name=update_situations value=Update" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
I have it so I can update it but my data is not fully shown. EX: I would have "Dukes Blvd" in Location and it would only show "Dukes" Please Help! and I am newb to PHP, MySQL.
You're missing quotes;
while($row = mysql_fetch_array($result))
{
?>
<form action="situations.php" method="post">
<tr>
<td><?php echo $row['Case#']; ?></td>
<td><input type="text" name="cop" value="<?php echo htmlentities($row['Cops'], ENT_QUOTES, 'UTF-8') ; ?>"></td>
<td><input type="text" name="sector" value="<?php echo htmlentities($row['Code'], ENT_QUOTES, 'UTF-8'); ?>"></td>
<td><input type="text" name="vehicle" value="<?php echo htmlentities($row['Vehicle'], ENT_QUOTES, 'UTF-8'); ?>"></td>
<td><input type="text" name="location" value="<?php echo htmlentities($row['Location'], ENT_QUOTES, 'UTF-8'); ?>"></td>
<td><input type="text" name="division" value="<?php echo htmlentities($row['Division'], ENT_QUOTES, 'UTF-8'); ?>"></td>
<td><input type="hidden" name="hidden" value="<?php echo htmlentities($row['Case #'], ENT_QUOTES, 'UTF-8'); ?>"></td>
<td><input type="submit" name="update_situations" value="Update"></td>
</tr>
</form>
<?php
}
?>
</table>
I don't disagree with ntgCleaner about ending PHP although some times its extra work. If you always use the same rules for quotes its not to big a problem plus after a while you recognize the syntax error right off or it is usually the first thing I look for when I get one. I always start with a single quote followed by a double quote inside the html portions.
echo 'some code "quoted code" more code'.$variable;
You can also use a backslash to id the quote \' to separate it from an error state.

Categories