Jump to content
xisto Community
alex1985

Newjoke.php [resolved] Mistake?

Recommended Posts

Hi, do you know what is the mistake in the following PHP code:

It does give me the following message:

Parse error: syntax error, unexpected $end in /home/alex1985/public_html/mulhim/library_project/test_dir/newjoke.php on line 68

<?php$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');if (!$dbcnx) {exit('<p>Unable to connect to the ' .'database server at this time.</p>');}if (!@mysql_select_db('ijdb')) {exit('<p>Unable to locate the joke ' .'database at this time.</p>');}if (isset($_POST['joketext'])):// A new joke has been entered// using the form.$aid = $_POST['aid'];$joketext = $_POST['joketext'];if ($aid == '') {exit('<p>You must choose an author for this joke. Click ' .'"Back" and try again.</p>');}$sql = "INSERT INTO joke SETjoketext='$joketext',jokedate=CURDATE(),authorid='$aid'";if (@mysql_query($sql)) {echo '<p>New joke added</p>';} else {exit('<p>Error adding new joke: ' . mysql_error() . '</p>');}$jid = mysql_insert_id();if (isset($_POST['cats'])) {$cats = $_POST['cats'];} else {$cats = array();}$i = 0; // First indexwhile ($i < count($cats)) { // While we're not at the end// process $cats[$i]++$i; // Increment to the next index}$i = 0; // First indexwhile (isset($cats[$i])) { // While we're not at the end// process $cats[$i]++$i; // Increment to the next index}for ($i = 0; isset($cats[$i]); ++$i) {// process $cats[$i]}foreach ($cats as $catID) {// Process $catID}$numCats = 0;foreach ($cats as $catID) {$sql = "INSERT IGNORE INTO jokecategorySET jokeid=$jid, categoryid=$catID";$ok = @mysql_query($sql);if ($ok) {$numCats = $numCats + 1;} else {echo "<p>Error inserting joke into category $catID: " .mysql_error() . '</p>';}}?><p>Joke was added to <?php echo $numCats; ?> categories.</p><p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add anotherjoke</a></p><p><a href="jokes.php">Return to joke search</a></p><?php

Share this post


Link to post
Share on other sites

I believe the proper way to end that code would be ?>, maybe a second opinion would be in order.

Share this post


Link to post
Share on other sites

Yeah it's ?> don't worry I am new to PHP as well to be honest I only started learning it today lolincedently do i have to mysql_connect every time I want to connect to mysql or just @ the beggining?I hope the latter! yeah i c now sorry i was only looking at the last line so didn't notice the html

Edited by kobra500 (see edit history)

Share this post


Link to post
Share on other sites

The last line in your code seems to re-start a block of PHP with no real purpose. At that point, on the last line, you are in HTML mode - all PHP blocks have been closed. Unless there is some PHP code after that last line which you have not shown us, remove that last line and you should be OK.

incedently do i have to mysql_connect every time I want to connect to mysql or just @ the beggining?

You'd never want to connect to MySQL more than once. You should open the connection at the beginning of the file, run your various SQL commands, then close the connection at the end of the file.

Share this post


Link to post
Share on other sites

Lol - thing about PHP, most text editors, and all WhatYouSeeIsWhatYouGet web editors dont tell you whats wrong or what you are missing. So the only way to figure out whats going on is to look at the error message.

Share this post


Link to post
Share on other sites

Your problem could be this line:

if (isset($_POST['joketext'])):

If you look through your code, you have no endif; at all. To finish that if statement you will need endif; placed somewhere. This is why it complains the script ends early. It hits the end of the code and is still waiting for that endif; to appear.

Share this post


Link to post
Share on other sites

Help me out!!! I was trying to correct the mistake all the day, today. I can't still fix it. I'm using the tutorial in the textbook, like step-by-step guide how you make certain things. I'm putting the the experts from the textbook which makes the entire page. Probabli, there are some mistakes or I put them in the wrong order. So, I asking you to take a glance at it, and help me to create one. I will as well to make it tomorrow. Trying is one of the way of learning some certain aspects. Please, let me know as soon as possible. Thanks for you cooperation.

