Log in

View Full Version : Resolved Another error



keyboard
08-17-2011, 10:01 AM
hey everyone,


<?php

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user;






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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];


$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";



if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
}





else
{
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>

<?php
}
?>

Causing this error

( ! ) Parse error: syntax error, unexpected $end in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\pm system\new_message.php on line 59


Can't work out why. Any help?

JShor
08-17-2011, 01:22 PM
You never ended this line with a }:


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


Try this:


<?php

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user;






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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];


$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";



if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
}





else
{
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>

<?php
}

}
?>

keyboard
08-17-2011, 09:32 PM
<?php

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user;







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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];


$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";



if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
}


$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);




else
{
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>

<?php
}

}
?>


Somewhere it's causing this error

( ! ) Parse error: syntax error, unexpected T_ELSE in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\pm system\new_message.php on line 41

I'm 99% sure it in here


$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);

bluewalrus
08-17-2011, 09:45 PM
It's caused by



else
{
?>
<form name="messa

The error message provides you with a lot of information



( ! ) Parse error: syntax error, unexpected T_ELSE in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\pm system\new_message.php on line 41

When is this suppose to execute if the condition is met or isnt?


$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);

keyboard
08-18-2011, 12:33 AM
If the form has been submitted, It will upload it to the database. Otherwise It will display the form.
The error only started when I put this code in

$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);

bluewalrus
08-18-2011, 12:35 AM
Okay so you just need to add a curly brace before the else



$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);




} else
{

keyboard
08-18-2011, 01:52 AM
?php

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user;







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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];


$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";



if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");


$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);




} else
{
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>

<?php
}

}
?>






Is that how it should look because when I open it the whole page is blank

traq
08-18-2011, 04:50 AM
if you don't have the cookie set, or $_POST['submit'], then you should get a blank page.


1) if there's no cookie, then $user will be empty, and echo will print nothing.

2) then, if the form was not submitted (likely, as it was never displayed), the first if() will be FALSE, and execution will skip to the end of the script.

3) you're done, and nothing happened.
(I suspect your first if() conditional is supposed to end earlier than it does. Also, even if your form was submitted, you'd never insert any records because you die() right before the lines that query the db.)



Honestly, I would try rewriting this after planning out the structure a little more. All of your php code should come as early as possible, and be mostly (preferably completely) done by the time you start outputting html. Make a step-by-step outline of what needs to happen in php (if you do so here, I'll be happy to offer advice), then put the html at the end.

djr33
08-18-2011, 09:48 AM
In addition to Traq's advice, if you for some reason want a blank page in certain circumstances, then for testing it will be a good idea to add some sort of feedback to be sure the page is working. A blank page can mean the page is not processing at all (there is some major error in parsing the PHP) or that there is nothing to show (no error, just no content). To fix that, you can add a single line at the end of your script, <?php echo 'Hello World'; ?>. This way at the very least you know your script has no parse errors. But generally you should just follow what Traq said.

keyboard
08-19-2011, 09:36 AM
echo "Thanks";

Thats why I put that in

traq
08-19-2011, 04:51 PM
but if the form is not submitted yet, then you won't see that. And the form won't be printed because you have it inside the same if() block. look over your code carefully (and I'd recommend indenting it properly to make it easier to read, and catch these kinds of mistakes):
?php //<--- you didn't start with <?php
// that's another possible cause of your blank page

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user; //<-- if there is no cookie, then $user will be empty
// and you won't see any output.

// if this is the case, and you also don't see any errors,
// (probably something like "Warning: undefined index 'ID_my_site'")
// then you need to turn on error reporting


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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];


$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";



if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");


$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);




} else //<-- this "else" closes "if mysql_num_rows([...]) === 0"
// but we're _still_inside_ "if isset($_POST['submit'])"
{
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>

<?php
} //<-- this is where the else for "mysql_num_rows([....]) === 0" ends

} //<-- this is where "isset($_POST['submit'])" ends
// as you can see, _nothing_happens_ if the form is not submitted,
// and you'll get a blank page.
?>

