my if condition isn't working with the $_GET['delete'] condition. How to solve this? I'm want to delete a row from a table by reading the url. if the url contains the work delete this the mysql row should be deleted. what is the wrong i'm doing here?
<?php
$query = "SELECT * FROM category";
$select_category_id_and_title = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_category_id_and_title) )
{
$cat_id = $row['id'];
$cat_title = $row['cat_title'];
echo "<tr>";
echo "<td>$cat_id</td>";
echo "<td>$cat_title</td>";
echo "<td><a href='admin_category_dashboard_new.php?delete = {$cat_id}'>DELETE</a></td>";
echo "<td><a href='admin_category_dashboard_new.php?edit = {$cat_title}'>EDIT</a></td>";
echo "</tr>";
//echo $_GET['delete'];
}
?>
<?php
if(isset($_GET['delete'])) // doesn't work
{
$delete_cat_id = $_GET['delete'];
echo '<h1>' . $delete_cat_id . '</h1>';
$query = "DELETE FROM category WHERE id = $delete_cat_id ";
$Cat_id_delete_query = mysqli_query($connection, $query);
if(!$Cat_id_delete_query)
{
die("error" . mysqli_error($connection));
}
header("Location: admin_category_dashboard_new.php ");
}
?>
You've included a space after 'delete' in your URL.
Thus the $_GET index you would need to look for is 'delete_'
try the following in a file
var_dump($_GET);
then go to yourfile.php?delete = any
then retry with yourfile.php?delete=any
see the difference
If the condition is not firing at all, then it is probably because:
You misspelled delete in your URL.
delete has a null value.
The isset() function returns false even if the value is null; not just if the key doesn't exist.
Related
I have cleaned my code a little to have the following as my form. But I'm having trouble sending the data and Updating from the new update.php. The form works ok retrieving the data and displaying it. But on submission I get the ok update message but the record isn't changed in the database any ideas.
index.php
<?php
include 'connectdb.php';
// include 'query.php';
$sql = "SELECT id, WeightorMeasure FROM weightsmeasures";
$result = $conn->query($sql)
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<form action=\"update.php\"method=\"post\">";
echo "<input type=\"text\" name=\"id\" value = ".$row["id"].">";
echo "<input type=\"text\" name=\"WeightorMeasure\" value = ".$row["WeightorMeasure"] .">";
echo "<input type=\"submit\" value=\" Submit \" name=\"Update\">";
}
echo "</form>";
} else {
echo "0 results";
}
$conn->close();
?>
update.php
<?php
include 'connectdb.php';
$wm = $_POST['id'];
$id = $_POST['WeightorMeasure'];
$sql = "UPDATE weightsmeasures SET WeightorMeasure='$wm' WHERE id='$id'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
$conn->close();
?>
Have changed to Below and now get this error.
Error updating record: Unknown column 'sdada' in 'field list'. So it looks like its trying to use the form value $wm as a column header in the table rather than the input value.
$wm = $_POST['WeightorMeasure'];
$id = $_POST['id'];
$sql = "UPDATE weightsmeasures SET WeightorMeasure=$wm WHERE id=$id";
$wm = $_POST['id'];
$id = $_POST['WeightorMeasure'];
Maybe you have these the wrong way round? :D
$wm = $_POST['WeightorMeasure'];
$id = $_POST['id'];
By the way your query is vuln to MySQL injection, please consider using prepared statements
You realize that you switched your ID and WeightOrMeasure in the variable assignments from your $_POST data?
This results in an update query that can't find the ID but does not run into a problem. Thus telling you that the operation was successful
I figure out following possible problem in your code.
mysqli_query($conn, $sql); //should be $conn->query($sql);
and this line
$wm = $_POST['id']; //$_POST['WeightorMeasure'];
$id = $_POST['WeightorMeasure'];//$_POST['id'];
the order is wrong. I hope you already have $conn object created in dpconnect.php file.
Ok found the problem was a mixture of the above having $_POST["WeightorMeasure"]; and $_POST["id"]; mixed up but the most important factor was that the table I was posting from contained Multiple Rows and on _POST to update.php it didn't know what to do with all the different rows as the SQL was only dealing with one row. Once I sent single rows through the post it worked fine. Now to learn and add prepared statements as suggested.
update.php
<?php
include 'connectdb.php';
$wm = $_POST["WeightorMeasure"];
$id = $_POST["id"];
echo $wm . "<br>";
echo $id . "<br";
$sql = "UPDATE weightsmeasures SET WeightorMeasure=\"$wm\", id=
$idWHERE id= $id";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
$conn->close();
?>
Manual single entry.
index.php
<?php
include 'connectdb.php';
// include 'query.php';
$sql = "SELECT id, WeightorMeasure FROM weightsmeasures WHERE id=11";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<form action=\"update.php\"method=\"post\">";
echo "<input type=\"text\" name=\"id\" value = ".$row["id"].">";
echo "<input type=\"text\" name=\"WeightorMeasure\" value = ".$row["WeightorMeasure"].">";
echo "<input type=\"submit\" value=\" Submit \" name=\"Update\">";
}
echo "</form>";
} else {
echo "0 results";
}
$conn->close();
?>
I am trying to delete row from table.
Table name is username of user (which user logged in )
delete page is here
require('db.php');
if(isset($_POST["deletebtn"])){
$x = $_SESSION['username'];
$id=$_POST["statusid"];
$row=$conn->query("select id from $x where id='$id'");
if ($conn->query($row) === TRUE) {
$sql = "delete from $x where id ='$id'";
// echo 'success';
header( "Refresh:3; url=admin2.php", true, 303);
}else{
echo "not";
}
}
and here is my content code
$sql = "SELECT id,date,status FROM $x order by id DESC;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
echo "<div class='border'> ";
echo "";
echo "<form action='deletepage.php?action=$id'>";
echo "<input type='hidden' name='statusid' value='$id' >";
echo "<p> " . $row["status"] ."</p><br>";
echo "<input type='submit' value='delete' class='btn1 pull-right' name='deletebtn'> ";
echo "<a class=' btn1 pull-right' href='#'>edit </a>";
echo "<small> " . $row["date"]. "</small><br>";
echo "</form>";
echo "</div>";
}
} else {............}
You are not executing query on the sql
$sql = "delete from $x where id ='$id'";
Execute it like
$del= $conn->query($sql);
After the query
You forgot to execute query
require'db.php';
if(isset($_POST["deletebtn"])){
$x = $_SESSION['username'];
$id=$_POST["statusid"];
$row="select id from $x where id='$id'"; //<---remove query execution from here
if ($conn->query($row) === TRUE) {
$sql = "delete from $x where id ='$id'";
$conn->query($sql); //<-------- Add this line
// echo 'success';
header( "Refresh:3; url=admin2.php", true, 303);
}else{
echo "not";
}
}
There are several problems with your code.
there is no query execute for delete query.(which you may be solved later)
in your while loop you put $conn->query ($row) which is wrong. $row is not a query... It is the result set return from previous query.
and lastly $conn->query does not return true in SELECT, SHOW, DESCRIBE or EXPLAIN query. Check php manual
Change this
$sql = "delete from $x where id ='$id
To$sql = "delete from $table where id ='$sessionid'
Hi I am trying to do a Registration that the users will put their name password and their answers to some questions and then an admin will manually answer to it if it's accepted.I did the system that loads their name password and answers in the database,and I also ran the things that will show the answers to the admin,but I can't figure a way to change a value just for one user not for all of them,I will leave you my codes and everything over here.
Here is my admin.viewapplications.php code
(Here,it shows everything fine,but I can't figure a way that the button to act just for one id not for all)
<?php
//include(__DIR__ . "/signup.php");
include("../resources/config.php");
//$name = $_POST['Name'];
//$mg = $_POST['MG'];
//$pg = $_POST['PG'];
//$rk = $_POST['RK'];
$sql = "SELECT id, name, tutorial, MG, PG, RK FROM rp_users WHERE tutorial = 2";
//$tutorial = "SELECT tutorial FROM rp_users";
$result = mysql_query($sql);
//$result2 = mysql_query($tutorial);
//$value = mysql_fetch_object($result2)
/*if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}*/
//if($value > 1)
//
while($row = mysql_fetch_array($result))
{
//$tutorial = row["tutorial"];
//f($tutorial == 2)
//}
$id = $row["id"];
$name = $row["name"];
$mg = $row["MG"];
$pg = $row["PG"];
$rk = $row["RK"];
echo "ID: " . $id."<br> <br>";
echo "Nume: " . $name."<br> <br>";
echo "MG: " . $mg."<br> <br>";
echo "PG: " . $pg."<br> <br>";
echo "RK: " . $rk."<br> <br>";
echo '<form action="./?p=applicationaccept" method="POST">';
echo '<input type="submit" name="accept" value="Accepta">';
echo '</form><br>';
echo '<form action="./?p=applicationdeny" method="POST">';
echo '<input type="submit" name="deny" value="Respinge">';
echo '</form><br> <br> <br>';
}
//}
//
?>
And here is my applicationaccept.php
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $id;
$sql = "UPDATE rp_users SET tutorial=0";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
/*while($row = mysql_fetch_array($result))
{
}*/
?>
I think what you want to do is a simple UPDATE to your MySQL database..
but make sure you format the PHP code you're using otherwise it'll give you an ERROR!
Also you have to use 'mysqli' now in PHP!
<?php
$someID = '1';
$sql = "UPDATE `rp_users` SET `tutorial`= '0' WHERE `id` = $someID";
$result = mysqli_query($link, $sql);
if($result)
{
echo "Success";
}
else
{
echo ("Error");
}
?>
BTW I forgot to mntion the '$link' is the connection to your database!
As of my understanding of your question if your form action is applicationaccept.php and you are trying to update for one user in applicationaccept.php file, try this:
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $_POST["id"]; // pass id as parameter in form
$sql = "UPDATE rp_users SET tutorial=0";// change this line to following line
$sql = "UPDATE rp_users SET tutorial=0 where id=$iduser";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
?>
Be aware your code is vulnerable
I have used urldecode to receive a member ID from a previous site. The correct ID is being displayed in the URL but I can't fetch information from the database.
members.php:
<?php
$query = "SELECT name, memberID FROM members";
if(!$result = $db->query($query)){
die('There was an error running your query[' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
printf ('<li>' . $row['name'] . '</li>');
}
?>
profiles.php:
<?php
$id = isset($_GET['memberID']);
$query = "SELECT * FROM members WHERE memberID = '".$id."'";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()){
printf("%s (%s)\n", $row["memberID"], $row['name']);
}
}
var_dump($query);
?>
All I get is a blank screen.
I found couple of problems in the code:
members.php
while($row = $result->fetch_assoc()){
printf ('<li>' . $row['name'] . '</li>');
}
Here you are using printf function which have 1st argument for format of string.
Correct that with echo statement as below:
while($row = $result->fetch_assoc()){
echo '<li>' . $row['name'] . '</li>';
}
profiles.php
$id = isset($_GET['memberID']);
Here you are setting the $id with isset() function return value.
You should instead set the value from GET parameter as below:
if(isset($_GET['memberID'])) $id = $_GET['memberID'];
See now if it's working.
Make sure that you use the correct capitalization of memberId vs. memberID. This is very important.
Do not pass values retrieved from GET/POST through urldecode. They already are.
Please try the following based on your code and let us know the results:
<?php
$id = isset($_GET['memberID']) ? $_GET['memberID'] : 0;
if($id > 0){
$query = "SELECT * FROM members WHERE memberID = '".$id."'";
$result = $db->query($query);
if($result){
echo "Rows found: " + $result->num_rows;
} else {
echo "No rows found";
}
} else {
echo "memberID is 0";
}
?>
Is memberID an int field in the database or a string field? If it is an int field then remove the single quotes in your query on profiles.php.
I have a page where I list items ( in this case businesses) that are pulled from a mysql database with php.
<?
include('config.php');
echo "<h3>Saved Businesses</h3>";
echo "<ul style='list-style-type:none;'>";
$result = mysql_query("SELECT * FROM `saved_biz` WHERE user_id = '$id'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$business_id = $row['business_id'];
$result = mysql_query("SELECT * FROM `company` WHERE id = '$business_id'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
$business_name = $row['name'];
}
echo "<li>" . nl2br( $business_name);
echo "<a href=deletesavedbiz.php?id={$row['id']}>Delete</a></li>";
echo "</tr>";
}
echo "</ul>";
?>
If the user decides to delete one of the businesses ( by clicking the "Delete" link ) he/she is then taken to deletesavedbiz.php and then, if successful, presented with a link to get back to the profile.php page that he/she was just on.
<?
include('config.php');
$id = (int) $_GET['id'];
mysql_query("DELETE FROM `saved_biz` WHERE `id` = '$id' ") ;
echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> ";
?>
<a href='profile.php'>Back To Listing</a>
Now, what I want to do is have the php delete and then do like a php header redirect to profile.php without ever making the user click a link back. How can I accomplish this? Also, I'm fine with having the answer being javascript if it's not possible or not very clean in PHP.
Thanks for all help!
Do redirect in case your query returned TRUE.
function redirect($page = 'profile.php'){
header("Location: $page");
exit;
}
function your_query(){
include('config.php');
$id = (int) $_GET['id'];
//returns TRUE on success, FALSE otherwise
return mysql_query("DELETE FROM `saved_biz` WHERE `id` = '$id' ") ;
}
if ( your_query() ){
redirerct();
} else {
redirect('some_error_page.php');
}
header("Location: profile.php");
Put that at the bottom of your script that deletes the record.