Honesty Rocks! truth rules.

Newjoke.php [resolved] Mistake?

HOME      >>       Programming

alex1985

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


alex1985

Oh, I forgot to number the lines. The 68th line is at the end of the code which is: "<?php". Need feedback as soon as possible. Please!!!


Kubi

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


kobra500

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


rvalkass

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.

alex1985

Oh, thanks! I think, that will work fine, if I close the PHP code tag properly. I let you know, if-not!


flashy

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.


alex1985

Well, it does still give me the same message?!


alex1985

Guys, if do not know or do not want to do it because it does bother you, please reply something.


rvalkass

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.

alex1985

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.

rvalkass

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.


galexcd

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.


alex1985

So, anyone, please put it all together?! And if you can do it, please right the steps.


alex1985

The problem is resolved. Thank you for your cooperation!!!


rvalkass

Topic is resolved.Please PM any moderator to continue this discussion. Until then, this topic is closed.