DELETE FROM statement not working - php

I have a table in a database called 'threads' and I want to delete a row where the id is equal to $forumid which is set by the URL. The URL looks something like this: domain.com/viewThread.php?forumid=1
I am getting this using
$forumid = $_GET['forumid'];
and I am sure that this is working because I use
echo $forumid;
and it works correctly. But when I go to delete a row using
$db->query("DELETE FROM threads WHERE id='$forumid'");
its not working for some reason.
Can somebody please help me with this? Is it possible that there is something wrong with my phpMyAdmin or mySQL database?

Try the below query
$db->query("DELETE FROM threads WHERE id='.$forumid.'");

Check the query
("DELETE FROM threads WHERE id= '.$forumid.'");

Try by using any of the below codes.
$db->query("DELETE FROM threads WHERE id='.$forumid.'");
OR
$db->query("DELETE FROM threads WHERE id=$forumid");

Related

How to make update query when user status changed to 1

i got issue while if statement running. i want to update query on those user_id's who status changed from 0 to 1. but this query run on all status userid who already 1. but i want run on those who changed to 0 to 1. and run only one time don't many time. please help me to solve this. Thanks
if($status==1)
{
$query22= "UPDATE `user` SET `global` =$row[user_id] WHERE
`user_id` = '".$_SESSION['userid']."' ";
$current_id = $db_handle->insertquery($query22);
}
Try with below condition:
if($status=1)
If I understand correctly you want to take some action when a status is changed by user, Ignore answer if thats not the case.
First you need to check the initial status. This you most probably need to fetch from database.
Lets call this variable $initialStatus
$initialStatus = fetchUserStatus($userId); // need to implement fetchUserStatus
then you can put your check something like this,
if($status==1 && $initialStatus==0){
// do some action
}

Using SQL on wordpress page

Using a plugin I'm able to use PHP on page by using [insert_php] as a tag however, whenever I try using SQL it doesn't seem to work.
I tried using:
global $wpdb;
$prepared = $wpdb->get_row(
"SELECT SiteID, SiteName
FROM $wpdb->Site
WHERE SiteID = 1");
echo $prepared->SiteName;
echo "test";
All I'm getting is test on the page and I've tested to see if my sql statement was at fault and it seems to be working fine so I'm guessing there's an issue with $wpdb or the way I'm outputting the data.
WordPress.org has a lot of detailed information in their reference.
I think attempting to refer to $wpdb->Site is a likely suspect for why your code is not working. You will need to know the exact fields in the table to pull your information.
Here is a reference for the wp_site table. I think you're actually looking for the 'domain' field, not 'sitename'.
Try replacing $wpdb->Site with the actual name of the table. I also get errors like that at first since $wpdb->table_name only works with the default wp tables.
EDIT
It should be something like this:
SELECT SiteID, SiteName FROM Site WHERE SiteID = 1

how can i update a table column with another table column based on another based off 2 other columns

Hey guys how's it going?
Ok so i'm having trouble writing a small script and i can't seem to understand what its not working my reasoning is:
update all the tech_kunena_messages.subject with tech_kunena_topics.subjects where tech_kunena_messages.thread and tech_kunena_topics.id are the same...
the script i've come up with is this:
$con = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_selectdb($db,$con) or die(mysql_error());
mysql_query("UPDATE tech_kunena_messages
SET tech_kunena_messages.subject=tech_kunena_topics.subject
FROM tech_kunena_messages
JOIN tech_kunena_topics ON tech_kunena_messages.thread = tech_kunena_topics.id");
echo "Done Pease check the data base";
mysql_close($con);
?>
But for some reason tech_kunena_messages.subject is not getting updated with any thing, this is like my third time writing a script and i don't have the reflex to see what is wrong for me it should work ( i also tried with INNER JOIN but nothing happend either oh the code is beeing read as when i navigate to the scripte i get the please check the database..
any ideas?
Thanks guys
Alex
Your UPDATE query should look like below
UPDATE tech_kunena_messages tkm
JOIN tech_kunena_topics tkt
ON tkm.thread = tkt.id
SET tkm.subject= tkt.subject

How to get inserted data from a query to database as success message?

i'm working for a project and this problem came across me as i'm not a master in php and mysql.
here is the sample.
table
id(AI)-fname-lname
<php
include 'db.php';
$query=mysql_query("INSERT INTO table(`fname`, `lname`) VALUES('some', 'one')");
if($query==1)
{
//some code
}
this is working fine, i can know whether the Query is Successful or not. what i want to know is, how to get the data of the row i just inserted, so that i can use the 'id' in another related table...
hoping for positive responce..
Use this function
mysql_insert_id()
This will return you the last inserted id with specific database connection. Also you don't want to get the data inserted other than id, because you already have it while inserting row.
you can get id of recent inserted record using mysql_insert_id()
by using that id you can get records using select query
<?php
include 'db.php';
$query=mysql_query("INSERT INTO table(`fname`, `lname`) VALUES('some', 'one')");
$id=mysql_insert_id();
$select_query=mysql_query("SELECT * FROM table where id=".$id."");
?>
You can use this function
$id = mysql_insert_id();
This will give the id to which the data is inserted, you can use that id for further use.
This will give you the key ID of the row
$id = mysql_insert_id();

How to delete and update data in MySQL at the same time

Basically what I am trying to do is when I delete a client with an ID of lets say 6 and I have 50 clients, I then want to update the client with the ID of 50 to 6.
This is my code, in PHP, but it won't execute 2 mysql_query-s at the same time at least I think that's the problem. Otherwise the SQL syntax works fine.
public function delete () {
$last=$this->numrow; //contains last ID works fine
if (isset ($_GET['x'])) {
mysql_query('DELETE FROM proba WHERE ID ='.$_GET['x']);
mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].'WHERE ID='.(int)$last);
}
}
The $_GET['x'] contains the ID on which it was clicked . But only the first mysql_query gets executed how do i make it so the second one gets executed also ?
And another question is is it possible to get <a href="munka/index.php?x=5" > [-] </a> the x=5 with a $_POST ?
You might save yourself a lot of trouble by using mysql's replace query:
see http://dev.mysql.com/doc/refman/5.6/en/replace.html
for details.
most probably you are facing a php error on the first query. Check the php error log.
for the second question $_GET is used to take parameters from the URL for example
munka/index.php?x=5
$_POST is used to get parameters posted on http post (usually on form submits).
just change the update query with a space before the where clause
mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].' WHERE ID='.(int)$last);
Better to use transactions support by using InnoDB Mysql DB Engine, so both delete and update execute together wuth COMMIT without miss , and in case anything goes wrong your delete changes get ROLLBACK
if (isset($_GET['x'])) {
mysql_query('DELETE FROM proba WHERE ID =' . $_GET['x']);
mysql_query('ALTER TABLE `proba` DROP `ID`;');
mysql_query('ALTER TABLE `proba` ADD `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');
}
Try in Phpmyadmin delete record no of 5 and drop id column and recreate id column.
It is work.

Categories