keyboard
08-23-2011, 07:21 AM
} else //<-- this "else" closes "if mysql_num_rows([...]) === 0"
// but we're _still_inside_ "if isset($_POST['submit'])"
{

but when I change it to this


} }else //<-- this "else" closes "if mysql_num_rows([...]) === 0"
// but we're _still_inside_ "if isset($_POST['submit'])"
{

It comes up with this error

( ! ) Parse error: syntax error, unexpected '}' in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\pm system\new_message.php on line 56

bluewalrus
08-23-2011, 03:49 PM
On line 58 or 60 remove the closing bracket since you already closed it on 41, you should also adjust your syntax to use indents that would make this much easier to read.


} //<-- this is where "isset($_POST['submit'])" ends



Indented




<?php
// that's another possible cause of your blank page

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user; //<-- if there is no cookie, then $user will be empty
// and you won't see any output.

// if this is the case, and you also don't see any errors,
// (probably something like "Warning: undefined index 'ID_my_site'")
// then you need to turn on error reporting


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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];
$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";
if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ) {
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);
}
} else {
// below is displayed if submit is not set
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} //<-- this is where the else for isset submit ends
?>


You should also escape all of these


$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];


http://php.net/manual/en/function.mysql-real-escape-string.php
http://en.wikipedia.org/wiki/SQL_injection

keyboard
08-23-2011, 11:32 PM
The form isn't inserting anything into the sql database. Any help?

traq
08-24-2011, 01:08 AM
please post the current version of the code you're working with, along with any output/errors.

bluewalrus
08-24-2011, 01:23 AM
I think your conditional is off, this should do it


if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 )
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
else {
$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);
}

Assuming that SQL is checking if the user exisits.

keyboard
08-24-2011, 03:33 AM
( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\pm system\new_message.php on line 20
Call Stack
# Time Memory Function Location
1 0.0006 375632 {main}( ) ..\new_message.php:0
2 0.0144 382488 mysql_num_rows ( ) ..\new_message.php:20


Thats the error I got after changing the code you suggested, Blue Walrus.



<?php


require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user;


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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];

if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 )
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
else {
$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);
}


} else {
// below is displayed if submit is not set
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} //<-- this is where the else for isset submit ends
?>


Thats the full code

JShor
08-24-2011, 05:39 PM
This thread is reaaally dragged out.

Pay attention to this line:


if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 )


Nowhere in your code is $ck_reciever declared, therefore mysql_query() is querying nothing, returning false (and a false value is passed as the parameter to mysql_num_rows(), which is why you're getting a an error that a boolean value is being passed as the parameter to mysql_num_rows().'

I think when bluewalrus asked you to change the code, he assumed that you would leave the query in the code.

Try this:


<?php
// that's another possible cause of your blank page

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user; //<-- if there is no cookie, then $user will be empty
// and you won't see any output.

// if this is the case, and you also don't see any errors,
// (probably something like "Warning: undefined index 'ID_my_site'")
// then you need to turn on error reporting


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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];
$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";

if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 )
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
else {
$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);
}
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} //<-- this is where the else for isset submit ends
?>

keyboard
08-24-2011, 08:32 PM
Hi everyone, I'm sorry this is taking so long. When I submit the form, nothing happens. It is still displaying my username, but the message isn't submitted. Also I can put anything I want into the to field of the form and it is not coming up with the , the user you are trying to contact does not excist,.


<?php
// that's another possible cause of your blank page

require "database.php";
$userfinal=$_COOKIE['ID_my_site'];
$user=$userfinal;
echo $user; //<-- if there is no cookie, then $user will be empty
// and you won't see any output.

// if this is the case, and you also don't see any errors,
// (probably something like "Warning: undefined index 'ID_my_site'")
// then you need to turn on error reporting


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

echo "Thanks";

$title=$_POST['message_title'];
$to=$_POST['message_to'];
$content=$_POST['message_content'];
$from=$_POST['message_from'];
$time=$_POST['message_date'];
$ck_reciever = "SELECT username FROM users WHERE username = '".$to."'";

