SitePoint Sponsor |
|
User Tag List
Results 1 to 15 of 15
-
Aug 11, 2005, 09:24 #1
Why is the if <condition> being ignored?
Hi Folks,
I've been tweaking this bit of code to display and add (insert) records.
The page is first displayed with a list of records and an option to add records is supplied to reload the same url page with a FORM. when text is supplied (a new joke in this case) the page is recalled and should display 'your joke has been added' and the subsequent list of jokes. However and without any notices or errors the if condition is ignored, yet the condition seems to be correct. I've tried differnet variables with or without quotes etc. but to no avail. baffling. here's the code;
<HTML>
...
<BODY>
<?php
// If the user wants to add a joke
if (isset($_GET['addjoke'])):
?>
<FORM ACTION="<?php echo($_SERVER['PHP_SELF']); ?>" METHOD=GET>
<P>Type your joke hereBR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE="SUBMIT" NAME="submitjoke" VALUE="SUBMIT">
</FORM>
<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost",
"root", "Quink63");
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 ("SUBMIT" == 'submitjoke') {
$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="' . $_SERVER['PHP_SELF'] . '?addjoke=1">Add a Joke!</a></p>');
endif;
?>
</BODY>
</HTML>
just for interest. How can I post using scrollable text boxes? this seems to take up an awfull lot of space
thankyou
Bruce
-
Aug 11, 2005, 09:31 #2
- Join Date
- Sep 2004
- Location
- Singapore
- Posts
- 405
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The problem lies here:
PHP Code:if ("SUBMIT" == 'submitjoke') {
$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>");
}
}
Change that into
PHP Code:if ($_GET['SUBMIT'] == 'submitjoke') {
And to use scrollable textboxes,
[PHP.]
code
[/PHP.]
or
[CODE.]
code
[/CODE.]
Use those tages without the .
Hope that helps.
-
Aug 11, 2005, 09:36 #3PHP Code:
<HTML>
...
<BODY>
<?php
// If the user wants to add a joke
if (isset($_GET['addjoke'])) {
?>
<FORM ACTION="<?php echo($_SERVER['PHP_SELF']); ?>" METHOD=GET>
<P>Type your joke here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE="SUBMIT" NAME="submitjoke" VALUE="SUBMIT">
</FORM>
<?php
} else {
// Connect to the database server
$dbcnx = @mysql_connect("localhost",
"root", "Quink63");
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 ("SUBMIT" == 'submitjoke') {
$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>");
}
} // i dont know what this brace is ending??
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="' . $_SERVER['PHP_SELF'] . '?addjoke=1">Add a Joke!</a></p>');
}
?>
</BODY>
</HTML>
use braces.. not colons.. i'm not sure if colons are valid syntax, i believe if you use them you have to say endif; or soemthing like that.. just use braces
Write your if, elsif, else statements using the same syntax everytime.. it looks like you kind of just copied and pasted different scripts from teh web into one page.. If you are going to use braces, use braces. If you prefer the other syntax, just use that.. Doing both makes your code very confusing.
-
Aug 11, 2005, 10:12 #4PHP Code:
[QUOTE=killerkooki]The problem lies here:
[PHP]
if ("SUBMIT" == 'submitjoke') {
$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>");
}
}
Change that into
PHP Code:if ($_GET['SUBMIT'] == 'submitjoke') {
And to use scrollable textboxes,
[PHP.]
code
[/PHP.]
or
[CODE.]
code
[/CODE.]
Use those tages without the .
Hope that helps.[/QUOTE][/PHP]
PHP Code:nope I tried that to no avail. What I don't understand is that when the page reloads the url it says 'adjoke2.php?joketext=test&submitjoke=SUBMIT so the condition seems to be correct. ugh!
-
Aug 11, 2005, 10:23 #5
- Join Date
- Sep 2004
- Location
- Singapore
- Posts
- 405
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Argh! Sorry, I got the thing mixed up.
PHP Code:if ($_GET['submitjoke'] == 'SUBMIT') {
-
Aug 11, 2005, 10:25 #6PHP Code:
Hi Mwolfe
that seemingly lost ending curly bracket. I deleted it but then I got a parsing erro for line 70. As you say I should keep the coding uniform, and will try. Do you have any comments as to why the if condition is being ignored. the reloaded page url says that "SUBMIT" is equal to 'submitjoke' so why doesn't it proceed?
Bruce
-
Aug 11, 2005, 10:33 #7
Originally Posted by killerkooki
Notice: Undefined index: submitjoke in C:\Server\Apache2\htdocs\addjoke2.php on line 37
-
Aug 11, 2005, 10:44 #8
- Join Date
- Sep 2004
- Location
- Singapore
- Posts
- 405
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
But does the rest of it work properly, despite that message?
It's only a Notice message and not a Warning message. Either way, I suggest you change your code to the following:
PHP Code:<HTML>
...
<BODY>
<?php
// If the user wants to add a 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">
</FORM>
<?php
}
else {
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "root", "Quink63");
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 ($_POST['submitjoke' == 'SUBMIT') {
$joketext = $_POST['joketext'];
$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="' . $_SERVER['PHP_SELF'] . '?addjoke=1">Add a Joke!</a></p>');
}
?>
</BODY>
</HTML>
-
Aug 11, 2005, 11:42 #9
Originally Posted by BruceX
LOL
we got a jokester here.. its no wonder your making a joke website.
-
Aug 12, 2005, 00:52 #10
Thanks wolfe. very constructive reply and I don't mind being humiliated by experts like yourself. Although it would kindly of you to treat my posts with less cynicism. I know you probably get impatient and frustrated at the level of ignorance shown by us newbies. But with firm guidance from you I'm sure we will be able to progress.
thankyou
Bruce
-
Aug 12, 2005, 00:59 #11
Thanks killercooki
YES! works fine thankyou.I'll exame the logic of the code and try and learn something.
Thanks again
Bruce
-
Aug 12, 2005, 08:32 #12
I'm sorry about that.. Now that i read my post again it did sound a bit harsh (the first one).. The other one i was just playing around.
But don't take it personally, I was just being an a-hole. Happens every so often.
Oh and i'm not an expert... i ask plenty of questions on here the experts would probably consider stupid.
-
Aug 12, 2005, 08:49 #13
- Join Date
- Aug 2005
- Location
- South Florida
- Posts
- 9
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
adding more variables to this script - how to make it work?
I'm in the same boat as Bruce, newbie trying to learn and absorb. Started my own thread earlier but have gotten very few responses. I am also trying to use K. Yank's book to make my own db/php thang. I modified his basic script because I have more variables than his example. Well, I can get it to work halfway, but when I input new info into the form, nothing happens, I just get the default display again. Nothing gets posted to the db. I can't make heads or tails of the previous explanations as applied to my particular situation with the many variables. Could you tell me the following:
What is isset and am i using it correctly?
Can I use something more simple to post these variables to the db than isset?
Please use multiple variable examples as I am just learning PHP syntax and don't really know how it works yet. Yo no hablo PHP!
Many many thanks. Here's the script I am working with:
PHP Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Used Trawlers For Sale</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php if (isset($_GET['addboat'])): // If the user wants to add a vessel
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<label>Add a new vessel to the database:<br /></label>
<td>Description</td>
<td><textarea name="description" cols="50" rows="10" id="description"></textarea></td>
</tr>
<tr>
<td>Length</td>
<td><input name="length" type="text" id="length"></td>
</tr>
<tr>
<td>Manufacturer</td>
<td><input name="manufacturer" type="text" id="manufacturer"></td>
</tr>
<tr>
<td>Year</td>
<td><input name="year" type="text" id="year"></td>
</tr>
<tr>
<td>Price</td>
<td><input name="price" type="text" id="price"></td>
</tr>
<tr>
<td>Engine</td>
<td><input name="Engine" type="text" id="Engine"></td>
</tr>
<tr>
<td>Location</td>
<td><input name="location" type="text" id="location"></td>
</tr>
<tr>
<td>Material</td>
<td><input name="material" type="text" id="material"></td>
</tr>
<tr>
<td>Draft</td>
<td><input name="draft" type="text" id="draft"></td>
</tr>
<tr>
<td>Beam</td>
<td><input name="beam" type="text" id="beam"></td>
</tr>
<tr>
<td>Photo</td>
<td><input name="photo" type="file" id="photo"></td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="Submit" id="submit" value="Add Vessel"></td>
</tr>
</table></form>
<?php else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'lordjim', 'williams');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the vessels database
if (!@mysql_select_db('classic-yacht-charters-miami_c_-_boats4sale')) {
exit('<p>Unable to locate the vessel ' .
'database at this time.</p>');
}
// If a vessel has been submitted,
// add it to the database.
if (isset($_POST['description'])&& isset($_POST['length'])&& isset($_POST['manufacturer'])&& isset($_POST['year'])&& isset($_POST['price'])&& isset($_POST['Engine'])&& isset($_POST['location'])&& isset($_POST['material'])&& isset($_POST['draft'])&& isset($_POST['beam'])&& isset($_POST['photo'])) {
$description = $_POST['description'];
$length = $_POST['length'];
$manufacturer = $_POST['manufacturer'];
$year = $_POST['year'];
$price = $_POST['price'];
$Engine = $_POST['Engine'];
$location = $_POST['location'];
$material = $_POST['material'];
$draft = $_POST['draft'];
$beam = $_POST['beam'];
$photo = $_POST['photo'];
$sql = "INSERT INTO listing SET
description='$description',
length='$length',
manufacturer='$manufacturer',
year='$year',
price='$price',
Engine='$Engine',
location='$location',
material='$material',
draft='$draft',
beam='$beam',
photo='$photo',";
if (@mysql_query($sql)) {
echo '<p>Your vessel has been added.</p>';
} else {
echo '<p>Error adding submitted vessel: ' .
mysql_error() . '</p>';
}
}
echo '<p>Here are all the vessels in our database:</p>';
// Request the text of all the vessels
$result = @mysql_query('SELECT description FROM listing');
if (!$result) {
exit('<p>Error performing query: ' .
mysql_error() . '</p>');
}
// Display the text of each vessel in a paragraph
while ($row = mysql_fetch_array($result)) {
echo '<p>' . $row['description'] . '</p>';
}
// When clicked, this link will load this page
// with the vessel submission form displayed.
echo '<p><a href="' . $_SERVER['PHP_SELF'] .
'?addboat=1">Add Another Boat!</a></p>';
endif;
?>
</body>
</html>
http://www.classic-yacht-charters-miami.com/boats.php
Anxiously, MaggiLu
-
Aug 12, 2005, 10:34 #14
sailmiami.. I'm not trying to be mean, but seriously, its not polite to double post, nonetheless post your problem in someone elses thread. There is no need to advertise your thread. I think you need to continue on through kevin yanks book and/or look for more information out there. There are lots of good articles explaining how to do things with php.
Another good method i'd say is if you want to learn by doing, do 1 thing a time basically, when you get that working, move on. If you can't get that simple thing you were doing working, then ask whats wrong. It will make it faster for us and for you, because we will only have to fix that one or two problems for you, plus you'll see exactly what it was that needed to be changed.
Now, this should go back to your other thread, where people can help you out with it there.
-
Aug 13, 2005, 07:40 #15
Originally Posted by mwolfe
Bruce
Bookmarks