Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2012, 02:56 PM   PM User | #1
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
PHP/SQL delete based on form criteria:

Hi all..
I have a table that i want users to be able to delete based on 2 values..
"name tag" and "cpm"
tag = varchar(25)
cpm = float

I have this form on the main page:

Code:
<form method=post action="purgestrikesearch.php">
<label>Delete Records:  Tag Name:</label>
<input size=10 type=text id=tag name=tag>
<label> Where CPM is greater than:</label>
<input type=text size=2 id=cpmval name=cpmval>
<input type=submit value="Delete">
I can't seem to wrap my head around what to do on my purgestrikesearch.php page..

obiously i'm conncting, but building the query is puzzling me..

any help would be appreciated..

thanks,
-steven
stevenryals is offline   Reply With Quote
Old 11-12-2012, 03:41 PM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenryals View Post
Hi all..
I have a table that i want users to be able to delete based on 2 values..
"name tag" and "cpm"
tag = varchar(25)
cpm = float

I have this form on the main page:

Code:
<form method=post action="purgestrikesearch.php">
<label>Delete Records:  Tag Name:</label>
<input size=10 type=text id=tag name=tag>
<label> Where CPM is greater than:</label>
<input type=text size=2 id=cpmval name=cpmval>
<input type=submit value="Delete">
I can't seem to wrap my head around what to do on my purgestrikesearch.php page..

obiously i'm conncting, but building the query is puzzling me..

any help would be appreciated..

thanks,
-steven
It's simple really.

Code:
DELETE FROM `table` WHERE `tag`=$verifiedInput1 AND `cpm`=$verifiedInput2
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 03:49 PM   PM User | #3
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
Sorry i know this is noob stuff.. but i'm in the learning phase here

I have:

Code:
$tagval = $_POST['tagval']; 
$cpmval = $_POST['cpmval']; 

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

$sql = mysql_query("DELETE FROM {$table} WHERE tag = $tagval AND cpm > $cpmval" );
if (!$sql) {
    die(mysql_error());
}
it's erroring.. says: "unknown collumn 'steven' in where clause.
the tagval i'm sending is "steven-slist" so it's obviously coming from there somehow..

I thought this was because $tag was being sent, $tag was being retried by POST and then tag was the name of the collumn.. so i changed that variable to $tagval and it's still the same..

Last edited by stevenryals; 11-12-2012 at 03:55 PM.. Reason: because my first response was dumb lol
stevenryals is offline   Reply With Quote
Old 11-12-2012, 03:59 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenryals View Post
Sorry i know this is noob stuff.. but i'm in the learning phase here

I have:

Code:
$tagval = $_POST['tagval']; 
$cpmval = $_POST['cpmval']; 

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

$tagval = mysql_real_escape_string($tagval);
$cpmval = mysql_real_escape_string($cpmval);

$sql = mysql_query("DELETE FROM {$table} WHERE tag = $tagval AND cpm > $cpmval" );
if (!$sql) {
    die(mysql_error());
}
it's erroring.. says: "unknown collumn 'steven' in where clause.
the tagval i'm sending is "steven-slist" so it's obviously coming from there somehow..

I thought this was because $tag was being sent, $tag was being retried by POST and then tag was the name of the collumn.. so i changed that variable to $tagval and it's still the same..
Notice my amendments in bold. Try that and see how it goes.

It's important, for security, to use that function every single time you put user input into an SQL query.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 04:03 PM   PM User | #5
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
One step closer!

now i get this error:

Code:
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 'AND cpm  >' at line 1
none of that looks bad to me.. unless for some reason the 2nd variable isnt being populated..

in that case, i switched cpm > $cpmval and the tag = $tagval
the error is the same..

hmmmm.....

Last edited by stevenryals; 11-12-2012 at 04:37 PM..
stevenryals is offline   Reply With Quote
Old 11-12-2012, 04:40 PM   PM User | #6
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Can't see the error on my phone, but make sure the form input names match up to the POST array keys
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 04:48 PM   PM User | #7
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
Quote:
Originally Posted by BluePanther View Post
Can't see the error on my phone, but make sure the form input names match up to the POST array keys

They do.. here's the error in text:

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 'AND cpm >' at line 1

in the input form:

<input size=10 type=text id=tagval name=tagval>
<input type=text size=2 id=cpmval name=cpmval>

in the action file:

$tagval = mysql_real_escape_string($tagval);
$cpmval = mysql_real_escape_string($cpmval);

DELETE FROM {$table} WHERE tag = $tagval AND cpm > $cpmval

Code:
$tagval = mysql_real_escape_string($tagval);
$cpmval = mysql_real_escape_string($cpmval); 

$sql = mysql_query("DELETE FROM {$table} WHERE tag = $tagval AND cpm > $cpmval " );
if (!$sql) {
    die(mysql_error());
}
stevenryals is offline   Reply With Quote
Old 11-12-2012, 04:55 PM   PM User | #8
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenryals View Post
They do.. here's the error in text:

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 'AND cpm >' at line 1

in the input form:

<input size=10 type=text id=tagval name=tagval>
<input type=text size=2 id=cpmval name=cpmval>

in the action file:

