Jump to content

Help with IF and ELSE statement confusion


andrew_biggart

Recommended Posts

Ok what I am trying to do is create a script which first checks if the user is logged in, if they are display a comment form if they are not do not display the form. Next once that has been checked I want to check if the user has submitted the form, if they have run the insert mysql statement and if they havnt do nothing.

 

I have messed about with it so much and I can get certain parts working but not others, Please help me get my head around it...

 

Here is what I have.

 

				<?php
				if(isset($_SESSION['myusername'])) {
				echo "
						<form method='post' action='#Profile_comment'>
							<table class='pro_addcomment'>
								<tr>
									<td class='pro_add_h' colspan='3'>Add a profile comment</td>
								</tr>
								<tr>
									<td class='pro_add_text' colspan='2'>
									<textarea name='profilecomment' class='pro_add_input'></textarea>
									</td>
									<td rowspan='2' class='pro_view_pic' valign='top'>
									<img src='../Profile_pics/". $image ."' style='width:50px; height:50px;' />
									</td>
								</tr>
								<tr>
									<td class='pro_add_max'>Max 200 characters</td>
									<td class='pro_add_button'>
									<input class='acc_alerts_accept' name='add_comment' type='submit' value='Add Comment' />
									</td>
								</tr>
							</table>								
						</form>
						";

						if(isset($_POST['add_comment'])){

						include("config.php");

						$Comment_user=$_GET['username'];
						$Comment_sender=$_SESSION['myusername'];
						$Comment_comment=$_POST['profilecomment'];
						$Profile_picture=$_SESSION['myavatar'];

						$sql="INSERT INTO biggartfp9_profile_comments_tempt (Comment_user, Comment_sender, Comment_date, Comment_content, Profile_picture)VALUES('$Comment_user','$Comment_sender',  NOW(),'$Comment_comment', '$Profile_picture')";
						$result=mysql_query($sql);


						if($result){
						header("location:profile.php?username=$Comment_user#Profile_comment");
						}
						else{
						}

				}
				else{
				echo"<h1 class=status_red>Please Login to submit a comment!</h1>";
				}		
			?>

Link to comment
Share on other sites

ok sorted the syntax error, but now even when i am logged in it shows the form but also the the you need to be logged in echo. And also the second if statement is not working.

 

Hear is the new code.

 

				<?php
				if(isset($_SESSION['myusername'])) {
				echo "
						<form method='post' action='#Profile_comment'>
							<table class='pro_addcomment'>
								<tr>
									<td class='pro_add_h' colspan='3'>Add a profile comment</td>
								</tr>
								<tr>
									<td class='pro_add_text' colspan='2'>
									<textarea name='profilecomment' class='pro_add_input'></textarea>
									</td>
									<td rowspan='2' class='pro_view_pic' valign='top'>
									<img src='../Profile_pics/". $image ."' style='width:50px; height:50px;' />
									</td>
								</tr>
								<tr>
									<td class='pro_add_max'>Max 200 characters</td>
									<td class='pro_add_button'>
									<input class='acc_alerts_accept' name='add_comment' type='submit' value='Add Comment' />
									</td>
								</tr>
							</table>								
						</form>
						";

						if(isset($_POST['add_comment'])){

						include("config.php");

						$Comment_user=$_GET['username'];
						$Comment_sender=$_SESSION['myusername'];
						$Comment_comment=$_POST['profilecomment'];
						$Profile_picture=$_SESSION['myavatar'];

						$sql="INSERT INTO biggartfp9_profile_comments_tempt (Comment_user, Comment_sender, Comment_date, Comment_content, Profile_picture)VALUES('$Comment_user','$Comment_sender',  NOW(),'$Comment_comment', '$Profile_picture')";
						$result=mysql_query($sql);


						if($result){
						header("location:profile.php?username=$Comment_user#Profile_comment");
						}
						else{
						}

				}
				else{
				echo"<h1 class=status_red>Please Login to submit a comment!</h1>";
				}		

			}
			?>

Link to comment
Share on other sites

Try this.

also in Header(); use a full path to url.

 

				<?php
				if(isset($_SESSION['myusername'])) {
				echo "
						<form method='post' action='#Profile_comment'>
							<table class='pro_addcomment'>
								<tr>
									<td class='pro_add_h' colspan='3'>Add a profile comment</td>
								</tr>
								<tr>
									<td class='pro_add_text' colspan='2'>
									<textarea name='profilecomment' class='pro_add_input'></textarea>
									</td>
									<td rowspan='2' class='pro_view_pic' valign='top'>
									<img src='../Profile_pics/".$image."' style='width:50px; height:50px;' />
									</td>
								</tr>
								<tr>
									<td class='pro_add_max'>Max 200 characters</td>
									<td class='pro_add_button'>
									<input class='acc_alerts_accept' name='add_comment' type='submit' value='Add Comment' />
									</td>
								</tr>
							</table>								
						</form>
						";

						if(isset($_POST['add_comment'])){

						include("config.php");

						$Comment_user=$_GET['username'];
						$Comment_sender=$_SESSION['myusername'];
						$Comment_comment=$_POST['profilecomment'];
						$Profile_picture=$_SESSION['myavatar'];

						$sql="INSERT INTO biggartfp9_profile_comments_tempt (Comment_user, Comment_sender, Comment_date, Comment_content, Profile_picture)VALUES('$Comment_user','$Comment_sender',  NOW(),'$Comment_comment', '$Profile_picture')";
						$result=mysql_query($sql);


						if($result){
						header("Location: profile.php?username=$Comment_user#Profile_comment");
						}
						else{
						}

				}
				else{
				echo"<h1 class=status_red>Please Login to submit a comment!</h1>";
				}		

			}
			?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.