Hi everyone :)

I am working on a small error message, but i can not seem to get it to work correctly.

Basically, what I am trying to do, is check the account balance field from a mysql database
against a variable of $smsnum

smsnum is automatically populated based on the number of charatcers entered.
this is working as it should do.

The problem I am facing is if the account balance is less than the number of messages being sent, the error message should be displayed and the script should stop working and display the error message.

What I am trying to do is display an error message if the account balance is less than the number of message being sent against a variable of $smsnum

So if $smsnum is greater than account balance - display the error

Here is the code for my error message.
This has been driving me mad for the last few days now, hope someone can point me in the right direction or explain to me what I am doing wrong

if($smsnum != '') {
		if( strlen($smsnum) > 1 )
            $qryt = "SELECT accbala, securecode FROM table WHERE securecode='$securecode'";
            $result = mysql_query($qryt);
            if($result) {
                //if($result['accbala'] < $smsnum) {
				if(mysql_num_rows($result) < $smsnum) {
                    $msg_lowtexts = '<img src="images/loadingsms.gif" width="16" height="16"
		hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />
		<font color="#FF0000" size="-3">&nbsp;&nbsp;Can not send your message<br />
		You do not have enough texts - Try shortening you message below or <strong>top up with more texts</strong></a>
		</font>';
		@mysql_free_result($result);
		$b1=false;
	}
	}
	}

Recommended Answers

All 19 Replies

if($smsnum != '') {//if(ctype_digit($smsnum)){ ?
	if( strlen($smsnum) > 1 ){//this is going to exclude 0-9 its checking the string length not the value
		$qryt = "SELECT accbala, securecode FROM table WHERE securecode='$securecode'";
		$result = mysql_query($qryt);
		if($result) {
			//if you do want row count this is safer
			$row_count = @mysql_num_rows($result);
			if(!ctype_digit($row_count)){//your version may not support ctype_digit can use is_int() instead
				$row_count = 0;
			}
			$value = $row_count;
			//otherwise you arn't actually looking at the account balance at all
			//the sound of your query the row count is always going to be 1
			//so if you want to get the acount balance value
			$array = mysql_fetch_assoc($result);
			$accountBalance = $array['accbala'];
			$value = $accountBalance;
			
			if($value < $smsnum) {
				$msg_lowtexts = '<img src="images/loadingsms.gif" width="16" height="16"';
				$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
				$msg_lowtexts .= '<font color="#FF0000" size="-3">&nbsp;&nbsp;Can not send your message<br />';
				$msg_lowtexts .= 'You do not have enough texts - Try shortening you message below or <strong>top up with more texts</strong></a></font>';
				$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
				@mysql_free_result($result);
				$b1=false;
			}else{
				$b1=true;
			}
		}
	}
}

Hi thanks for your reply,

so do I remove