$tagval = mysql_real_escape_string($tagval);
$cpmval = mysql_real_escape_string($cpmval);

DELETE FROM {$table} WHERE tag = $tagval AND cpm > $cpmval

Code:
$tagval = mysql_real_escape_string($tagval);
$cpmval = mysql_real_escape_string($cpmval); 

$sql = mysql_query("DELETE FROM {$table} WHERE tag = $tagval AND cpm > $cpmval " );
if (!$sql) {
    die(mysql_error());
}
Wait until I can get on a computer.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 05:29 PM   PM User | #9
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
Quote:
Originally Posted by BluePanther View Post
Wait until I can get on a computer.
no worries.. thanks a ton!
stevenryals is offline   Reply With Quote
Old 11-12-2012, 06:18 PM   PM User | #10
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
ok..

i was in a raw command prompt and tried the same command with REAL params.. such as 'WHERE tag = testing AND cpm > 3'

i'm missing the single quotation around the value of $tval..

i think that's the problem.. but how do i do that with a variable?
stevenryals is offline   Reply With Quote
Old 11-12-2012, 06:39 PM   PM User | #11
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenryals View Post
ok..

i was in a raw command prompt and tried the same command with REAL params.. such as 'WHERE tag = testing AND cpm > 3'

i'm missing the single quotation around the value of $tval..

i think that's the problem.. but how do i do that with a variable?
If you notice my first reply, that contained the correct format (although I didn't know tag was going to be a string).

PHP Code:
$query "DELETE FROM `table` WHERE `tag`='$verifiedInput1' AND `cpm`=$verifiedInput2"
Notice the ` around objects in the SQL (table names and column names) and ' around strings.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 06:47 PM   PM User | #12
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
hmm.. now i get this:

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 '' at line 1

form is sending: cval and tval


my action file:
Code:
$tval = mysql_real_escape_string($tval);
$cval = mysql_real_escape_string($cval); 

$sql = mysql_query("DELETE FROM expedia WHERE `tag`='$tval' AND `cpm`=$cval" );
if (!$sql) {
    die(mysql_error());
}

my current submit form:

Code:
form method=post action="purgestrikesearch.php">
<label>Delete Records: Tag Name:</label>
<input size=15 type=text id=tval name=tval>
<label> Where CPM is greater than:</label>
<input size=2 type=text id=cval name=cval>
<input type=submit value="Delete"></b>
stevenryals is offline   Reply With Quote
Old 11-12-2012, 06:51 PM   PM User | #13
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by stevenryals View Post
hmm.. now i get this:

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 '' at line 1

form is sending: cval and tval


my action file:
Code:
$tval = mysql_real_escape_string($tval);
$cval = mysql_real_escape_string($cval); 

$sql = mysql_query("DELETE FROM expedia WHERE `tag`='$tval' AND `cpm`=$cval" );
if (!$sql) {
    die(mysql_error());
}

my current submit form:

Code:
form method=post action="purgestrikesearch.php">
<label>Delete Records: Tag Name:</label>
<input size=15 type=text id=tval name=tval>
<label> Where CPM is greater than:</label>
<input size=2 type=text id=cval name=cval>
<input type=submit value="Delete"></b>
Is that your entire "action file"? Have you made sure you've updated to $_POST['tval'] and $_POST['cval']? Also, wrap your input tag attributes in HTML. You should always do that. <input size="15" type="text" id="tval" name="tval"/>.

If that still doesn't solve anything, at the top of your "action file" (after <?php obviously) put var_dump($_POST); and post here what comes out, as well as your complete "action file".
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 07:15 PM   PM User | #14
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
The variable are coming through:

array(2) { ["tval"]=> string(8) "testing3" ["cval"]=> string(2) "12" }



here is my form: (since we made a couple of small changes there)

Code:
<form method=post action="purgestrikesearch.php">
<label>Delete Records: Tag Name:</label>
<input size="15" type="text" id="tval" name="tval">
<label> Where CPM is greater than:</label>
<input size="2" type="text" id="cval" name="cval">
<input type=submit value="Delete"></b>
Here is action:

Code:
<?php

$db_host =  <defined>
$db_user =  <defined>
$db_pwd = <defined>
$database = <defined>
$table =<defined>

$tval = mysql_real_escape_string($val);
$cval = mysql_real_escape_string($cval); 

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

$sql = mysql_query("DELETE FROM expedia WHERE `tag`='$tval' AND `cpm`=$cval" );
if (!$sql) {
    die(mysql_error());
}



mysql_free_result($result);

?>
sorry to cause such trouble on what is probably a simple mistake
stevenryals is offline   Reply With Quote
Old 11-12-2012, 08:15 PM   PM User | #15
stevenryals
New Coder

 
Join Date: Jul 2012
Posts: 63
Thanks: 4
Thanked 0 Times in 0 Posts
stevenryals is an unknown quantity at this point
here's an update:

i've moved my vardump through the file..

i'm now getting the variables to dump AFTER declaration..

i've added "settype($cval, "float"); to match my database..

no SQL error now.. just completely whitepaged..

Last edited by stevenryals; 11-12-2012 at 09:54 PM..
stevenryals is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:59 AM.


Advertisement
Log in to turn off these ads.