I Am trying to learn PHP and trying to make a very simple guestbook but it seems I am doing something wrong and I can't figure out what. The problem is that it isn't inserting the entries into the DB and I get an error when trying to display entries a Manually entered in phpmyadmin. I would really appriciat if someone could take a quick look at the code and se if they se what I have done wrong. I am using the following code for the file. <html> <head> <title>Guestbook</title> </head> <body> <h1>Make a entry </h1> <form action="guestentry.php" method="POST"> <p>Name: <br /> <input type="text" name="fornamn" size="30" /></p> <p>Email: <br /> <input type="text" name="email" size="30" /></p> <p>Text: <br /> <textarea name="entry" cols="100" rows="5" wrap="VIRTUAL"></textarea> </p> <p>refered by: <br /> <input type="text" name="ref" size="30" /></p> <p><input type="reset" value="Clear!" /> <input type="submit" value="Send!" /></p> </form> <h1>Guestbook entires</h1> <?php $link = mysqli_connect ('localhost','root','','guest'); if (!$link) { echo mysql_connect_error($link); } $query = mysqli_query($link, "SELECT * FROM guest LIMIT 10"); $guest = mysqli_fetch_assoc($query); while ($row = mysqli_fetch_assoc($query)) { echo '<p>Name:<br />'; echo $row['user_name'] . '</p>'; echo '<p>Email:<br />'; echo $row['user_email'] . '</p>'; echo '<p>text:<br />'; echo $row['entry_text'] . '</p>'; echo '<p>refered by:<br />'; echo $row['ref'] . '</p>'; } mysqli_close($link); ?> </body> </html> Code (markup): and the following code in "guestentry.php" to insert entries into the Mysql DB <?php $link = mysqli_connect ('localhost','root','','guest'); if (!$link) { echo mysqli_connect_error($link); } $name = $_POST['fornamn']; $email = $_POST['email']; $text = $_POST['entry']; $ref = $_POST['ref']; $sql = "INSERT INTO guest (user_name, user_email, entry_text, user_ref) VALUES ('$name, $email, $text, $ref')"; mysqli_query($link, $sql); mysqli_close($link); echo 'Thanks for posting'; ?> Code (markup):
why are you using "mysqli_connect"? Also what is the error? On all of your insert scripts change them to )or die(mysql_error()); and they will spit out an error if they dont insert. I use this when debugging scripts and it works well. I later wrap them in if statements though. Danny
I am using "mysqli_connect" because my book teaches me to do it that way. The insert data procedure don't provide me with any error. I get Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\MKFC\index.php on line 36 and same on line 38 when I try to display data. How do you mean change the insert scripts to )or die(mysql_error()); ???
OK. I have tried that. It didn't provide any error but prevented the echo 'Thanks for posting'; text to be shown when i made an entry. Thanks for helping me figur this out.
that is odd If it isnt erroring it should be going into the database. Try mysql_connect instead of mysqli_connect
mysqli_connect As far as I can tell, mysqli_connect is used just for gathering information about the database/connection. Use mysql_connect() instead. Change: $link = mysqli_connect ('localhost','root','','guest'); to $link = mysql_connect ('localhost','root','','guest');
mysqli is the 'improved' version of the mysql library. From http://dev.mysql.com/downloads/connector/php/ : The main features of the mysqli extension are: * access to all MySQL 4.1/5.0 features * a procedural interface that is similar to the mysql extensions * an object-oriented interface that is easier to extend than the procedural interface mysqli is required for connecting to MySQL 4.1 and above.