Reload Page After Delete in PHP - php

I have made this function to delete the record from the table. When the delete operation is done, the page displays all data(including the deleted one), But, I need to refresh or reload the page to see the results after I have deleted the row. How it can be done in following code? Thanx in advance!!
public function deletedata(){
if(isset($_GET['del_id'])){
$delete_id = $_GET['del_id'];
$query ="DELETE FROM tbl_data WHERE project_id ='".$delete_id."' ";
$this->databaseObject->getConnection()->query($query);
}

Usually you'd use the header function to reload a page.
Something like this should work: header('Location: '.$_SERVER['REQUEST_URI']);
http://php.net/manual/en/function.header.php
Alternatively, depending on the structure of your code you could simply call the delete code before the code that retrieves the records. That way you'd avoid the need to reload the page.

As mentioned in laurencek's alternative, there's probably no need to reload the page.
Just perform any necessary record deletion before rendering the HTML.
<?php
$delete_id=isset($_GET['del_id'])&&is_numeric($_GET['del_id'])?$_GET['del_id']:false;
if ($delete_id) {
$query ="DELETE FROM tbl_data WHERE project_id ='".$delete_id."' ";
$this->databaseObject->getConnection()->query($query);
}
$query ="SELECT * FROM tbl_data WHERE 1;";
$this->databaseObject->getConnection()->query($query);
?>
<html>
// some php/html to loop through the query and display records
</html>
This assumes your IDs are numeric.
If not, I strongly suggest checking/escaping the variable before introducing it to your database.

Related

PHP query output page

The action on form page venue_events calls event_list and gives me the query output requested. If you type the link to the event_list page, it will populate a new query with out values from the form. Is there a way I can prevent the user from doing this or can I redirect them back to the venue_events page if this has been done?
In your landing page you have to check if you have a correct query, or correct data to build up your query. If not, for example because the user typed in the url directly, you can redirect the user to the page you want (probably the form one) using, if you have echoed nothing before, header('Location: http://www.mysite.com/my_form_page.php');.
If I understand you right you should validate the values before querying your database. If there are no valid values you can initiate a redirect using javascript or meta redirection in html or using:
header('Location: http://www.domain.com/site.php');
That is only working of there is no output before the header() command.
In addition you could also check if you got back some results from the database. If thats not the case you can also initiate a redirect.
My suggestion to your comment:
<?php
// Was a search term passed?
if (isset($_POST['search']) && !empty($_POST['search'])){
$sql=" SELECT * FROM venue_event ";
$search_term = mysql_real_escape_string($_POST['AAC']);
$sql .= "WHERE venue = '{$search_term}' ";
$sql .= "AND state = '{$search_term2}'"; // $search_term2 isn't defined in your code at all...
$sql .= "ORDER BY startdate ASC";
$result=mysql_query($sql) or die(mysql_error());
}else{
// Do redirection here
}
?>

Update table value on page view?

I am trying to create a PHP trigger for when a user views certain pages on my website it will update the user table in the points section.
I understand the process would work something like this
on page view > update user > where user id is (**get username from session**) > add 5 to points row
Anyone have any idea how to set up something simple like this for giving users simple points for viewing pages?
My site is using PHP and mySQL for the database.
Use cookies or session variables to keep track of the user details like the username or ID. So making a pageview trigger would be as easy as adding a mysql query at the top of every page which would update the database table for views. Kinda the same way that forums operate.
E.g
<?php
session_start();
$db_connection = mysqli_connect('host','username','password','db');
$user_id = $_SESSION['userid']; //That is asssuming that you had gotten the user id on login
mysqli_query($db_connection, 'UPDATE page_views SET views_column=views_column+1 WHERE userid=$user_id');
?>
Yes, you could do something like (if you own the page the user has to visit):
<?php
$pointsForThisSite = 5;
include "points_adder.php";
?>
While Points_adder looks whether $pointsForThisSite is defined and > 0, then adds the Points to the database as you descripbed.
Is that what you are looking for?
Create a php function and call it everytime the user enter the page.
You don't need a mysql trigger because, the action is at the webpage.
function add_points($user, $page){
//If users visits too many maybe you don't want to gave him some points.
//add points
}
and invoke the function in that pages you want to score
The most unobtrusive way to do this is with an AJAX call after the page has loaded. The call should be to an include file that performs the database update operation and returns a 204 response so that the visitor's browser doesn't wait for response content.
For an Apache server;
header('HTTP/1.0 204 No Content');
header('Content-Length: 0', true);
header('Content-Type: text/html', true);
flush();
// do the table update here

php querying for data just inserted...by itself

I don't know how to title this...but anyway,
In my script if data doesn't exist, i insert a new row into a database and then check again for that row, for example:
1 search the db
2 if nothing
include(create.php) -> create entry
3 search the db for that row
Am I going to have to put in a usleep(1000000); between the include and the next search on the db? or is there something I am missing?
THanks!
Include the file create.php on top of your php script and call the functions you required inside this block
if ($number_of_rows < 1) { //call the functions from create.php you need here}
Seriously, why even have a create.php used in that manner anyway? Include create.php at the top of the page, put all the insert syntax into a function, and call it later on in your main script. That would work.
Or even better, don't even bother including it. Just run the queries straight in your main page. That way if you need to change something, you won't have to affect other pages.
You can use something like that
$query="Select * from table where id='23'";
$result=mysql_query($query);
if(mysql_num_rows($result)>0){
//result find in sql table
}else{
$query1="INSERT INTO table (schema) values(values)";
$result=mysql_query($query1);
}
sleep(1);
$query="select *...."
You can also use mysql INTERVAL query.It will automatic make a query after a particular interval and search for data.

PHP isnull First pass/Second pass flag

I have a PHP results page which starts off "first-pass" with ALL rows returned. It's a search listing of all pizza places in the county.
SELECT * from pizzeria;
Then the user can drill down into more detail... the page also has a CSS dropdown menu where the user can pick a specific neighborhood (which carries a URL):
href="samepage.php?neighborhood=HELLSKITCHEN"
which then changes the query after I pick up the $_GET[]
SELECT * from pizzaria WHERE nbh=(the $_GET[] variable sent in the URL);
but I'd like the page to call itself and I have header("Cache-Control:no-cache"); at the top.
I'm trying to create a first-pass or first visit flag variable with the isnull() function:
if (is_null($firstpass)) {
$query = SELECT all the records from the pizzaria table
} else {
$query = SELECT only the records WHERE I $_GET[] the value from the reloaded URL
}
It seems though that the $firstpass variable doesn't stick on reloads. Should I SESSION that variable? (though still have the problem of constantly resetting it)
Or maybe implement some other approach?
I know I can redirect to a separate second page and javascript back to this page to avoid "headers already sent", but I want to avoid the round-trip back to the client.
Is there a known best practice on reloads with new info? Kinda new to PHP here. thanks
Maybe I didn't understand well your problem but why wouldn't you do :
if (!isset($_GET['example'])) {
$query = 'SELECT * FROM pizzerias';
} else {
$query = 'SELECT * FROM pizzerias WHERE pizzeria = \'.mysql_real_escape_string($_GET['example']).\' LIMIT 1';
}
at the first pass because, it seem that the $_GET variable is set only when the user choose a pizzeria?
Here is a more targeted answer.
NOTICE: mysql_* functions are being depreciated, so use PDO instead. In my example I'm being semi-lazy and not using PDO.
//Connect to database and define table up here
...
if(!isset($_GET['neighborhood')){
$q = "SELECT * FROM pizzeria;";
}else{
$q = sprintf("SELECT * FROM pizzeria WHERE nbh=%s",mysql_real_escape_string($_GET['neighborhood']));
}
$query = mysql_query($q);
foreach($row = mysql_fetch_array($query,MYSQL_ASSOC){
//display the updated view of restaurants.
}
I would also suggest that you use jQuery for that Web 2.0 effect. It's really nice when you select from a drop-down menu and things magically move without a page reload.

MySQL query needs an additional page refresh to show UPDATE changes

Why does this not show the changes after submit? The page has to be refreshed AFTER submission to see the changes.
$full_path = 'users/'.$_SESSION['user_id'].'/images/'.$name;
if($query = mysql_query("UPDATE user_info
SET user_image = '$full_path'
WHERE user_id = '".$_SESSION['user_id']."' AND
username = '".$_GET['username']."'
"))
{
if(move_uploaded_file($tmp_name, '/Applications/XAMPP/xamppfiles/htdocs/'.$full_path)) {
echo 'Got it!';
}
}
So, if I upload / click submit, the query is successful, but you can't see the changes until an additional page refresh.
Make sure that your update query is before your select for your data in the execution of the PHP page.
You need to fix your SQL, you are just leaving yourself open for SQL injection, with using $_GET['username'] directly in your SQL query.
Please look at utilizing parameterization, also keep in mind that order counts when you develop these things. TOP -> DOWN.
If you have a display SQL call BEFORE your UPDATE call, then you will have to refresh again to see changes from the UPDATE SQL call.
You should do a GET redirect after POST request anyway.

Categories