Jump to content

count check


DarkDot

Recommended Posts

Hey I'm trying to create a script that does two checks for a registration website. First I want it to check to see if the person being enteered is already registered. The code I made for this is

 

$check = mysql_query("SELECT id firstname,lastname FROM registrations WHERE firstname='$firstname' && lastname='$lastname'",$link);
	$rows = mysql_num_rows($check);
	if ($rows == "1") { die ("This person has already been added to the registration list, please <a href=\"javascript:history.go(-1)\">go back</a>"); }

 

Then I want it to do a second check based on the value of shortsize. If there are more than 6 of the same shortsize I want it to give the user another message saying they have been placed on a waiting list, in which case I then want to insert then into a waiting list table. I'm pretty sure i know how I would enter it into the database the problem I'm having is that its still letting me enter people into the database even though there are 6 of that shortsize already there. This is the latest code I've tried.

 

		$check2 = mysql_query("SELECT shortsize COUNT(id) FROM registrations WHERE shortsize='$shortsize'",$link);
	if ($check2 == "6") { die ("There are too many attman you have been added to the waiting list."); }

 

These two checks are right after each other and the are followed by this code:

 

		// INSERT INTO DB
	mysql_query("INSERT INTO registrations (firstname,lastname,phone,address,city,zip,sex,grade,clinic,shortsize,shirtsize) VALUES ('$firstname','$lastname','$phone','$address','$city','$zip','$sex','$grade','$clinic','$shortsize','$shirtsize')",$link) OR DIE(mysql_error());

	echo "<p>Thank you, $firstname, your registration information has been sent! .</p><p><a href=\"index.php\">Register another child?</a></p>";
	// end 

 

I'm not sure if I can combine those two checks into one but any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/131830-count-check/
Share on other sites

I dont think this is correct:

$check2 = mysql_query("SELECT shortsize COUNT(id) FROM registrations WHERE shortsize='$shortsize'",$link);
      if ($check2 == "6") { die ("There are too many attman you have been added to the waiting list."); }

 

I think you want to do something more like this:

<?php
$result = mysql_query("SELECT COUNT(id) FROM registrations WHERE shortsize='$shortsize'",$link);
if ($result && mysql_result($result, 0) == 6) { 
    die ("There are too many attman you have been added to the waiting list."); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/131830-count-check/#findComment-684822
Share on other sites

You need to print the results of both queries and make sure that they are returning what you expect them to return.  Remember, mysql_query() returns a resource, it does no return data from your query.  You have to use something like mysql_result() or mysql_fetch_assoc() to actually get the data returned from the query.

Link to comment
https://forums.phpfreaks.com/topic/131830-count-check/#findComment-684825
Share on other sites

I dont think this is correct:

$check2 = mysql_query("SELECT shortsize COUNT(id) FROM registrations WHERE shortsize='$shortsize'",$link);
      if ($check2 == "6") { die ("There are too many attman you have been added to the waiting list."); }

 

I think you want to do something more like this:

<?php
$result = mysql_query("SELECT COUNT(id) FROM registrations WHERE shortsize='$shortsize'",$link);
if ($result && mysql_result($result, 0) == 6) { 
    die ("There are too many attman you have been added to the waiting list."); 
}
?>

 

In that code you used mysql_result() though so shouldn't it work?

Link to comment
https://forums.phpfreaks.com/topic/131830-count-check/#findComment-684829
Share on other sites

I believe the code should work, perhaps there is another error in your script somewhere.  Try printing some data to see what is going on

 

<?php
$sql = "SELECT COUNT(id) FROM registrations WHERE shortsize='$shortsize'";
$result = mysql_query($query);
if (!$result) {
    die('Could not query:' . mysql_error());
}
echo 'query : ' . $query;
echo '<br />';
echo 'result : ' . mysql_result($result, 0);
?>

Link to comment
https://forums.phpfreaks.com/topic/131830-count-check/#findComment-684833
Share on other sites

i'm sorry your right. Anyway when with the new code this is what I get

 

query : SELECT COUNT(id) FROM registrations WHERE shortsize='YS'

result : 15

 

Which is right there are 15 of them, so its more than 6.....it shouldn't let it go through correct? or is it because its ==6 only if its equal to 6 if won't let it go through? is my logic wrong here?

 

Link to comment
https://forums.phpfreaks.com/topic/131830-count-check/#findComment-684849
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.