There are experts:

1)

<?phpelse: // Allow the user to enter a new joke$authors = @mysql_query('SELECT id, name FROM author');if (!$authors) {exit('<p>Unable to obtain author list from the database.</p>');}$cats = @mysql_query('SELECT id, name FROM category');if (!$cats) {exit('<p>Unable to obtain category list from the ' .'database.</p>');}?>

2)

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><p>Enter the new joke:<br /><textarea name="joketext" rows="5" cols="45"></textarea></p>

3)

<p>Author:<select name="aid" size="1"><option selected value="">Select One</option><option value="">---------</option><?phpwhile ($author = mysql_fetch_array($authors)) {$aid = $author['id'];$aname = htmlspecialchars($author['name']);echo "<option value='$aid'>$aname</option>\n";}?></select></p>

4)

<p>Place in categories:<br /><?phpwhile ($cat = mysql_fetch_array($cats)) {$cid = $cat['id'];$cname = htmlspecialchars($cat['name']);echo "<label><input type='checkbox' name='cats[]' " ."value='$cid' />$cname</label><br />\n";}?></p>

5)

<input type="submit" value="SUBMIT" /></form><?php endif; ?>

6)

<?php$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');if (!$dbcnx) {exit('<p>Unable to connect to the ' .'database server at this time.</p>');}if (!@mysql_select_db('ijdb')) {exit('<p>Unable to locate the joke ' .'database at this time.</p>');}if (isset($_POST['joketext'])):// A new joke has been entered// using the form.$aid = $_POST['aid'];$joketext = $_POST['joketext'];if ($aid == '') {exit('<p>You must choose an author for this joke. Click ' .'"Back" and try again.</p>');}$sql = "INSERT INTO joke SETjoketext='$joketext',jokedate=CURDATE(),authorid='$aid'";if (@mysql_query($sql)) {echo '<p>New joke added</p>';} else {exit('<p>Error adding new joke: ' . mysql_error() . '</p>');}$jid = mysql_insert_id();

7)

if (isset($_POST['cats'])) {$cats = $_POST['cats'];} else {$cats = array();}

8)

$i = 0; // First indexwhile ($i < count($cats)) { // While we're not at the end// process $cats[$i]++$i; // Increment to the next index}

9)

$i = 0; // First indexwhile (isset($cats[$i])) { // While we're not at the end// process $cats[$i]++$i; // Increment to the next index}

10)

for ($i = 0; isset($cats[$i]); ++$i) {// process $cats[$i]}

11)

foreach ($cats as $catID) {// Process $catID}

12)

$numCats = 0;foreach ($cats as $catID) {$sql = "INSERT IGNORE INTO jokecategorySET jokeid=$jid, categoryid=$catID";$ok = @mysql_query($sql);if ($ok) {$numCats = $numCats + 1;} else {echo "<p>Error inserting joke into category $catID: " .mysql_error() . '</p>';}}?><p>Joke was added to <?php echo $numCats; ?> categories.</p><p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add anotherjoke</a></p><p><a href="jokes.php">Return to joke search</a></p>

Finished. That the experts are given from the textbook. When I type all of it, it does not work on the server?! So, I think there is a mistake or I'm puttinng in the wrong way in the PHP file. Thanks.
Edited by alex1985 (see edit history)

Share this post


Link to post
Share on other sites

Well, the first problem with the code you have put in your last post is line 2. The code is started with an else: statement, yet there is no if statement before it. For an else statement to get called, an if statement directly before it must have returned false. As you have no if statement before the else statement, it makes no sense.Double check the book and make sure you haven't missed a section of code before part 1.

Share this post


Link to post
Share on other sites

Most of those excerpts (numbers 8 through 10) do the same thing just written in different ways, and they are not completed. It looks like they want you to replace the comment "// process $cats[$i]" with something.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.