I'm trying to setup a button that will delete all the rows we loaded on this run but it's not working.
PHP:
if(isset( $_GET['startID'], $_GET['endID']))
{
$sql_query = "DELETE FROM users WHERE id BETWEEN " .$_GET['startID'] . " AND " . $_GET['endID'] . ";";
mysql_query($sql_query);
header("Location: index.php");
}
Button:
<a onClick="javascript: return confirm('Are you sure?');" href='index.php?startID=<?php echo $firstID; ?>&endID=<?php echo $lastID; ?>'>Delete</a>
Button redirects to: index.php?startID=11111&endID=22222
The button is passing the values but they're not getting deleted from the database. Can anyone point me to the mistake?
For this kind of problem you need to start Debugging yourself. What you really need to do is-
Check the database connection is established or not.
Checkout the Condition is correct or not that is applied, You can use a simple echo for this.
echo the query string and run into the phpMyAdmin and see is there anything miss use or others.
These are the suggestion for all the OP who ask this type of
questions. We are always here to help you where you get stuck.
Try for instead:
if(isset( $_GET['startID'], $_GET['endID']))
{
for($i=$_GET['startID'];$i<=$_GET['endID'];$i++){
$sql_query="DELETE * FROM users WHERE id=" .$i. ";";
mysql_query($sql_query);
}
header("Location: index.php");
}
Related
I have a table with registered users. My code is suppose to delete a row when clicking delete in the table.
This is in the database.php
.....
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr><td align="left">' . $row['Id'] . '</td><td align="left">Delete</td></tr>';
}
......
So, I'm getting the id when clicking delete. So far, this part works but when I tried to run the delete query it doesn't work.
delete.php
<?php
session_start();
include 'connection.php';
if (isset($_POST['Id']) && is_numeric($_POST['Id'])){
$id = mysqli_real_escape_string($conn, $_POST['Id']);
$result = mysqli_query("DELETE FROM table_name WHERE Id= '$id' ")
or die(mysqli_error());
echo "<h3><br><br><a href=database.php> <b> Go Back</a></h3>";
echo "Data Deleted";
}else {
echo "Error";
echo "<h3><br><br><a href=database.php> <b> Go Back</a></h3>";
}
?>
I just get "Error" and it doesn't remove the row. How can I fix it?
Edit:
<?php
session_start();
include 'connection.php';
if (isset($_GET['Id']) && is_numeric($_GET['Id']))
{
$id = mysqli_real_escape_string($conn, $_GET['Id']);
$result = mysqli_query("DELETE FROM User_reg WHERE Id= '$id' ")
or die(mysqli_error());
echo "<h3><br><br><a href=AdminLog.php> <b> Go Back</a></h3>";
echo "Data Deleted";
}else {
echo "Error";
echo "<h3><br><br><a href=AdminLog.php> <b> Go Back</a></h3>";
}
?>
Still getting the same result with the delete query not working.
Also "Id" name is set in the same way as in the database.
$_POST['Id'] is not set, because you got to that script via a link.
Delete
links are GET requests, not POST requests. So, $_GET['id'] (note that it is $_GET['id'] rather than $_GET['Id'] because you used id in your link) should be set, but it's not really safe to use a link to delete things to begin with.
There are various ways to get around this issue. One way is to have the delete link in your table direct you to a intermediate confirmation page that posts to the actual delete script.
it would not work because you are sending a get parameters and checking for post and note the comment above for prepared statement and also try not to use get to delete data because a programmer can easily change the id and delete another user info use post instead because it cant be tweaked that is why social use let me callm it ajax to delete, because a deleted cannot be retrieved unless you create an alternative so use POST METHOD instead change this
if (isset($_POST['id']) && is_numeric($_POST['id'])){
$id = mysqli_real_escape_string($conn, $_POST['id']);
to this
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = mysqli_real_escape_string($conn, $_GET['id']);
This should work
I have three pages in the website. The first is login page,second is profile page and third is main page.
<?php
session_start();
$servername="blah blah blah";
$connectioninfo=array('Database'=>'mbr');
$conn=sqlsrv_connect($servername,$connectioninfo);
if($conn)
{
echo 'connection established';
}
else
{
echo 'connection failure';
die(print_r(sqlsrv_errors(),TRUE));
}
$q1="SELECT * FROM EmployeeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt=sqlsrv_query($conn,$q1);
if($stmt==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
$row=sqlsrv_fetch_array($stmt);
echo $row['EmployeeName'];
$q2="SELECT * FROM pointsBadgeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt1=sqlsrv_query($conn,$q2);
if($stmt1==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
$pbrow=sqlsrv_fetch_array($stmt1);
?>
The above is the php used in the second page of the website. Here I am using two queries $q1 and $q2 to retrieve information from two different tables (EmployeeTable and pointsBadgeTable) after connection to the database "mbr" here.
I then echo the desired Info in my html after retrieving info from the tables.
For instance,
<?php echo "". $row['goldTotal'] .""?>>
Here 'goldtotal' is a column in the pointsBadgeTable in the above php. Also please note that I am using " . $_SESSION['id'] ." here to show info only about the person who logs in the first page of the website.
The issue here is that I want to echo the same value in the third page as in second page. Will I have to write the same php code in third page as I wrote in second page or I need to store it in some session variable. How to use a session variable here?
Also, is it correct to rewrite the same code in third page also as in second page and use the same queries $q1 and $q2? I will copy and paste the same PHP to the third page also.
You Can include the second page in third page , you will get the value .
Example : file3.php
**<?php
include 'file2.php';
?>**
The code below works, in that, it updates the database correctly. However, after the post is completed the data on the page is old i.e. $reader_busy[0] is not updated even though it is driven by a SELECT query.
To force $reader_busy[0] to update properly on the users screen a refresh is required. This means the pages reloads after the post and then refreshes.
How do I get the page to display the correct data from the mysql without having to load the page twice?
Simplified version of my PHP code:
<?php
$reader_key = 'jm2';
$reader_busy_query = "SELECT `BusyWithClient` " .
"FROM {$wpdb->prefix}assistant " .
"WHERE `Key`='" . $reader_key . "'";
$reader_busy = $wpdb->get_col( $wpdb->prepare( $reader_busy_query ) );
$busy_with_client = $reader_busy[0];
if(isset($_POST['toggle'])){
$result = $wpdb->update(
$wpdb->prefix . 'assistant',
array('BusyWithClient' => $busy_with_client),
array('Key' => $reader_key)
);
// I want to remove / replace this line, and still see updated data.
echo "<meta http-equiv='refresh' content='0; url=" . curPageURL() . "#instructions" . "'>";
}
?>
<h1>
<?php print_r('busy: ' . $reader_busy[0]); ?>
<h1>
<form method="post" action="<?php echo curPageURL() . '#instructions'; ?>">
<input type="submit" name="toggle" value="toggle" >
</form>
Just flip the order of how you do things. Instead retrieving then updating, update before you retrieve.
<?php
if (isset($_POST['toggle'])) {
$result = $wpdb->update(
$wpdb->prefix . 'assistant',
array('BusyWithClient' => $busy_with_client),
array('Key' => $reader_key)
);
}
$reader_busy_query = "SELECT `BusyWithClient` " .
"FROM {$wpdb->prefix}assistant " .
"WHERE `Key`='" . $reader_key . "'";
$reader_busy = $wpdb->get_col($wpdb->prepare($reader_busy_query));
?>
I see what you are trying to accomplish and that's looks impossible. Instead of refreshing page you can (should) make ajax request and update your page by refreshing only the page data that you need.
EDIT:
Because, you are trying to post to a page with ANCHOR, and browsers by there nature will try to find information on the page without refreshing it. Browser still sends information to post, so it updates in background, but not refreshes the page.
You should make post over ajax request and return data to field, or post it to another php file (without #hashtag).
PS - You will not get more answerers if you will downgrade every answers that didn't work for you :)
I'm working on my project for my school, I made activating a user account work, if the admin want to activate one user at a time,
but whenever the admin wants to update multiple record at once, it won't work
I tried to include the update SQL inside a foreach loop, but it gave me an error: invalid argument for foreach()
pleas guys help me,
if(isset($_POST['activate'] )){
$impid = implode(',', $_POST['SSU']);
// for each .. to update more than one at once.
foreach($impid AS $i){
$sql = "UPDATE Accounts SET Activated = '".$_POST['activate']."' WHERE SSU IN('" . $i . "') ";
$result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
}
// to test. if anything got effected..
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<script type='text/javascript'> alert('Successfully Updated ')</script>";
}
else {
echo "<script type='text/javascript'> alert('Failed to Update ')</script>";
} // end the test.
}else{echo "<script type='text/javascript'> alert(' Select Whether to Activare Or Deactive ')</script>";}
}
} // end of first if $_post[update]
thank u guys, I fixed it, i just passed $_POST['SSU'] to my foreach and it worked.
I'm trying to create a redirection but for some reason it is not working and for the life of me I can't figure out why. The if command works well but not the else.
Any suggestions?
$sql="SELECT * FROM $tbl_name WHERE uid='";
$sql=$sql . $xoopsUser->uid("s") . "' ";
// Get a specific result from the "example" table
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
print "<div class='blockTitle'><strong>DOWNLOADS</strong></div><p><br><p>";
echo "<div align='center'>You currently have no projects pending payment.<p>If you just completed a transaction but missed the link on the PayPal page, click <a href='http://site.co.uk/site/modules/cjaycontent/index.php?id=6'>here</a>.</div>";
}
else{
header('Location: http://site.co.uk/site/myComposerList2.php');
}
You need to use exit, header wont stop the script from executing instantly.
Like this:
header('Location: http://site.co.uk/site/myComposerList2.php');
exit;