andrew_biggart Posted December 8, 2009 Share Posted December 8, 2009 Ok at the minute I have written some code for my suggestion box so when it is run it will check if the form has been submitted, if it hasnt then it will do nothing if it has it then checks if the user is logged in, if they are not it redirects them to the registration page, if they are it adds the information to the database. <?php include("config.php"); if(isset($_POST['add_suggestion'])) { if(isset($_SESSION['myusername'])) { $Suggestion_username=$_SESSION['name']; $Suggestion_avatar=$_SESSION['myavatar']; $Suggestion_comment=$_POST['Suggestion_comment']; $sql="INSERT INTO admin_suggestionst (Suggestion_username, Suggestion_comment, Suggestion_date, Suggestion_avatar)VALUES('$Suggestion_username', '$Suggestion_comment', NOW(), '$Suggestion_avatar')"; $result=mysql_query($sql); if($result) { echo "<h1 class=status_green>Thanks for your feedback !</h1>"; } else { echo "<h1 class=status_red>Oppps, please try again !</h1>"; } mysql_close(); } else { header("location:registration.php"); } } ?> What I would like to do is after it has checked the the form has been submitted and the user is logged in I would like to add another if statement to count how many suggestions have been submitted by this user, if count = 1 then echo u have already submitted bla bla, if count = 0 then run the add to the databse code. Everytime I try and adapt this into my orginal code I mess one part or another up. Any help would be appreciated thanks. Quote Link to comment https://forums.phpfreaks.com/topic/184413-helping-adding-another-if-statement-please/ Share on other sites More sharing options...
rajivgonsalves Posted December 8, 2009 Share Posted December 8, 2009 you will have to have a select after your insert. Quote Link to comment https://forums.phpfreaks.com/topic/184413-helping-adding-another-if-statement-please/#findComment-973466 Share on other sites More sharing options...
Psycho Posted December 8, 2009 Share Posted December 8, 2009 Adding comments and proper format is key to writing good code. I changed us some of your logic as I prefer to check for the negative/error conditions first and the success conditions are at the end. I also separated the logic (PHP) from the output (HTML) <?php include("config.php"); //Check if a submission was made if(isset($_POST['add_suggestion'])) { //Check if user is logged in if(!isset($_SESSION['myusername'])) { //User is not logged in, redirect to login header("location:registration.php"); } //Set default status color $statusColor = "red"; //User is logged in, check for number of submissions $userName = mysql_real_escape_string($_SESSION['name']); $query = "SELECT COUNT('Suggestion_comment') FROM admin_suggestionst WHERE Suggestion_username='$userName'"; $result = mysql_query($query); if (!$result) { //Problem running query $output = "Oops, please try again !"; } else { //Check for number of submissions $submissionCount = mysql_result($result, 0); if($submissionCount>0) { //User has previous submissions $output = "You already have {$submissionCount} submissions."; } else { //Users first submissions, add it $avatar = mysql_real_escape_string($_SESSION['myavatar']); $comment = mysql_real_escape_string($_POST['Suggestion_comment']); $query="INSERT INTO admin_suggestionst (Suggestion_username, Suggestion_comment, Suggestion_date, Suggestion_avatar) VALUES('{$userName}', '{$comment}', NOW(), '{$avatar}')"; $result=mysql_query($query); if(!$result) { //Problem running query $output = "Oops, please try again !"; } else { //Submission was added successfully $statusColor = "green"; $output = "Thanks for your feedback !"; } } } } mysql_close(); ?> <html> <body> <h1 class="status_<?php echo $statusColor; ?>"><?php echo $output; ?></h1> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/184413-helping-adding-another-if-statement-please/#findComment-973488 Share on other sites More sharing options...
andrew_biggart Posted December 8, 2009 Author Share Posted December 8, 2009 Thanks alot for your help, I will try and get into th habit of doing it that way. I ended up getting it working straight after I posted this thread... Always the way lol Anyway I ended up doing it this way. <?php include("config.php"); if(isset($_POST['add_suggestion'])) { if(isset($_SESSION['myusername'])) { $Suggestion_username=$_SESSION['name']; $Suggestion_avatar=$_SESSION['myavatar']; $Suggestion_comment=$_POST['Suggestion_comment']; $sql = "SELECT count(Suggestion_username) as sugcount FROM admin_suggestionst WHERE Suggestion_username='$Suggestion_username' "; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); if($row['sugcount'] != 0) { echo "<h1 class=status_red>You have already submitted your feedback!</h1>"; } else{ $sql2="INSERT INTO admin_suggestionst (Suggestion_username, Suggestion_comment, Suggestion_date, Suggestion_avatar)VALUES('$Suggestion_username', '$Suggestion_comment', NOW(), '$Suggestion_avatar')"; $result2=mysql_query($sql2); if($result2) { $Message_user=$_SESSION['name']; $Message_sender='AdMiN'; $Message_content='We appreciate you taking the time to send us your feedback, we will try and reply to you as soon as possible.'; $Message_subject='Your feeback has been received.'; $Profile_picture=$_SESSION['myavatar']; $sql3="INSERT INTO user_messagest (Message_user, Message_sender, Message_content, Message_subject, Profile_picture)VALUES('$Message_user', '$Message_sender', '$Message_content', '$Message_subject', '$Profile_picture')"; $result3=mysql_query($sql3); echo "<h1 class=status_green><img alt='' height='16' src='Icons/s_success.png' width='16' />Feedback sent !</h1>"; } else { echo "<h1 class=status_red>Oppps, please try again !</h1>"; } } mysql_close(); } else { header("location:registration.php"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/184413-helping-adding-another-if-statement-please/#findComment-973544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.