I am having difficulty deleting rows from my database.
I have a delete button in a form that when it is clicked then performs a DELETE FROM query but it doesnt work and Im wondering whether my theory is completly wrong (the theory being that having a form and submit button to INSERT data into a database works so why not use that to delete stuff? This is the code
$league_id = $_GET['id'];
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
if ($_POST['ooops']) { //if the data is rubbish then delete and start again...
$delete_lge_sql = "DELETE FROM st_position WHERE league_id = '$league_id'";
$delete_lge_res = mysqli_query($statto, $delete_lge_sql)
or die(mysqli_error($statto));
}
When I click Delete Entries the page reloads and the URL looks like this
page.php?ooops=Delete+Entries
Many thanks for any help
You missed a double-quote in this statement
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
Change it to
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
Try :
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"hidden\" name=\"ooops\" value=\"1\" />
<input type=\"submit\" value=\"Delete Entries\">
</form>";
This way, your $_POST['ooops'] variable will be equal to "1". Just make your test on this value and it will be ok.
Related
I guess the title got complicated, I got a Userlist where a table shows users and it should get a option to delete the user. So I requested all users from the database, and placed them in a table, but a delete-button should be next to every user, to delete it. I would like to call the button-text "delete" but to gain the ID of the user, I thought of giving the value-attribute the database-id to send that value to the $_POST var.
This is what I made so far:
if ($con) {
$sql = "SELECT * FROM user ";
$ergebnis = $con->query($sql);
while ($zeile = $ergebnis->fetch_assoc())
{
echo "<table>
<tr><td><h4> User: " . $zeile["user"] . "</h4></td></tr>
<tr><td> <form action='admin.php' method='post'> <input type='submit' name='delete' value='" . $zeile["id"] . "'></td></tr> </form>
</table>";
}
}
The list as an image:
So every user has it's ID as an value, have a $_POST varaiable to work with. But of course, the text shouldn't be the value but something like "delete". Is there an alternative way to do it?
I hope you get my question..
Make your form as:
<form action='admin.php' method='post'>
<input type='submit' name='delete' value='Delete user'>
<input type='hidden' name='user_id' value='" . $zeile["id"] . "'>
</form>
<!-- Also not that first you close `form` and only then - `td-tr` -->
On server side you will have user id in $_POST['user_id'].
I am making a webpage that allows you to add friends. For the confirm part I made a confirm button that made a post redirect to the confirm page:
while($rs=$friendsToConfirm->fetch_row()) {
echo "Friends $count to confirm's ID: $rs[0]";
echo "
<form name=\"confirm\" method=\"post\" action=\"confirmfriend.php\">
<input type=\"hidden\" name=\"userID\" value=\"$userID\">
<input type=\"hidden\" name=\"friendID\" value=\"$rs[0]\">
<input type=\"submit\" value=\"Confirm\">
</form>
";
echo "<br>";
$count++;
}
The $friendToConfirm variable is all the friends needed to confirm and is retrieved from MySQL. When I hit the button, I was expected to receive the userID and friendID from confirmfriend.php, but I did not receive anything using $_POST['userID'];. Is there another way to do this or an I doing something wrong.
This is because all of your forms have the same name [confirm]. You can try this:
$sl = 1;
while($rs=$friendsToConfirm->fetch_row()) {
echo "Friends $count to confirm's ID: $rs[0]";
echo "
<form name=\"confirm{$sl}\" method=\"post\" action=\"confirmfriend.php\">
<input type=\"hidden\" name=\"userID\" value=\"$userID\">
<input type=\"hidden\" name=\"friendID\" value=\"$rs[0]\">
<input type=\"submit\" value=\"Confirm\">
</form>
";
echo "<br>";
$count++;
$sl++;
}
Hope this will work.
Both of your hidden input fields are called "userId". I imagine the 2nd one needs to be renamed to "friendId".
All of the forms will also have the same name. It is usually preferred to ensure form names are unique.
If $_POST ['userId'] exists but is blank, check that $userId isn't blank.
To debug you can just var_dump the post array.
For the last 4 hours I've been struggling to get something to work. I checked SO and other sources but couldn't find anything related to the subject. Here is the code:
<?php
$email=$_SESSION['email'];
$query1="SELECT * FROM oferte WHERE email='$email'";
$rez2=mysql_query($query1) or die (mysql_error());
if (mysql_num_rows($rez2)>0)
{
while ($oferta = mysql_fetch_assoc($rez2))
{
$id=$oferta['id_oferta'];
echo "<input type='radio' name='selectie' value='$id' id='$id'> <a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo "</br>";
}
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
echo "</form>";
echo "</div>";
}
else
{
}
?>
The while drags all of the user's entries from the database and creates a radio button for each one of them with the value and id (because I don't really know which one is needed) equal to the entry's id from the db. I echoed that out and the id is displayed as it should so no problems there.
The delete script works ok as well so I won't attach it unless you tell me to. All good, no errors, until I try to delete an entry. Whatever I choose from the list of entries, it will always delete the last one. Note that I have two other inputs echoed out, those will be the "view" and "modify" buttons for the entry.
I really hope this is not JavaScript related because I have no clue of JS. I think this will be of major help to others having this problem. Please let me know if I need to edit my question before downrating. Thanks!
After edit:
This is the delete script, which as I said earlier works fine.
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
echo $id;
require_once('mysql_connect.php');
$query = "DELETE FROM oferte Where id_oferta = '$id'";
mysql_query($query) or die(mysql_error());
//header('Location: oferte.php');
}
else
{
//header('Location: oferte.php');
}
?>
The session is started as well, like this:
<?php
session_start();
?>
The reason the last $id is deleted is because this line is outside/after the while loop:
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
You want to move this line inside the loop so that you have a button that executes delete for each radio button.
Update:
To have links to delete and
echo "<input type='radio' name='selectie' value='$id' id='$id'> ";
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
Also I do not think the radio button is needed here at all since you are not really doing anything with it. You could simply echo out the value of your choice and have these links as follows:
echo $oferta['denumire_locatie'] . ' '; // replace $oferta['denumire_locatie'] with something of your choice
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
echo "<br />";
The problem, in this case, is JavaScript related, yes. What I recommend you to do is to simply add a Remove link for each item.
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo " - <a href='delete.php?id={$oferta['id_oferta']}'>Remove</a>";
echo "</br>";
Your $id is outside your while() loop.
The last one is getting deleted because the $id has the last one's value when the loops is exited.
Include all your code :
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
Inside your while loop.
When the rendered html reaches the browser, it will be something like this:
<input type='radio' name='selectie' value='1' id='1'> <a href='oferta.php?id=1'>TEXT</a>
<input type='radio' name='selectie' value='2' id='2'> <a href='oferta.php?id=2'>TEXT</a>
<input type='radio' name='selectie' value='3' id='3'> <a href='oferta.php?id=3'>TEXT</a>
<input type='radio' name='selectie' value='4' id='4'> <a href='oferta.php?id=4'>TEXT</a>
<br/>
<input type="button" id="sterge" value="Sterge" onclick="window.location.href='delete.php?id=5'" />
With this markup you won't be able to accomplish what you want without using javascript to update the onclick attribute whenever you select a radio button.
On the other hand, instead of using the client-side onclick event you can use the button's default behaviour, which is to submit the form.
You'll just have to set the action attribute:
<form method="post" action="http://myurl.php">
and write the myurl.php page which will just read the posted variable $_POST['selectie'] and call the delete method with the posted id.
I have made a yes/no quiz where the game tries to guess what creature you are thinking of based on these questions, there are only 16 possible answers.
What I am trying to is make it so that if the user closes the site part-way through the quiz then when they return to the quiz they will taken to the same question they left off on. Below is what I have done so far but it doesn't work properly (see for yourself at http://s504518.brunelweb.net/Creatures.php) You must answer yes to the first question and then close the browser as I have only done a session for that question so far. Here is part of the what I have done so far
//These are used for saving the session
$q2 = "<div class='questions'><p>{$questions[0][0]}</p></div>";
$q3 = "<div class='questions'><p>{$questions[0][1]}</p></div>";
$f2 = "<div class='questions'><form method ='GET' action='Creatures.php'>
<input type='submit' name='answer1' value='Yes' class='buttons' />
<input type='submit' name='answer1' value='No' class='buttons' />
</form></div>";
$f3 = "<div class='questions'><form method ='GET' action='Creatures.php'>
<input type='submit' name='answer2' value='Yes' class='buttons' />
<input type='submit' name='answer2' value='No' class='buttons' />
</form></div>";
//If start button has not been pressed, display nothing
if (!isset($_POST['start'])){
} //If start button has been pressed, display questions
else
{
//Display the first question
echo $firstquestion;
echo "<div class='questions'><form method ='GET' action='Creatures.php'>
<input type='submit' name='yes1' value='Yes' class='buttons' />
<input type='submit' name='no1' value='No' class='buttons' />
</form></div>";
}
//Question 2
if (isset($_SESSION['form']))
{
echo $_SESSION['question'];
echo $_SESSION['form'];
}
else{
if ($_GET['yes1']) //If answer to Q1 is yes then display this
{
echo "<div class='questions'><p>{$questions[0][0]}</p></div>";
showquestion(1);
$_SESSION['question'] = $q2;
$_SESSION['form'] = $f2;
}
}
What I am trying to achieve here is, if a session has already been set then display the question and form from that session and if it hasn't display nothing until they click the start button. I'm not sure why it doesn't work properly and any help would be much appreciated.
You need to create a session with session_start(), put this into the first line of php:
session_start();
As php.net tells me, it will also use already generated sessions. Just be shure you have no html-output before starting the session
the below code works perfectly in FF and CHROME but not in IE. Please help. I have commented out my santize functions as i thought they might be affecting it, but it still does the same.... nothing in IE.
Thank you in advance for any assistance.
<?php
//IF UPDATE BUCKET CHANGE STATUS...
if(isset($_POST['updatebucket'])){
$complete = $_POST["complete"];
$bucketid = $_POST["bucketid"];
//$complete = sanitizeone($_POST["complete"], "plain");
//$complete = strip_word_html($complete);
//$bucketid = sanitizeone($_POST["bucketid"], "plain");
//$bucketid = strip_word_html($bucketid);
if ($complete=="1")
$complete = "0";
else
$complete = "1";
$updatebucket = "UPDATE membersbuckets SET complete = '$complete' WHERE userid = '$userid' AND bucketid = '$bucketid'";
mysql_query($updatebucket);
}
?>
and the front end....
<? if ($complete=="1") {
echo "<form action='' method='post' name='updatebucket'><input name='complete' type='hidden' value=" .$complete. " /><input name='userid' type='hidden' value=" .$userid. " /><input name='bucketid' type='hidden' value=" .$bucketid. " /><input type='image' name='updatebucket' value='updatebucket' src='images/tick.png' /></form>";
}else{
echo "<form action='' method='post' name='updatebucket'><input name='complete' type='hidden' value=" .$complete. " /><input name='userid' type='hidden' value=" .$userid. " /><input name='bucketid' type='hidden' value=" .$bucketid. " /><input type='image' name='updatebucket' value='updatebucket' src='images/cross.png' /></form>";
}
?>
Dan
You should post your front-end, not back-end (since it's pretty much not browser-dependant).
Your HTML probably isn't valid.
Edit:
Yep, IE doesn't take value for image type of input. It only sends the x & y (field_name_x, field_name_y) and totally discards the original "value" attribute.
Try with a hidden input instead.
It seems that input type='image' doesn't send the value when used from IE. You'll need another hidden field:
<input type='hidden' name='updatebucket' value='updatebucket' />
<input type='image' src='images/tick.png' />
That way, the updatebucket parameter will be posted to the server, regardless of the browser used.
The assumption here was that all browsers handle HTML forms the same way (and they don't); that's why I keep Eric Lawrence's excellent Fiddler around - it can diff two HTTP requests, so you'll see the difference between the browsers immediately.
An alternative would be to check for $_POST[{image-element-name}_x}] (in this case $_POST['updatebucket_x']. All browsers will send the x/y coordinates of the image element as updatebucket.x & updatebucket.y, and PHP silently (and frustratingly) alters the updatebucket.x to updatebucket_x. Then again, you only need this is clicking different input type=submit / type=image elements would alter the action taken, otherwise the previous solution of a hidden element as previously suggested would do.