Checking number of rows and checking status of row

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

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Checking number of rows and checking status of row

Post by Smudly »

Hi,

I'm currently working on a support page. Right now, it successfully allows a user to type in a question, where the information is inserted into my database. With one flaw though.

There is a check that ensures the user doesn't have more than 3 questions at a time. I need to have this check for questions that also have a status "answered" set to 'no'.

For example: For every question that the user asks, the answered status is set to 'no' by default. If the user has more than 3 unanswered questions at any time, they won't be able to ask another question. If they have 5 questions they have asked though perhaps, but 4 are set to 'yes', and 1 is set to 'no, they will be able to ask a question.

What should I change to get this to work properly?
Thanks

Code: Select all

<?php
session_start();
$username = $_SESSION['username'];

if(isset($_SESSION['username'])){  

$submit = $_POST['submit'];

if($submit){
include_once('inc/connect.php');

$supportsql = mysql_query("SELECT * FROM `users` WHERE `username`='$username'"); 
$row = mysql_fetch_assoc($supportsql); 

$userid = $row['id']; 

$message = $_POST['problem'];
$date = date("Y-m-d");

$checksql = mysql_query("SELECT id FROM `support` WHERE `id`='$userid'");
$idcount = mysql_num_rows($checksql);

$checkmessage = mysql_query("SELECT message FROM `support` WHERE `message`='$message'");
$messagecount = mysql_num_rows($checkmessage);

// Fix this to take in account if the question has already been answered.
if ($idcount<=2){

	if ($messagecount==0){
$supportquery = "INSERT INTO support VALUES ('','$userid','$username','$message','no','$date')";
$supportresult = mysql_query($supportquery) or die("Error Inputting Message");
}
else{
	$error = "You have already submitted this question. It will be replied to within 24 hours.";
}

}
else{
	$error = "Sorry, you currently have 3 Support Tickets awaiting to be answered.<br />Please wait until these are answered before asking another question.";
}
}
}
else{
	header("Location: index.php");
}

?>

<html>
<head>
<title>Support</title>
</head>
<body OnLoad="document.support.problem.focus();">

<center>
<h1>Get Support!</h1>
<form name="support" action="support.php" method="POST">
<textarea rows="8" cols="50" name="problem"><?php echo $message; ?></textarea><br />
<input type="submit" name="submit" value="Submit">
</form>
<br />
<?php echo $error; ?>
</center>

</body>
</html>
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Checking number of rows and checking status of row

Post by jraede »

No need to separate the queries on the support table...just do something like this

Code: Select all

$result = mysql_query("SELECT * FROM `support` WHERE `id`='$userid' AND `answer`='no'");
if(mysql_num_rows($result) >= 3) // error
else // good to go
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Re: Checking number of rows and checking status of row

Post by Smudly »

Thanks! All fixed.
Post Reply