checking for the existence of a value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

checking for the existence of a value

Post by Obadiah »

i know we have been over this before but i cant seem to find the thread...but i we have not here goes....

i have a table called merchant and a field called merchant_num...what im wanting to do is
if the value of merchant number is matched by a number that a user is wanting to enter into the database it gives the error message "The merchant number $merchant_num has already been entered into the database please check merchant and try again"

how do i do this? right now i have this and its giving me a parse error a cant see bc i think ive been staring at it too long...lol

Code: Select all

$sql = "SELECT merchant_num FROM merchant WHERE merchant_num = '{$_POST[merchant_num]}'";
if (merchant_num == ($conn,'{$_POST[merchant_num]}'))
{
	echo "The merchant number $merchant_num has already been entered into the database please check merchant and try again";
}
going a little deeper than the parse i have a feeling that it wouldnt work without the parse....help please
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

first thing I see is 'merchant_num':

is that a constant (I already know the answer, just asking rhetorically). Make sure your variable names have dollar signs in front of them.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

sup burrito....thanks for the reply :) i cant believe i missed that one :oops: i changed it....however im still getting the same parse error

ive named the primary key in the table "merchant" 'merchant_num' and i have also named the text field containing it the same....is that what your meaning by constant?

here is the correction ive made

Code: Select all

$sql = "SELECT merchant_num FROM merchant WHERE merchant_num = '{$_POST[merchant_num]}'";
if ($merchant_num == ($conn,'{$_POST[merchant_num]}'))
{
	echo "The merchant number $merchant_num has already been entered into the database please check merchant and try again";
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I have no idea what you're trying to do with this:

Code: Select all

if ($merchant_num == ($conn,'{$_POST[merchant_num]}'))
what is the parse error you're getting?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

Parse error: parse error, unexpected ',' in C:\Program Files\file.php on line 29
what i was trying to do was compare the string entered in the textfield to the string in the database...if there was a similar one then tell the user to change it instead of it crapping out like it does now(it doesnt actually crap out...this is what happens)

(ignoring for a secong what im trying to do)
right now if the merchant_num string being submitted matches one in the database....it gives the error message "something went wrong" via

Code: Select all

if(mysql_query($sql, $conn))
{
	header("Location: $next_program");
}
else
{
	echo "something went wrong";
}
but thats not good because that error message is for me to know that there is a much bigger problem going on with my application that needs to be fixed(or just different perhaps) so instead of me having to run over to customer service everytime the lady there decides to put in a similar merchant number because of a message meant for me i can make one of her own
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well I suspect your parse error is elseware on the page... what is on (or around) line 29?

also, the query is going to return true every time unless it has an error, you need to check against mysql_fetch_assoc() or the like to see if it returned any rows.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

k....heres my code

Code: Select all

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
 
$conn = mysql_connect("somehost","someuser","andthepassword") or die(mysql_error());
mysql_select_db("customerdirectory",$conn) or die(mysql_error());
$next_program = "added.php";

$sql = "INSERT INTO merchant values('$_POST[merchant_num]',
										'$_POST[date_recieved]',
										'$_POST[merchant_name]',
										'$_POST[purchase_type]',
										'$_POST[lease_score]',
										'$_POST[amex]',
										'$_POST[app_id]',
										'$_POST[discover]',
										'$_POST[user_name]',
										'$_POST[check_conversion]',
										'$_POST[gift_loyalty]',
										'$_POST[app_type]',
										'$_POST[terminal]',
										'$_POST[serial_num]',
										'$_POST[nms]',
										'$_POST[ckmerchant_num]',
										'$_POST[giftmerchant_num]',
										'$_POST[comments]')";

$sql = "SELECT merchant_num FROM merchant WHERE merchant_num = '{$_POST[merchant_num]}'";
if ($merchant_num == ($conn,'{$_POST[merchant_num]}'))//this is line 29
{
	echo "The merchant number $merchant_num has already been entered into the database please check merchant and try again";
}
										
