Page 1 of 1

Trouble with PHP_SELF, I think

Posted: Tue Feb 11, 2003 11:51 am
by Purdue23
I am having trouble with the attached code. When I select the Add Joke link, echo("<p><a href='$PHP_SELF?addjoke=0'>Add a Joke!</a></p>");, it does not show the textarea to add a joke. It just brings up the original page with the new address, http://localhost/jokes.php?addjoke=0.

Can someone help?

Thanks

<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($addjoke)): // If the user wants to add a joke?>
<form action="<?php echo($_SERVER("PHP_SELF"))?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>

<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT") {
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
} else {
echo("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}
}

echo("<p> Here are all the jokes in our database: </p>");

// Request the text of all the jokes
$result = @mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}

// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p><a href='$PHP_SELF?addjoke=0'>Add a Joke!</a></p>");

endif;

?>
</body>
</html>

Posted: Tue Feb 11, 2003 12:14 pm
by daven
## Line 6: a) there should be a '{', not a ':' after the if statement; b) you need to use $_GET['addjoke'] to access the URL variable

Code: Select all

<?php
if (isset($_GET['addjoke'])){ // If the user wants to add a joke?>
## Line 7: it should be $_SERVER['PHP_SELF'] (brackets & single quotes)

Code: Select all

<form action="<?php echo($_SERVER['PHP_SELF'])?>" method="post">
## line 58: a) use . to concatenate stuff b) escape double quotes

Code: Select all

<?php 
echo("<p><a href="".$PHP_SELF."?addjoke=0">Add a Joke!</a></p>");
?>
You can use $_SERVER['PHP_SELF'] or $PHP_SELF. Either/both are acceptable. I tend to use $_SERVER['PHP_SELF'] because I like explicit variable names and I also jump between languages a lot so it is easier for me to read.

Parse error: parse error, unexpected T_ELSE

Posted: Tue Feb 11, 2003 12:31 pm
by Purdue23
Now I am getting this error:


Parse error: parse error, unexpected T_ELSE in C:\Program Files\Apache Group\Apache2\htdocs\jokes.php on line 19

Textarea to show up on same page

Posted: Tue Feb 11, 2003 12:46 pm
by Purdue23
The GET method brings the text area up on a different page. I want it to show up on the same page. Also, when using the get method and filling out the textarea, it still does not update the database.

Please Help!

Posted: Tue Feb 11, 2003 12:51 pm
by volka
http://www.php.net/manual/en/control-st ... syntax.php

Code: Select all

if (condition):
...
elseif:
...
else:
...
endif;
is alright; one suggestion: choose one syntax and stay with it (and choose the the C-like style ;) )
$_SERVER("PHP_SELF") that would call a function with the parameter PHP_SELF (and $_SERVER would contain the name/reference of that function).
$_SERVER['PHP_SELF'] most likely is what you want.
Read http://www.php.net/manual/en/reserved.variables.php to learn more about the superglobal arrays defeined in php.
Also read viewtopic.php?t=511 to see wether isset($addjoke) is ok for your version of php
The GET method brings the text area up on a different page.
yes it's a request for a complete new document.

Posted: Tue Feb 11, 2003 1:16 pm
by daven
The $_GET method is for when you submit things with form method="get" or do a direct url encode. When you use form method="post" you need to use $_POST. Try something like the following:

Code: Select all

<?php
// Form to enter joke
if (isset($_GET['addjoke'])){?>
  <form action="<?php echo($_SERVER['PHP_SELF'])?>" method="post">
  <p>Type your joke here:<br />
  <textarea name="joketext" rows="10" cols="40" wrap></textarea>
  <br />
  <input type="submit" name="submitjoke" value="SUBMIT" /></p>
  </form> 
<?php }?>


<?php // to process joke upload
if (isset($_POST['joketext'])) { // check to see if a joke has been posted
  $sql = "INSERT INTO Jokes SET JokeText='$joketext', JokeDate=CURDATE()";
  mysql_query($sql,$dbcnx) or die(mysql_error());
} 
?>

<?php // the link to add a new joke
echo("<p><a href="".$_SERVER['PHP_SELF']."?addjoke=0">Add a Joke!</a></p>");
?>
Go read viewtopic.php?t=511 for info on variable passing.

Hope this helps. :)

doesn't seem like section of code is working

Posted: Tue Feb 11, 2003 2:16 pm
by Purdue23
The get method brings up the textarea page, and I can submit the text. But the database is not being updated. Do you have to use Post for this to occur? When I try to use Post nothing happens. I bolded the section of code that is not executing.

Any suggestions?

<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>

<?php
if (isset($_POST['addjoke'])): // If the user wants to add a joke?>

<form action="<?php echo($_SERVER['PHP_SELF'])?>" method="POST">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>

<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
if (!$dbcnx){
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}

// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT"){
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
}
else{
echo("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}}
echo("<p> Here are all the jokes in our database: </p>");

// Request the text of all the jokes
$result = @mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}

// When clicked, this link will load this page
// with the joke submission form displayed.

echo("<p><a href=\"".$PHP_SELF."?addjoke=1\">Add a Joke!</a></p>");

endif;

?>

</body>
</html>

Posted: Tue Feb 11, 2003 6:44 pm
by volka
have read viewtopic.php?t=511 carefully?
If not: please do so, certainly it will answer your question

Posted: Wed Feb 12, 2003 12:05 pm
by daven
Aside from passing variables, your SQL statement is flawed (sorry, I did not catch this earlier).

What you have:

Code: Select all

$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
What it should be:

Code: Select all

$sql="INSERT INTO Jokes(JokeText,JokeDate) VALUES('".$joketext."', '".CURDATE()."')";
The syntax for an insert statement is INSERT INTO table(column1, column2, . . ., columnN) VALUES(value1, value2, . . . , valueN).
The syntax for an update statement is UPDATE table SET column1=value1, column2=value2, . . . , columnN=valueN WHERE condition.
You were mixing up the statements.

Posted: Wed Feb 12, 2003 1:00 pm
by volka
http://www.mysql.com/doc/en/INSERT.html
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name
SET col_name=(expression | DEFAULT), ...
[ ON DUPLICATE KEY UPDATE col_name=expression, ... ]

Posted: Wed Feb 12, 2003 2:40 pm
by daven
Ah. Forgot about that option. I have been skipping from MS-SQL to MySQL to Oracle recently, so I keep forgetting the various options.