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>";
				}		
			?>

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>";
				}		

			}
			?>

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>";
				}		

			}
			?>

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.