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.
Related
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
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.
I have the following code to load users in a table: register.php here I want to delete users from. Only the second script deleteUsers.php doesn't work.
Can any body help me out?
register.php`
<?php
$edit = 'edit:';
$account = 'Username:';
$account = '<font size="4">'.$account.'</font>';
$password1 = 'Password:';
$password1 = '<font size="4">'.$password1.'</font>';
$id = 'id:';
$id = '<font size="4">'.$id.'</font>';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table border=4><tr><td>$account</td><td>$password1</td><td>$id</td><td>edit</td><td>delete</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php'> delete</a> </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>
You see there is an delete button who redirect to deleteUser.php
This is the page you find here below, unfortunately the delete function doesn't work:
<?php
require_once("/db_conn.php");
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
(isset($_GET['id'])
// delete the entry
$result = mysql_query("DELETE FROM login WHERE id =$id")
or die(mysql_error());
// redirect back to the view page
header("Location: register.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
echo "doesn'twork";
}
?>
You need to pass the id variable :
while($row = $result->fetch_assoc())
{
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php?id=" . $row['id'] . "'> delete</a> </td></tr>";
}
you need to pass the id
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td>".$row['id']."</td><td><a href='/editUser.php'> edit </a> </td><td> <a href='/deleteUser.php?id=". $row['id']."'> delete</a> </td></tr>";
}
on deleteuser.php
$id=$_GET['id'];
$result = mysql_query("DELETE FROM login WHERE id =$id")
or die(mysql_error());
$id=$_GET['id']; must put after query
How do I insert to the database when you click a button?
I also need to insert in the same command, the row [id] and SESSION [id]
I use an html page that calls the php, then the variaves SESSION are not in my php page. I'm stuck here .. please help
<?php
session_start();
if(!isset($_SESSION["email"]) || !isset($_SESSION["senha"])) {
header("Location: login.php");
exit;
}
?>
<?php
$deets = $_POST['deets'];
$deets = preg_replace('#[^0-9/]#i', '', $deets);
include ("connect.php");
$events = '';
$query = mysql_query('SELECT hora, status FROM horario');
$num_rows = mysql_num_rows($query);
if($num_rows > 0) {
$events .= '<div id="eventsControl"><button class="btn2" style=" float:right;" onMouseDown="overlay()"><b>Fechar</b></button><p><b> ' . $deets . '</b></p></div> <br />';
while($row = mysql_fetch_array($query)) {
$desc = $row['hora'];
$desc1 = "<input type='submit' class='btn1' name='insert' value='Marcar Hora' />";
$events .= '<div id="eventsBody">' . $desc . ' | '.$desc1. '<br /><hr><br /></div>';
}
}
echo $events;
if(isset($_REQUEST['insert']))
{
$SQL = "INSERT INTO eventos (id, data, idhora,) VALUES ('', '.$deets.', '$row[id]', 'session[id]')";
$result = mysql_query($SQL);
}
?>
2 Problems I initially see, although I don't have full context of your code.
First, I don't see you starting the session anywhere, this requires you to run session_start(); before you try to grab the session ID or save any variables into the session.
Second, as far as I understand it you'll need to reference the session id by doing something like this.
$id = session_id();
I need script to delete registered users from database. It is working but not how it should.
My script delete last entry instead of entry I clicked on?
Here is my script for listing entries.
<form action="user_delete.php" method="post">
<?php
error_reporting(0);
$sql="SELECT * FROM users WHERE status='member' ORDER BY id_user";
$result = mysql_query($sql) or die(mysql_error());
while ($record = mysql_fetch_array($result))
{
$id_c=$record['id_user'];
$mail=$record['mail'];
$_SESSION["del_user"]=$id_c;
$user = mysql_result(mysql_query("SELECT username FROM users WHERE id_user='$id_c'"), 0);
echo "Name: <b>$user</b><br />";
echo "E-mail korisnika: <b>$mail</b><br />";
echo "ID: <b>$id_c</b><br /><br />";
echo "<input type=\"submit\" name=\"delete_row\" class=\"btn\" value=\"Delete\" />";
echo "<hr />";
}
?>
</form>
And this is my user_delete.php
<?php
session_start();
include 'function.php';
include 'db_config.php';
$del=$_SESSION["del_user"];
$sql = "DELETE FROM users WHERE id_user = $del";
mysql_query($sql) or die(mysql_error());
if(mysql_affected_rows()>0)
{
header("Location:user_management.php");
}
else
{
echo "No";
}
mysql_close();
?>
In your while loop, you are overwriting the session variable del_user:
while ($record = mysql_fetch_array($result))
{
$id_c=$record['id_user'];
$mail=$record['mail'];
$_SESSION["del_user"]=$id_c;
This means that $_SESSION["del_user"] will always be the last record.
Pass your POST variable for email OR primary key of that table. Right now you are pchecking with session id. It will delete only your record every time.
$del = $_POST['email'];
$sql = "DELETE FROM users WHERE id_user = $del";