lettheflamesbegin Posted May 25, 2009 Share Posted May 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/ Share on other sites More sharing options...
GingerRobot Posted May 25, 2009 Share Posted May 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-841762 Share on other sites More sharing options...
ToonMariner Posted May 25, 2009 Share Posted May 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-841769 Share on other sites More sharing options...
lettheflamesbegin Posted May 25, 2009 Author Share Posted May 25, 2009 @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 Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-841772 Share on other sites More sharing options...
Dathremar Posted May 25, 2009 Share Posted May 25, 2009 query = "INSERT INTO gastboek (naam, bericht, datum) VALUES ('" . $naam . "', '".$bericht."', '" . $datum . "');"; Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-841785 Share on other sites More sharing options...
GingerRobot Posted May 25, 2009 Share Posted May 25, 2009 @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. Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-841918 Share on other sites More sharing options...
lettheflamesbegin Posted May 26, 2009 Author Share Posted May 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-842367 Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 You never ran the query. Use mysql_query on $query. Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-842373 Share on other sites More sharing options...
lettheflamesbegin Posted May 26, 2009 Author Share Posted May 26, 2009 Oh, how stupid. But it didn't stand in our template.. so i asumed that that couldn't be wrong because i thought my teacher wasn't that stupid.. how wrong could i be haha Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/159592-guestbook-doesnt-work/#findComment-842477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.