Jump to content

Guestbook doesn't work


Recommended Posts

Hello,

 

I'm new too the PHP language, we learn it at our school. We're working on a guestbook, but it has to be as they showed us. We have strict rules to make it, and that's where i get stuck. We have to fill in the blanks, i guess you could say. And I don't get it working, and since the teacher is sick this week and we have holiday next week, I won't see my teacher to ask him, so instead I ask it here.

 

(I'm sorry if my English is not too good, by the way :))

 

Here you have the code:

(My databases connection is working)

 

----------------------index.php---------------------

<?php

include ('connection.php');

if($_SERVER['REQUEST_METHOD'] == 'POST')

{

include ('verwerkbericht.php');

}

?>

<html><head></head><body>

<?php

$foutmelding = mysql_error();

if(!empty($foutmelding))

echo '<p>' . $foutmelding . '</p>';

include ('toonberichten.php');

?>

<form action="index.php" method="POST">

<h3>Nieuw bericht toevoegen</h3>

<p>Naam:<br /><input type="text" name="naam" /></p>

<p>Bericht:<br /><textarea name="bericht"></textarea></p>

<input type="submit" name="verzendknop" value="verzenden"/>

</form>

</body></html>

 

------------toonberichten.php---------------(in English: showmessages.php)

<?php

$query = "SELECT naam, bericht, datum FROM gastenboek ORDER BY datum DESC";

$result = mysql_query($query);

echo '<table>';

while($row = mysql_fetch_array($result))

{

  $naam=$row['naam'];

  echo "de naam: $naam";

}

echo '</table>';

?>

 

----------------verwerkbericht.php--------------------(in English: something like addmessage.php

<?php

$foutmelding = '';

// $naam = (isset($_POST['naam']))'';

// $bericht = (isset($_POST['bericht']));

// $email = (isset($_POST['email']))

if(empty($naam))

$foutmelding = 'Vul een naam in.';

if(empty($bericht))

$foutmelding = 'Vul een bericht in.';

if (empty($error))

{

$datum = date('Y-m-d');

$query = 'INSERT INTO gastboek (naam, bericht, datum) VALUES (\'' . $naam . '\'. \'' . $bericht . '\'. \''  . $datum . '\');';

return $query;

?>

 

If you need the example we have been given, tell me, but i don't think it's usefull to you because the explanation is dutch..

 

What am i doing wrong? The error i get is this:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\wamp\www\wp22\gastenboek\toonberichten.php on line 8

 

(it's about this part in toonberichten.php: while($row = mysql_fetch_array($result))

{

  $naam=$row['naam'];

  echo "de naam: $naam";

}

)

 

I hope you guys can help me. :)

Link to comment
Share on other sites

The error would suggest that there's a problem with your query. Try adding some debugging and see what the problem is. Change this:

 

$result = mysql_query($query);

 

To

$result = mysql_query($query) or trigger_error(mysql_error(),E_USER_ERROR);

 

p.s. In future posts, try to use


tags around your code. And welcome to the forums :)

Link to comment
Share on other sites

Wie geht's?

 

the query in toonberichten.php...

 

SELECT naam, bericht, datum FROM gastenboek ORDER BY datum DESC

 

run that query in your db admin (phpmyadmin???) and see what results are delivered...

 

if you don't need the enumerated array of results just use mysql_fetch_assoc()

 

let us know how you get on.

 

viel gluck

Link to comment
Share on other sites

@GingerRobot: Thank you :) He now shows the messages i put into the database, the only problem i now have is with adding messages, this is the error:

Notice: Undefined variable: naam in C:\Program Files\wamp\www\wp22\gastenboek\verwerkbericht.php on line 19

 

Notice: Undefined variable: bericht in C:\Program Files\wamp\www\wp22\gastenboek\verwerkbericht.php on line 19

 

It is about this part:

query = 'INSERT INTO gastboek (naam, bericht, datum) VALUES (\'' . $naam . '\'. \'' . $bericht . '\'. \''  . $datum . '\');';

 

@Toonmariner

I did what you said, and i saw my mistake: gastenboek is the wrong name, it's gastboek. Such a stupid mistake haha :P

Link to comment
Share on other sites

@GingerRobot: Thank you :) He now shows the messages i put into the database, the only problem i now have is with adding messages, this is the error:

Notice: Undefined variable: naam in C:\Program Files\wamp\www\wp22\gastenboek\verwerkbericht.php on line 19

 

Notice: Undefined variable: bericht in C:\Program Files\wamp\www\wp22\gastenboek\verwerkbericht.php on line 19

 

It is about this part:

query = 'INSERT INTO gastboek (naam, bericht, datum) VALUES (\'' . $naam . '\'. \'' . $bericht . '\'. \''  . $datum . '\');';

 

That's because the varaibles are , as it says, undefined. If you look at where you 'defined' them:

 

// $naam = (isset($_POST['naam']))'';

 

You've commented out the line, plus the syntax is out. I assume you were after something like:

 

$naam = (isset($_POST['naam'])) ? $_POST['naam'] : '';

 

You should also note that you're error checking is out. You see you're checking if $error is empty, but you never actually define that variable either.

Link to comment
Share on other sites

Thanks, he doens't show that error anymore, but the problem now is that he doesn't add the messages into the database.. why is that?

<?php

$foutmelding = '';
$naam = (isset($_POST['naam'])) ? $_POST['naam'] : '';
$bericht = (isset($_POST['bericht'])) ? $_POST['bericht'] : '';

if(empty($naam))
$foutmelding = 'Vul een naam in.';

if(empty($bericht))
$foutmelding = 'Vul een bericht in.';

if (empty($error))
{
$datum = date('Y-m-d');

$query = "INSERT INTO gastboek (naam, bericht, datum) VALUES ('" . $naam . "', '".$bericht."', '" . $datum . "');";

return $query;
}
?>

 

That's my code now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.