if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 )
die("The user you are trying to contact don\'t excist. Please go back and try again.<br> ");
else {
$insert = "INSERT INTO pmsys (to, from, title, message, date, ifread)
VALUES ('".$to."', '".$from."', '".$title."', '".$content."', '".$date."', '".$ifread."')";
$add_member = mysql_query($insert);
}
?>
<form name="message" action="new_message.php" method="post">
Title: <input type="text" name="message_title"> <br>
To: <input type="text" name="message_to"> <br>
Message: <br>
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<input type="hidden" name="message_from" value="'.$user.'"><br>';
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} //<-- this is where the else for isset submit ends
?>


That's the current code. Any help?

JShor
08-24-2011, 09:00 PM
Well, following your code by the output that you said, the only logical possibility is that $ck_receiver doesn't return zero rows, which means that conditional that determines if it is zero defaults to the else, and attempts to execute your SQL syntax. But, it's not. Which means there is probably an SQL error blocking it from executing.

Try adding or die(mysql_error()); to the end of your mysql_query() like so:


$add_member = mysql_query($insert) or die(mysql_error());

traq
08-24-2011, 09:16 PM
your mysql error will probably tell you that you have syntax error.

one of your fields is named from, which is a mysql keyword (command). you must tell mysql that you mean to use the literal value of the word. you should always use backticks to specify literal words (column names, etc.) in your sql commands:
$insert = "
INSERT INTO `pmsys` (`to`, `from`, `title`, `message`, `date`, `ifread`)
VALUES ('$to', '$from', '$title', '$content', '$date', '$ifread')"; note that these are backticks ( ` ), NOT single-quotes ( ' ).

(I could have sworn that we'd addressed this issue before, but I guess it was a different thread. also, in PHP, you don't need to break out of a double-quoted string to use a variable. that's what double-quotes are for, to use $variables inside a string. note my changes above.)

I've looked over your current code again (from post 19).
you still have the form inside your first if() block.
If you can see the form, then you're either submitting from a different page, or this is not the current version of the code you're using.
you have _no_ form fields named message_from or message_date.
this means that your $time and $from variables will always be empty.
further, your sql statement uses the variable $date (which doesn't exist), not $time as I suspect you intended.
the variable $ifread doesn't exist, either.
you don't escape any of your POST values, so your database is wide open to sql injection attacks.

As I said earlier, I would strongly recommend planning this all out first, and then opening a new file and rewriting it from scratch.
As it stands now, you are losing track of where your changes were made, and you're starting to have old problems show up again (probably as a result of copy-pasting).

I'll be happy to help you further if you decide to follow this advice.

keyboard
08-25-2011, 12:36 AM
Thanks for the advise Traq. If this dosn't work I will plan it all out then rewrite it all.

The page that the form is on, is also the page that is processing the form. It's called messageck.php

I would like it so that If the form hasn't been submitted yet, The form shows up, else it says thankyou and the form disappears. So the form can't be in the isset $ post and I think that's were the problem is. Could anyone offer a soloution?

traq
08-25-2011, 04:57 AM
I would like it so that If the form hasn't been submitted yet, The form shows up, else it says thankyou and the form disappears. So the form can't be in the isset $ post and I think that's were the problem is. Could anyone offer a soloution?

you should re-read posts #2, 6, 8, 11, 13, 16, and 21.

As far as "planning it out" goes, I'm going to press the issue. here's an outline of what you're trying to do (as I understand it so far):
if( /* cookie exists? */ ){
/* set user name */
} /* (no else{} block) */

if( /* form was submitted? */ ){
/* prepare variables to process form submission */

if( /* "to" user exists? */ ){
/* save message to database, display "thanks" */

}else /* "to" user doesn't exist */ {
/* display error message */
}

}else /* form not submitted */ {
/* display form */
}
once you understand the process that your script needs to follow, it becomes much easier to write the code.

keyboard
08-25-2011, 06:37 AM
Thanks Traq for all your suggestions. I have gotten the script working. Thankyou so much everyone.