I am trying to delete a row based on its ID
HTML:
<form action="" method="post">
<input type="submit" name="delete" value=" delete "/>
</form>
PHP:
$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if(isset($_POST['delete'])){
$db->exec("DELETE FROM infos WHERE id = '$u'");
}
Nothing happens when the delete button is pressed. How can I fix this?
You need an edit input whose value is the ID your want to delete.
<form action="" method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>
Replace $id with the actual variable you use in the script that creates the form.
<form action="" method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>
After that try this
$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if($u){
$db->exec("DELETE FROM infos WHERE id = '$u'");
}
Apparently when you are submitting only you posting data to php file.So you can check like this is enough.you told delete not happening so that i am posting this answer
Related
I created a category editing page on my site.
But that I'm trying to submit the category I want to edit the ID was not sent through form.
I'm unable to pull the ID.
edits_cat.php
<!--Form-->
<?php
if(isset($_POST['submit'])){
$id = $_GET['id'];
}
?>
<form action="edit_cat.php" method="post">
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Select Categorie</label>
<select name="edit" style="width:auto;" class="custom-select my-1 mr-sm-1" id="inlineFormCustomSelectPref">
<?php
while ($result = $query->fetch_assoc()){
echo "<option name='id' value='{$result['id']}'>{$result['cat']}</option>";
}
?>
</select>
<button name="submit" class="btn btn-primary" type="submit">Select a category</button>
</form>
<!--END Form-->
edit_Cat.php
$id = $_GET['id'];
$query = $mysqli->con->query("SELECT * FROM `categories` where id = '$id'");
$result = $query->fetch_assoc();
Replace this
<form action="edit_cat.php?id=<?php echo $id; ?>" method="post">
With this:
<form action="edit_cat.php" method="post">
And then Add Hidden Input Inside your form:
<input type="hidden" name="id" value="<?php echo $id; ?>" />
You are passing id via URL, so you can access it by $_GET['id']. If you want to access via $_POST['id'], you need to add a hidden input in your form.
<input type="hidden" value="<?php echo $id; ?>" />
I have a loop and submit form. I want to insert one query from a variable but when I click on the submit button last one (id number) variable inserts to the database and when I move $_POST['submit'] part inside the while all ID's insert to the database. How can I insert one of the variables from loop?
This is the while part:
<?php
$supql="SELECT * FROM `tbl_users_posts`";
$rez=mysqli_query($conn,$supql);
while ($row=mysqli_fetch_assoc($rez)) {
$psid=$row['id'];
echo $row['post'];
?>
<form id="form1" name="form1" method="post">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php } ?>
And this is the submit part:
if (isset($_POST['submit'])) {
$pid=$_POST['postid'];
$inslik="INSERT INTO t_plik (pid,uid)VALUES('$psid','$uid')";
mysqli_query($conn,$inslik);
}
thanks
<?php
$conn=mysqli_connect('localhost','root','','sitn');mysqli_set_charset($conn,"utf8");
$supql="SELECT * FROM `tbl_users_posts`";
$rez=mysqli_query($conn,$supql);
while ($row=mysqli_fetch_assoc($rez)){
$psid=$row['id'];
echo $row['post'];
?>
<form id="form1" name="form1" method="post">
<input type="hidden" name="postid" value="<?php echo $psid; ?>">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php }?>
<?php
if(isset($_POST['submit'])){
$pid=$_POST['postid'];
$inslik="INSERT INTO t_plik (pid,uid)VALUES('$psid','$uid') ";
mysqli_query($conn,$inslik);
}?>
Your form has no input with name="postid", so $_POST['postid'] wasn't being set. You can send this with a hidden input.
<form id="form1" name="form1" method="post">
<input type="hidden" name="postid" value="<?php echo $psid; ?>">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
The form should still be in the while loop, so you'll get a separate form for each post.
You also have a typo in the code that does the inserting:
$pid=$_POST['postid'];
should be:
$psid=$_POST['postid'];
So you weren't inserting the ID that was submitted.
I'm probably dumb, but I read all the questions made here on similar arguments but I couldn't be able anyway to solve my problem (and I'm aware it's a stupid simple problem).
In the page city.php there are two identical forms with two identical linked to the same page overview.php; like this:
<form method="get" action="overview.php">
<input type="submit" value="Gallery">
</form>
...
<form method="get" action="overview.php">
<input type="submit" value="Gallery">
</form>
Since I want to display different contents depending on the button the user presses, I thought to put an unique id like:
<form method="get" action="overview.php">
<input type="submit" id="1" value="Gallery">
</form>
...
<form method="get" action="overview.php">
<input type="submit" id="2" value="Gallery">
</form>
I know it's stupid, but I tried all the methods to get the id, unsuccesfully.
'Cause in overview.php I want to perform a check like:
$choiche = $_GET['id'];
if($choice == '1')
{echo '...display content X...';}
elseif ($choice =='2')
{echo '...display content Y...';}
else
{echo 'Error';}
Thanks in advance!
2 ways:
You can either adjust the form's action:
action="overview.php?id=1"
or you could do something like:
<form method="get" action="overview.php">
<input type="hidden" name="id" value="1" />
<input type="submit" value="Gallery" />
</form>
This will allow you at the next page request to do your code of:
$choice = $_GET["id"];
Since you want to use a Link instead, you can do it by setting up the following line: Gallery
Put name instead of id
<form method="get" action="overview.php">
<input type="submit" name="1" value="Gallery1">
</form>
...
<form method="get" action="overview.php">
<input type="submit" name="2" value="Gallery2">
</form>
You should do something like this:
This is the "Form1.php"
<form method="post" action="overview.php">
<input type="hidden" id="redirectTo" name="redirectTo" value="Type1">
<button type="submit">Go</button>
</form>
<form method="post" action="overview.php">
<input type="hidden" id="redirectTo" name="redirectTo" value="Type2">
<button type="submit">Go</button>
</form>
In "overview.php" you should have something like this:
<?php
$redirectTo $_POST["redirectTo"];
if($redirectTo == 'Type1'){
?>
<html>Make all you want</html>
<?php
}
if($redirectTo == 'Type1'){
?>
<html>Make all you want</html>
<?php
}
?>
I have a table that is in a foreach loop, and every iteration the hidden input receives the ID from the database. My table has in each column an input field because I'd like to edit the data from the database right in the table, and to make it happen I had to put a submit button that when clicked retrieves the ID from the hidden input into another file that then does the operation.
<form action="../php/Operations.php" method="post">
//...
<?php foreach ($Service->select() as $col) { ?>
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
//...
The problem is that the $_POST value is always the very first from the table, because there are n hidden input with the same name.
I'd like to know if there is a way to retrieve the hidden input's value from the clicked row, having in mind I'm not using $_GET, but a submit button,
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
that when clicked is supose to execute the code I wrote:
switch($_REQUEST["submit"]) {
case "update":
$ServiceDatabase->update($_POST["id"]);
break;
//...
Thank you.
If you want multiple inputs with the same name use name="id[]" for the input name attribute. $_POST will then contain an array for name with all values from the input elements. Then you can then loop over this array.
Example:
<form method="post">
<input type="hidden" name="id[]" value="foo"/>
<input type="hidden" name="id[]" value="bar"/>
<input type="hidden" name="id[]" value="baz"/>
<input type="submit" />
</form>
PHP
$ids= $_POST['id'];
print_r($ids);
EDIT: You can change your code like this so each submit button is tied to exactly one hidden input with the name="id".
<?php foreach ($Service->select() as $col) { ?>
<form action="../php/Operations.php" method="post">
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
</form>
<?php } ?>
Rendered HTML:
<form action="../php/Operations.php" method="post"><input type="hidden" value="1234" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="5678" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="9101112" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
i would recommend you to use ajax for submitting the data
Just create a click event in for the button and get the button id
in your htm form create an id attribute like this
<input type="hidden" id="colId<?php echo $col["id"]; ?>" value="<?php echo $col["id"]; ?>" />
and your button should look like this
<button class="buttonSubmit" id="<?php echo $col["id"]; ?>" >Submit</button>
$(".buttonSubmit").click(function(){
var id = $(this).attr("id");
var hiddeninput = $("colId"+id).val();
// here you can process how would you send the data to a php file
});
Either wrap each col in a separate form and have a separate submit button per col or make the name attribute an array. i.e. name="id[<?php echo htmlspecialchars($id); ?>"
So I am having an issue. I used POST to send data to a new page. I use get to send data to a function but it seems the POST data get wiped. Here some code to help explain.
POST CODE to send to form vieworder (works perfect!)
<form method="post" action="vieworder.php">
<input type="hidden" name ="user_id" value="<?php echo $_SESSION['user_id']; ?>">
<input type="hidden" name ="id" value="<?php echo $data1[$x]['id']; ?>">
<input type="submit" name="submit" value="View"> </td>
</form>
So on the vieworder page I want used to be able to update the data using this form.
This form works as well except i need that value "id" from the orginal post. It works and the "id"has the data until I use this form.
<form name="approveform" method="get" action="">
Index Number*: <input type="text" name="IndexNum">
<input type="submit" value="Approve" action="">
</form>
I would also prefer to use the POST method but using GET was my first solution to no deleting the data from POST.
Anyways I then just send the data to a function to update two fields.
Any way to get correct the code?
<?php
$id=$_POST['user_id'];
?>
<form name="approveform" method="get" action="">
Index Number*: <input type="text" name="IndexNum">
<input type='hidden' value='<?php echo $id;?>'>
<input type="submit" value="Approve" action="">
</form>