Delete post by id fails everytime - php

I have a newsfeed where people can upload things, and on there I have a delete button. I read a few techniques you could do to delete a row from the database.
I used this one by using input type hidden field etc.
HTML
<form action="logic/delete_post.php" method="GET">
<input type="hidden" name="id" value="?id='.$pica['img_id'].'" />
<input type="submit" name="deleteSubmit" value="Delete" class="delete_post" />
</form>
DELETE_POST.PHP
<?php
if(isset($_GET['deleteSubmit'])) {
$img_id = substr($_GET['id'],4,6);
if(isset($_GET['id'])) {
$result = $mysqli->query("SELECT picas.img_id FROM picas WHERE username='$ses_user'");
$mysqli->query("DELETE FROM picas WHERE img_id='$img_id'");
if (mysqli_affected_rows() == 1) {
echo 'Succes!';
} else {
echo 'Damn!';
}
}
}
?>
Every comment with help is appreciated!

Remove the questionmark:
<input type="hidden" name="id" value="?id='.$pica['img_id'].'" />
^
And make this look like this:
<input type="hidden" name="id" value="'.$pica['img_id'].'" />
Then
$img_id = $_GET['id'];
And addslashes() should be used if this is not enabled in PHP.ini for request values.

Related

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

Getting a value from text input field, then displaying on POST

I'm trying to get a value from a form, then display it on posting of the form. I can get the value to appear in the second text field, once I have chosen an option using the Ajax Auto-Select, but how do I get that value shown stored into a variable for display on posting? This is what I have been trying -
if ($_POST['action'] == 'getentity') {
$value= $entity;
$content .= '<div>'.$value.' hello</div>';
}
<form method="post" action="?">
<input type="text" name="TownID_display" size="50" onkeyup="javascript:ajax_showOptions(this,\'getEntitiesByLetters\',event)">
<input type="text" name="TownID" id="TownID_display_hidden" value="'.$entity.'" />
<input type="hidden" name="action" value="getentity" />
<input type="submit" name="submit" value="Find"/>
Many thanks for any help.
Try
<input type="text" name="TownID" id="TownID_display_hidden" value="<?php $value = $entity; echo $entity; ?>" />
and its better to use like this
if ($_POST['action'] == 'getentity') {
$value= $_POST['TownID'];
$content .= '<div>'.$value.' hello</div>';
}
it should work.

PHP Delete not working as I would I like

Can someone help me. My delete code below works, but it's deleting the most recent Favorited file and not the specific file chosen. Here's the code:
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>';
if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$vid));
}
}
You need to add a hidden form field that contains the Thread ID into your form, then read that back in your form handler, something like this:
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="hidden" name="thread" value="' . $vid . '" />
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>';
if(isset($_POST['submit']))
{
$id = $_POST["thread"];
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$id));
}
}
The reason for this is because you have the If statement in your while loop. The logic in the code you have given is to delete records when $_POST['submit'] is set. So it will follow the loop to delete the records and not a specific record.
You need to pass the id you want to delete to the user, as you are using form to do this, have a hidden field with the id.
if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$_POST['id']));
}
while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];
$List.='<form action="" method="POST" id="postForm">
<div class="LISTT">'.$preview.'<br/><label id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
<input type="hidden" name="id" id="id" value="'.$id.'" />
</div></form>';
}

how to send value to the from action page from database

I am creating a faq panel for there can be multiple answers for question and i want to take the answer id .because i am storing comment by answer id
the problem is that how to sent the $answer_id to the comment_submit_process.php and how to recognize the answer ?
$selected_ques= mysql_prep($_GET['ques']);
$query = "SELECT * FROM formanswer where question_id = {$selected_ques}";
$ans= mysql_query($query);
if($ans){
while($answer = mysql_fetch_array($ans))
//here is the form
<form id="add-comment" action="comment_submit_process.php" >
<textarea class="comment-submit-textarea" cols="78" name="comment" style="height: 64px;"></textarea>
<input type="submit" name="submitbutton" value="Add Comment" class="comment-submit-button" >
<br> <?php
$ans_id= $answer['id']; //i am fatching the $answer['id'] from database
?>
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>" />
<span class="counter ">enter at least 15 characters</span>
<span class="form-error"></span>
</form>
<?php }} ?>
You might have typo here !! it should be..
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
Other thing, you can add get param to action link it self.
<form id="add-comment" action="comment_submit_process.php?<?php echo $answer['id']; ?>" >
Instead of setting the ans_id, every time to the hidden field .
Generate a string of ans_id seperated with "," until while loop ends append the string and assign that value to the hidden field and in form action page you can get that value and generate van array from that string with delimiter ",".Now you can have the array of ans_id in your form action page
$answer_array = "nothing";
while($answer = mysql_fetch_array($ans))
{
if( $answer_array == "nothing")
$answer_array = $answer;
else
$answer_array .= ",".$answer;
}
<input type="hidden" name="answer_arr" value="<?=$answer_array?>">
In Form action page you can get that hidden value
$ans_array= explode(",",$_GET['answer_arr']);
You can echo answer_id in form action tag as additional parameter like this:
<form id="add-comment" action="comment_submit_process.php?ans_id=$ans_id" >
//Your stuff here
</form>
in comment_submit_process.php you can identify answer by
$ans_id=$_GET['ans_id'];
You can do further processing by using $ans_id
Edit:
change this line:
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>"
to:
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
so that value of that field would be $ans_id fetched from DB.

