473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to put MYSQL_QUERY with variables in it, in a if statement.

I have been trying for hours to get a php script to delete a record from a database. You first select the database and type in a ID and that gets send as a html form to the php code. In this php code I have added:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST') {}
  3.  
The delete line should be placed very simply in this if statement like this:
Expand|Select|Wrap|Line Numbers
  1. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  2.  
  3. mysql_query ("DELETE FROM `$_POST["DB"]`
  4. WHERE reactieID = `$_POST["ID"]`") or die (mysql_error());
  5. }
  6.  
But it doesn't work. First I get the error that there are quotes which aren't supposed to be there. So I change the code to this:
Expand|Select|Wrap|Line Numbers
  1. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  2.  
  3. mysql_query ('DELETE FROM `$_POST["DB"]`
  4. WHERE reactieID = `$_POST["ID"]`') or die (mysql_error());
  5.  
And now I can't get the $_POST to work. I have tried without quotes around them, with the '' quotes. It just uses the $_POST as the tablename, not what is written in the form. Hopefully someone can help me with this, why does this work outside a if statement but not inside one..
Nov 8 '12 #1
3 2627
Well, I have been able to delete stuff by giving a ID.
Expand|Select|Wrap|Line Numbers
  1. if(isset ($_SESSION['accountnaam'])){
  2.  
  3. Echo "U wilt een rij verwijderen";
  4.  
  5. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  6. $tabel = $_POST['DB'];
  7. $id = $_POST['ID'];
  8. mysql_query ("DELETE FROM gastenboek  
  9. WHERE reactieID = '$id'") or die (mysql_error());
  10.  
The ID is a number, but I still can't get $tabel to work, which is text, so I use it directly instead. But I have a whole list of names to choose from. I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''gastenboek' WHERE reactieID = '4'' at line 1

I assume that the double quotes, "", aren't being put around the text. Is that right?
Nov 8 '12 #2
Rabbit
12,516 Expert Mod 8TB
This line:
Expand|Select|Wrap|Line Numbers
  1. "DELETE FROM `$_POST["DB"]` WHERE reactieID = `$_POST["ID"]`"
is wrong because you haven't correctly escaped the quotes within the string.

But that point is moot because escaping the quotes won't work anyways because that's not how you reference arrays within a string if you want it to expand the variable.

This is what you need:
Expand|Select|Wrap|Line Numbers
  1. "DELETE FROM `$_POST[DB]` WHERE reactieID = '$_POST[ID]'"
You were also using the wrong quotes for ID.
Nov 8 '12 #3
You can try this:

Expand|Select|Wrap|Line Numbers
  1.     if(isset ($_SESSION['accountnaam'])){
  2.  
  3.     Echo "U wilt een rij verwijderen";
  4.  
  5.     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  6.     $tabel = $_POST['DB'];
  7.     $id = $_POST['ID'];
  8.     $query_delete="DELETE FROM gastenboek WHERE reactieID = '$id'";
  9.     mysql_query($query_delete) or die (mysql_error());
  10.     }
  11.     }
  12.  
  13.  
Nov 9 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: KathyB | last post by:
Hi, figured out where I was going wrong in my post just prior, but is there ANY way I can assign several variables to then use them in an Update statement, for example (this does not work): ...
1
by: Daniel Roy | last post by:
Hi gurus, I just started to look at a very slow-running SQL statement generated by an application (Siebel). I spooled the SQL from the application, replaced the bind variables by their values, and...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
8
by: Howard Kaikow | last post by:
I got bored today, so I decided to rewrite the code in KB article 316383 to decrease the number of object references. This resulted in a number of nested With ... End With. The original code had...
35
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
2
by: Dave | last post by:
ilee@bigpond.net.au (Ivan) wrote in message news:<90137e1b.0403301523.55aed707@posting.google.com>... Ivan - I little more specific to the java syntax. Right now you are probably using an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.