if($smsnum != ''){

and just use

{//if(ctype_digit($smsnum)){ ?

I am still getting the same problem....

if the account balance is less than the number of messages being sent,
the message is still being sent ? when the error message should be displayed...

yeah both these 2 lines can be replaced by that 1 if the string length isn't actually a requirement

if($smsnum != '') {//
	if( strlen($smsnum) > 1 ){
to
if(ctype_digit($smsnum)){

what data is in the accbala field?

i also see no actual sending function, i assume b1 being false won't send the text?

got to break it down and debug the vars in that key bit

var_dump($value);//could be a string type float erroring or even null meaning the error is further up, use $value = intval($value); if it is
echo '<br><br>';
var_dump($smsnum);
echo '<br><br>';
if($value < $smsnum) {//$value is less than smsnum(false if they are equal! use <= if it can be)
				$msg_lowtexts = '<img src="images/loadingsms.gif" width="16" height="16"';
				$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
				$msg_lowtexts .= '<font color="#FF0000" size="-3">&nbsp;&nbsp;Can not send your message<br />';
				$msg_lowtexts .= 'You do not have enough texts - Try shortening you message below or <strong>top up with more texts</strong></a></font>';
				$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
				@mysql_free_result($result);
				$b1=false;
}else{
$b1 = true;
}
var_dump($b1);//check this part is actually the problem, it may be after

can't beat just printing out all the values :)

Member Avatar for diafol

My take:

if(is_int($smsnum) && $smsnum > 9){
	//assume all variables are clean
	$result = mysql_query("SELECT COUNT(`accbala`) AS balance, securecode FROM table WHERE securecode='$securecode' LIMIT 1");
	if(mysql_num_rows($result) > 0){
		$d = mysql_fetch_array($result);
		$balance = $d['balance'];
		if($balance < $smsnum) {
			$msg_lowtexts = '<img src="images/loadingsms.gif" width="16" height="16"';
			$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
			$msg_lowtexts .= '<font color="#FF0000" size="-3">&nbsp;&nbsp;Can not send your message<br />';
			$msg_lowtexts .= 'You do not have enough texts - Try shortening you message below or <strong>top up with more texts</strong></a></font>';
			$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
			mysql_free_result($result);
			$b1=false;
		}else{
			$b1=true;
		}
	}
}

The actual html used in the concatenated string contains deprecated tags and inline attributes that could be handled by CSS.

Gents thanks very much for both of your ideas,

I will work through both and reply when I have the error message working correctly,

Ardav ? what is the reason behind if(is_int($smsnum) && $smsnum > 9){

smsnum is a variable created when the user starts typing the message

Member Avatar for diafol

Oh! My mistake - I assumed that the smsnum was the total num of msgs sent and that balance was how many separate messages are allowed to be sent.

Darn. I will redo code. Apologies.

Sorry - hold on - just re-read your 1st post. COuld you give me an idea of the meaning of $smsnum and $balance ($value) again. Typical values of these too?

Hi Gents, this is still not working correctly...

Any ideas why, I have done everything in the above examples but I must be missing something somewhere...

Messages are still being sent to the database -
Its like it is completely bypassing the error message and continuing with the script.

hi ardav, many thanks for helping with this -

$smsnum and $balance

$smsnum is auto populated when a user starts typing a message
accbala is the field name in the database that holds the amount of messages the user has left to send.

when the user clicks on send message - $smsnum could be 1, 2 or 3 (it will never be above three as three messages are only allowed to be sent at one time.)

so if the database field (accbala) = 4 and the user is sending 3 messages, thats ok
if accbala = 3 and the user is sending three messages, again, thats ok.

If accbala is 2 and the user is sending three messages, then the error should be displayed.

If accbala is 1 and the user is sending three messages, the error should be displayed.

Basically, if the database field (accbala) is less than $smsnum (the number of messages being sent) the error message should be displayed.

if accbala is greater than the number of message being sent, that is good, the messages can be sent.

Its driving me mad - Hope that makes sence :)

Member Avatar for diafol

OK, now it makes sense. I assumed the smsnum had to be > 9 from some bit of code you had.

Change:

if(is_int($smsnum) && $smsnum > 9){

to

if(is_int($smsnum) && $smsnum < 4){

That should work.
If not, echo out the main variables (e.g. for my solution):

echo $smsnum;

and then in the relevant place,

echo $balance;

The only thing I can think of is that the mysql_query is not returning a valid resource. As you disabled errors with @, it wouldn't be obvious.

Post back see what it says. If all looks ok, but still doesn't work, echo out the SQL:

echo "SELECT COUNT(`accbala`) AS balance, securecode FROM table WHERE securecode='$securecode' LIMIT 1";

Then copy this to phpmyadmin SQL box and see what it gives - if there is an error, it should give you info.

HI, I went to mysql admin and ran the above query and got the following error message

#1064 - 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 '"SELECT COUNT(`accbala`) AS balance, securecode FROM table WHERE securecode='$se' at line 1

Member Avatar for diafol

Is $securecode set? Doesn't look like it

HI i have ran the query in mysql admin and the query has run successfully -
as it displays the field accbala for the given securecode.

Im totaly lost here

yes, $securecode is set, as when the message is sent to the database - the correct securecode is placed against the correct message -

I also know the variable smsnum is set as the following works -

//update sent database with number number of messages sent
mysql_query("update table set sent = sent + '$smsnum' where securecode='$securecode'");

//send data to  header to use after successfull message sent									
header( 'Location: page.php?sendto=' . $nid . '&s='.$smsnum.'') ;
Member Avatar for diafol

OK, try this with test echoes:

if(is_int($smsnum) && $smsnum < 4){
	//assume all variables are clean
	$result = mysql_query("SELECT COUNT(`accbala`) AS balance, securecode FROM table WHERE securecode='$securecode' LIMIT 1");
	echo "SELECT COUNT(`accbala`) AS balance, securecode FROM table WHERE securecode='$securecode' LIMIT 1";//test
	if(mysql_num_rows($result) > 0){
		$d = mysql_fetch_array($result);
		$balance = $d['balance'];
		echo "<br />balance: $balance<br />";//test
		echo "smsnum: $smsnum";//test
		if($balance < $smsnum) {
			$msg_lowtexts = '<img src="images/loadingsms.gif" width="16" height="16"';
			$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
			$msg_lowtexts .= '<font color="#FF0000" size="-3">&nbsp;&nbsp;Can not send your message<br />';
			$msg_lowtexts .= 'You do not have enough texts - Try shortening you message below or <strong>top up with more texts</strong></a></font>';
			$msg_lowtexts .= 'hspace="2" vspace="2" align="left" title="Low message" alt="Low message" />';
			mysql_free_result($result);
			$b1=false;
		}else{
			$b1=true;
		}
	}else{
              echo "no rows";//test
        }
}else{
        echo "smsnum is all wrong!"//test
}

Hi when add the above piece of code to my page, it stops loading -

Would it be better if I sent you the complete page ?

Hi Ardav - I now have this working - Just wanted to say a massive thank you as I would not of got this working without your input.

Here is the working piece of code - FYI -
With the sql query working in mysql admin, I removed

if(is_int($smsnum) && $smsnum < 4){

I then edited the query and it now works great :)

$result = mysql_query("SELECT accbala FROM table WHERE securecode='$securecode' LIMIT 1");
    if(mysql_num_rows($result) > 0){
    $d = mysql_fetch_array($result);
    $balance = $d['accbala'];
    if($balance < $smsnum) {
    $msg_lowtexts = '<img src="images/loadingsms.gif" width="16" height="16"
		hspace="2" vspace="2" align="left" title="Low text messages" alt="Low text messages" />
		<font color="#FF0000" size="-3">&nbsp;&nbsp;Can not send your message<br />
		You do not have enough texts <br /> Try shortening your message or <strong>top up with more texts</strong></a>
		</font>';
		@mysql_free_result($result);
		$b1=false;
	}
	}
		
	if($b1) 
	
	{

again, a massive thanks for your input, very much appreciated,

Member Avatar for diafol

Great - mark as solved (link above the edit box below). :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.