refresh another page from current php

First let's explain what I want to do and then ask my question!
Well, I want to use a search filter for a query (the user should choose by which field will search the database, eg by name, code or fname) After the query runs, I want to show the data in some textfields, so the user can change them.
To do this, I put the first part (search filter-radio group- and filter value-text field-) in my first page(getStudentFilter.php). On submit, the query runs, I put values in SESSION and opens the second page(change_user.php) with my correct data!
If user change student's data, the update in db is ok, but in page change_user.php it shows the initial data again.
I tried to change SESSION values so I can keep my new values before run the update query, but it seems wrong.
Can someone give me a solution so I can fix the problem? Can this be done as it is or I have to change it and put both queries (select and update) in one form? Aaahh, I tried to put both in one form but I don't know how to control two "submit" in one form...
Thanks in advance..
my code after changes is
<form name="change_student" method="post" enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" >
<?php
if ( isset($_POST['upd_student']) && $_POST['upd_student'] = 'Change' ){
echo "UPDATE";
//RUN UPDATE QUERY
}
elseif( isset($_POST['get_filter']) && $_POST['get_filter'] == 'Show' ){
echo "SELECT";
$query = "select * from student where ".$_POST['filter']."='".$_POST['filter_val']."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$id = $row['idstudent'];
$fn = $row['fname'];
$ln = $row['lname'];
$ph = $row['phone'];
$sc = $row['school_dept'];
echo "<META HTTP-EQUIV='Refresh' CONTENT='0' >";
}
?>
<table width="310">
<tr><td><label><b>FILTER</b></label></td> </tr>
<tr><td><label><input type="radio" name="filter" value="idstudent" id="filter_5">ID </label></td></tr>
<tr><td><label><input type="radio" name="filter" value="fname" id="filter_3">FIRST NAME</label></td></tr>
<tr><td><label><input type="radio" name="filter" value="lname" id="filter_4">LAST NAME</label></td></tr>
<tr><td><input type="text" name="filter_val"> </td></tr>
<tr><td><input type="submit" name="get_filter" id="get_filter" value="Show"></td></tr>
</table>
<table>
<th colspan="2">STUDENT'S DATA</th>
<tr><td>ID</td><td><input type="text" name="st_id" value="<?php echo $id?>"></td></tr>
<tr><td>FIRST NAME</td><td><input type="text" name="fname" value="<?php echo $fn?>"></td></tr>
<tr><td>LAST NAME</td><td><input type="text" name="lname" value="<?php echo $ln?>"></td></tr>
<tr><td>PHONE</td><td><input type="text" name="phone" value="<?php echo $ph?>"></td></tr>
<tr><td>DEPT</td><td><input type="text" name="dept" value="<?php echo $sc?>"></td></tr>
</table>
<input type="submit" name="upd_student" value="Change">
</form>
Do not put search params into session.
Do not use 2 pages.
Make it all on one page and pass search parameters using GET method, like every search facility does.
To control 2 submits in one form you would have to test the values in you're php script .
Let's take the following html form:
<form action="index.php" name="contestForm" id="contestForm" method="POST">
<input type="submit" value="Select" name="select" />
<input type="submit" value="Update" name="update" />
</form>
Now in you're php script you would do this :
if ( isset($_POST['update']) && $_POST['update'] = 'Update' )
{
//do the update part
echo "UPDATE";
} elseif ( isset($_POST['select']) && $_POST['select'] == 'Select' )
{
//do the select part
echo "SELECT";
}

Categories