Jump to content

Problems with an IF statement


dexter_sherban

Recommended Posts

I'm trying to make a bulleting board. It has login, submit, edit and delete option. To keep it efficient I want it to have only 2 pages. One with the login(that I've finished) and one with the message output and the edit/delete/submit options. Im relatively new to PHP. I thought of making is menu like. So whenever the user selects an option, the program will go to an IF statement and execute the function needed. Also my database is mysql and I store the messages there.

 

I finlay got it to...well show something. I want the edit to work like this. you click on the edit button under the message u want to edit, and then the page will refresh itself and instead of the old message it will show a input form with the subject/message, you enter new data in it and update it.

 

So I think the if statement should work, And I thought about it a long time, it still hasnt hit me. any ideas. OH and my database key is 'entry'

 

<html>
<head>
</head>
<body>
<?php

$name=$_POST["username"];
$pass=$_POST["password"];
$entry=$_POST["entry"];
$subject=$_POST["subject"];
$message=$_GET["message"];
$option=$_GET["option"];
$date=date('y-m-d');

//Store initial entry...at one time I though the entry variable kept getting overwritten but still this didnt help...
$initialentry=$entry;
echo "$initialentry";


$con=mysql_connect("localhost","user","pass");
if (!$con)
{
die ('could not connect: ' .mysql_error());
}
mysql_select_db("Serban",$con);

function Forminput()
{
echo "<form action='Bulletin.php' method='POST'><br><br>
	<input type='hidden' name='entry' value=$entry>
	<input type='hidden' name='name' value=$name>
	Subject: <br><input type='text' name='subject' value='$subject'><br><br>
	Message: <br>";
 echo "<textarea cols='20' rows='10' name='message'>";
if(isset($message))
echo htmlspecialchars($message);
echo "</textarea>"; 
	echo "<br><br><input type='submit' value='edit/upload'></form>";
}

function Delete()
{
mysql_query("delete from message where entry='$entry'");
}

function Edit()	
{
mysql_query("update message set subject='$subject',message='$message',date='$date' where entry=$entry");
}

function Insert()
{
$result=mysql_query("select * from message");
$entry= mysql_num_rows($result);
mysql_query("insert into message (subject, message, date, entry) VALUES ('$subject','$message','$date', '$entry')");
}

Edit();

$data=mysql_query("select * from message order by entry desc");

while($row = mysql_fetch_array($data)) 
{
	$entry=$row['entry'];
                //This is the IF im walking about.
	if ($option==1 && $entry==$initialentry)
	{
	$subject=$row['subject'];
	$message=$row['message'];

	Forminput();
	}
	elseif (option == 2)
	{
	Delete();
	}
	else
	{
	echo "Posted on ";
	echo $row['date'];
	echo "<br><b>";
	echo "Subject : ";
	echo "</b>";
	echo $row['subject'];
	echo "<br><b>";
	echo "Message : ";
	echo "</b>";
	echo $row['message'];
	echo "<br>";
	echo "<br>";
	echo "<form>
		<input type='hidden' name='entry' value=$entry>
		<input type='hidden' name='option' value=1>
		<input type='submit' value='edit' action='Bulletin.php' method='POST'>
		</form>";
	echo "<form>
		<input type='hidden' name='entry' value=$entry>
		<input type='hidden' name='option' value=2>
		<input type='submit' value='delete' action='Bulletin.php' method='POST'>
		</form>";
	echo "<br>";
	}		
}
Forminput();
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/45274-problems-with-an-if-statement/
Share on other sites

function Forminput()
{
        global $message;
echo "<form action='Bulletin.php' method='POST'><br><br>
	<input type='hidden' name='entry' value=$entry>
	<input type='hidden' name='name' value=$name>
	Subject: <br><input type='text' name='subject' value='$subject'><br><br>
	Message: <br>";
 echo "<textarea cols='20' rows='10' name='message'>";
if(isset($message))
echo htmlspecialchars($message);
echo "</textarea>"; 
	echo "<br><br><input type='submit' value='edit/upload'></form>";
}

 

Try that.

Thanks for the previous post. It made me figure out why my form inputs are empty. The $option come from a hidden form.

 

echo "<form>
		<input type='hidden' name='entry' value=$entry>
		<input type='hidden' name='option' value=2>
		<input type='submit' value='delete' action='Bulletin.php' method='POST'>
		</form>";
	echo "<br>";

 

I guess we can say my problem is resolved. Figured out it was my GET and POST.They were all over the place, and as you can see from the line of code up, my forms were all bad two. It works fine now. Still thanks for your help.

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.