if(mysql_query($sql, $conn))
{
	header("Location: $next_program");
}
else
{
	echo "something went wrong";
}


?>
now ive done something similar in my login script...but i cant figure out a way to rewrite it for this instance

like you said, i think that i would need to use

Code: Select all

$num = mysqli_num_rows($result);

then perhaps do something like

Code: Select all

$result2 = mysqli_query($cxn,$sql)
                   or die("Couldn't execute query 2.");  
         $row = mysqli_fetch_assoc($result2);

but as far as what to do next im at a lost :? especially with this parse....i cant see where in the heck its comming from and until its fixed it it wont let me see if anything else works :(
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

again, I go back to this line:

Code: Select all

if ($merchant_num == ($conn,'{$_POST[merchant_num]}'))//this is line 29
it makes no sense...for starters, what is $merchant_num? it's not a variable defined on that page, secondly what are you trying to do with ($conn, $_POST....)?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

Burrito wrote:again, I go back to this line:

Code: Select all

if ($merchant_num == ($conn,'{$_POST[merchant_num]}'))//this is line 29
it makes no sense...for starters, what is $merchant_num? it's not a variable defined on that page, secondly what are you trying to do with ($conn, $_POST....)?
im sorry...i really suck at trying to explain things correctly...so ill try again

ok...$merchant_num is just a value being posted to the DB

here it starts out as just a textbox or on
a simple html form page

Code: Select all

<input type="text" name="merchant_num" size="12" maxlength="12">
then its taken to this page which puts it into the database here on a processing page

Code: Select all

$sql = "INSERT INTO merchant values('$_POST[merchant_num]');
then finally i output it to the screen like this on a different page

Code: Select all

<td align="center" class="output">$merchant_num &nbsp;</td>
all this works fine....what i am trying to do now is compare the string being entered via form submission on the html form page to one that is already in the database and if it exist then tell me that it does instead to giving the message that it does
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

wait...much appologies for not understanding if this is what your saying.....are you saying that here the value of the textbox merchant_num hasnt been processed yet...but if thats the case how do i get the value of the box to match it up to whats in the database?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

let me break down what your code is doing for you:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// error reporting...that's all fine

$conn = mysql_connect("somehost","someuser","andthepassword") or die(mysql_error());
//connect to a mysql database....that's all fine

mysql_select_db("customerdirectory",$conn) or die(mysql_error());
// select the db....that's all fine

$next_program = "added.php";
// set a string variable...that's all fine

$sql = "INSERT INTO merchant values('$_POST[merchant_num]',
                                                                                '$_POST[date_recieved]',
                                                                                '$_POST[merchant_name]',
                                                                                '$_POST[purchase_type]',
                                                                                '$_POST[lease_score]',
                                                                                '$_POST[amex]',
                                                                                '$_POST[app_id]',
                                                                                '$_POST[discover]',
                                                                                '$_POST[user_name]',
                                                                                '$_POST[check_conversion]',
                                                                                '$_POST[gift_loyalty]',
                                                                                '$_POST[app_type]',
                                                                                '$_POST[terminal]',
                                                                                '$_POST[serial_num]',
                                                                                '$_POST[nms]',
                                                                                '$_POST[ckmerchant_num]',
                                                                                '$_POST[giftmerchant_num]',
                                                                                '$_POST[comments]')";
// set another string variable....that's all fine

$sql = "SELECT merchant_num FROM merchant WHERE merchant_num = '{$_POST[merchant_num]}'";
// overwrite the $sql variable...not what you want to do probably, but it's fine.

if ($merchant_num == ($conn,'{$_POST[merchant_num]}'))//this is line 29
// this is where it gets wonky.  you have a local variable called $merchant_num that has not been defined anywhere on this page
// You're then trying to compare that variable's value to something that is non-existent in php as far as I know
// I don't see any parse errors, other than php might be puking on this logic as it's not correct
{
        echo "The merchant number $merchant_num has already been entered into the database please check merchant and try again";
}
                                                                               
