PHP Delete not working as I would I like - php

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>';
}

Related

Problem with <input type ="hidden"/> php/html

I currently have the problem that the value from the input type hidden "var" doesnt exist :(
<form id ="bewerbung" name = "bewerbung" action="./edit" method="POST">
<div class="form-group">
<input type="hidden" value="Supporter" id ="type" name="type" />
<button type="submit" value="Submit" class="form-control-submit-button">Bewerbung</button>
</div>
The edit page :
if (isset($_POST['type'])){
echo "set";
}else {
echo "not set";
}
the output : not set :/
Thanks #Aaron, the final Solution was to add an "/" at the end of the action.
just like :
<form method="POST" action="./edit/">

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";
}
?>

Delete post by id fails everytime

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.

to add an input element to the form dynamically using only PHP

I would like to add an input element to the form dynamically using only PHP.
I know how to make this using php and JavaScript combination, thus do nto advice abotu JavaScript.
The example below does not work. Could you please advice and comment:
input.php
<br> <input type="text" name="mob[]" value="" size="3" >
form.php
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount'];
$AddNumCount=$AddNumCount+1;
echo $AddNumCount;
}
if( isset($_POST['register']) ){
print_r($_POST['register']);
}
if (!isset($AddNumCount)) {$AddNumCount=5;}
?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<br>
<?php for ($i=0; $i<$AddNumCount; $i++) { Include('input.php'); } ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value=" <?php $AddNumCount; ?> "> </p>
<br></form><input type="submit" name="register" id="regcont" value="register"> </p>
</form>
Maybe you know how to make single submit button for many forms?
I mean each input would be a separare form and all forms can be submittted with the button on the end?
You use two action attrs. Maybe you mean:
<form method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
For submitting many forms by one button - you need to use JavaScript and send them in cycle via AJAX.
I am sorry i change this post. This is working example for dynamical PHP.
Use EXform.php. Other files are generated or helping.
Maybe it is also possible to make this using Session variables and header for redirecting to regenerated webpage.
EXform.php
<?php if (isset( $_POST['AddNum'])) { Include("GENinput.php"); } ?>
<?php if (!isset( $_POST['AddNumCount'])) { $_POST['AddNumCount']=1; Include("GENinput.php"); } ?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<?php Include("INCinput.php"); ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value="<?php echo $AddNumCount; ?>"> </p>
<input type="submit" name="register" id="regcont" value="register"> </p>
<br></form>
</form>
GENinput.php // generates included file
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount']; //=
$fnameinp="INCinput.php";
$fileinp=fopen($fnameinp,"w");
$_POST['AddNumCount']=$AddNumCount=$AddNumCount+1;
//echo "AddNumCount=".$AddNumCount;
$strV=""; $stri="";
for ($i=0; $i<$AddNumCount; $i++) {
$strV.=" \n
<?php
if( isset(\$_POST['v']['tname']['colname'][".$i."]) )
{ \$v['tname']['colname'][".$i."]=\$_POST['v']['tname']['colname'][".$i."];}
else { \$v['tname']['colname'][".$i."]=".$i."; }
?>
";
$stri.=" <br> <input type=\"text\" name=\"v[tname][colname][".$i."]\" value=\"<?php echo \$v['tname']['colname'][".$i."]; ?>\" > \n\n";
}
fwrite($fileinp,$strV);
fwrite($fileinp,$stri);
fclose($fileinp);
}

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

Categories