Page 1 of 1

how to make GUestbook

Posted: Mon Apr 19, 2004 2:45 pm
by kev83
now i am trying to prevent duplicate entries by checking if the e-mail already exists in the databse, i tried the following code but it still adds the entry even if the same e-mail already exists in the database.
<?php
if(isset($_POST["submit"]))
{
//This grabs the code from the form, I removed the
//addslashes because you don't really need them

// as for the addslashes you NEED them so people
// can't inject database queries into your code.
// You should always check your variables coming in
// from the user.
$name = $_POST['name'];
$location = $_POST['location'];
$email = $_POST['email'];
$url = $_POST['url'];
$comments = $_POST['comments'];
$query = "select * from guestbook where email = '$email'";
$result = mysql_query ($query);
$pattern = '^([._a-zA-Z0-9-]){2,255}@([._a-zA-Z0-9-]){2,255}\.([a-zA-Z]){2,3}$';
$urlpattern ="^http://[A-Za-z0-9\%\?\_\:\-\/\.-]+$";
if(!$name){
echo '<li>You have not entered your name.<br />'
.'Please try again.';
}
elseif (!ereg ($pattern,$email)) {
echo "<li>bad email, please try again. <br />";
}
elseif (mysql_num_rows($result)){
echo "<li>You have sign already!!<br />";
}
elseif (!ereg ($urlpattern,$url)){
echo "<li>bad url, please try again.<br />";
}
else{



//This connects to the mysql server and selects the databse
$con = mysql_connect("localhost", "root", "");
$db = "guest";
mysql_select_db($db, $con)or die("There was an error connecting to the database. Please send the following information to the site administrator.<br /><br />".mysql_error());

//This inserts the information in to the database
$sql = "insert into guestbook (name, location, email, url, comments) values ('$name', '$location', '$email', '$url', '$comments')";
mysql_query($sql)or die("There was an error adding your information to the database. This error occurred on page name \"sign.php\", line 68. Please send the following information to the site administrator.<br /><br />".mysql_error());

echo 'thank you for signing!';
exit;
//This closed the connection to the database
mysql_close($con);
}
} // end the if statment

?>


<html>
<head>
<title>Sign my Guest Book!!</title>
</head>

<body>
<h1>Sign my Guest Book!!</h1>
<form action="<?PHP print $_SERVER[PHP_SELF]?>" method="POST">

<table border="0">
<tr>
<td>name:</td>
<td><input type="text" name="name" maxlength="30" size="30"><br /></td>
</tr>
<tr>
<td>Location:</td>
<td> <input type="text" name="location" maxlength="30" size="30"><br /></td>
</tr>
<tr>
<td>Email:</td>
<td> <input type="text" name="email" maxlength="30" size="30"><br></td>
</tr>
<tr>
<td>url:</td>
<td><input type="text" name="url" maxlength="30" size="30"><br /></td>
</tr>
<tr>
<td>Comments:</td>
<td> <textarea name="comments" COLS=40 ROWS=3 scrollbar=yes></textarea><br></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Sign"><input type="reset" value="Start over!"></td>
</tr>
</table>
</form>
</body>
</html>

check box

Posted: Mon Apr 19, 2004 2:59 pm
by kev83
i really got no idea how to make check box????

checkboxes

Posted: Tue Apr 20, 2004 12:59 am
by Heehee
u have to create form 1st don't see any:
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
then create the checkbox button using this code:

<input type=\"checkbox\" name=\"delete[]\" value=\"$row[entry_id]\">

then u need to submit the checkbox values and close the form:
<input type="submit" name="submit" value="Delete"> <input type="reset" value="Start over!"></form>

hope this helps,cuz i'm quite new here :)