if(mysql_query($sql, $conn))
// this will return true every time unless you have a sql syntax error, so the redirect will always happen.
{
        header("Location: $next_program");
}
else
{
        echo "something went wrong";
}


?>
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

ok...i get where you were saying the problems were...so i tried a fix

Code: Select all

$sql = "SELECT merchant_num FROM merchant WHERE merchant_num='".mysqli_real_escape_string($cxn,$_POST['merchant_num'])."';

$result2 = mysqli_query($cxn,$sql) or die("Couldn't execute query 2.");  
$row = mysqli_fetch_assoc($result2);

while ($newArray = mysql_fetch_array($result))
{
	$merchant_num = $newArray['merchant_num'];
}

if($row)
{
	$merchant_num != mysqli_real_escape_string($cxn,$_POST['merchant_num']);
	header("Location: $next_program");
}
else
{
	echo" The merchant number $merchant_num has already been entered into the database please check merchant and try again<br>";
           extract($_POST);
}		
//i dont quite get what you meant about this line always evaluating to 
//true....because...if i tried to put in a merchant number that matches one in 
//the database it would echo something went wrong like its supposed to

if(mysql_query($sql, $conn))
{
	header("Location: $next_program");
}
else
{
	echo "something went wrong";
}


?>
lol....theres a parse error here too...i think im getting worse
Last edited by Obadiah on Wed Nov 22, 2006 2:07 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

($conn,'{$_POST[merchant_num]}')
There's a comma between $conn and '$_POST -> invalid.


And for the new code: Take a look at the colors the syntax highlighter used. Most of the script is red. And that's used for string literals. The highlighter "thinks" most of your script is a string. And that's because you forgot to close a string (and forgot a ; too)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$check_num = $_POST['merchant_num'];
// Validate $check_num here

// Validation complete

$sql = "SELECT `merchant_num` FROM `merchant` WHERE `merchant_num` = $check_num";
if (!$result = mysqli_query($cxn, $sql))
{
    die(mysqli_error());
}

if (mysqli_num_rows($result) > 0)
{
        echo 'The merchant number ' . $check_num . ' has already been entered into the database please check merchant and try again';
}

// Otherwise proceed
?>
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

thanks alot to all of you guys for replying

@everah= i implemented what you posted and got this
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\Program Files\status_modified.php on line 35

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files\status_modified.php on line 37
heres my new code

Code: Select all

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
 
$conn = mysql_connect("somehost","someuser","yimimbe") or die(mysql_error());
mysql_select_db("customerdirectory",$conn) or die(mysql_error());
$next_program = "added.php";

$sql = "INSERT INTO merchant values('$_POST[merchant_num]',
										'$_POST[date_recieved]',
										'$_POST[merchant_name]',
										'$_POST[purchase_type]',
										'$_POST[lease_score]',
										'$_POST[amex]',
										'$_POST[app_id]',
										'$_POST[discover]',
										'$_POST[user_name]',
										'$_POST[check_conversion]',
										'$_POST[gift_loyalty]',
										'$_POST[app_type]',
										'$_POST[terminal]',
										'$_POST[serial_num]',
										'$_POST[nms]',
										'$_POST[ckmerchant_num]',
										'$_POST[giftmerchant_num]',
										'$_POST[comments]')";


$check_num = $_POST['merchant_num']; 
// Validate $check_num here 

// Validation complete 

$sql = "SELECT `merchant_num` FROM `merchant` WHERE `merchant_num` = $check_num"; 
if (!$result = mysqli_query($conn, $sql)) 
{ 
    die(mysqli_error()); 
} 

if (mysqli_num_rows($result) > 0) 
{ 
        echo 'The merchant number ' . $check_num . ' has already been entered into the database please check merchant and try again'; 
} 

if(mysql_query($sql, $conn))
{
	header("Location: $next_program");
}
else
{
	echo "something went wrong";
}


?>
Post Reply