First I would like to say a huge thank you for such a great bunch of tutorials. I have wanted to build my own blog for a while but being a newbie I have found it a little daunting, and confusing to know where to start. These tutorials are really informative and helpful to my development.
I have a problem, however. I have got onto the Password Protect Your Entry section. Things seem to be working fine, but now I am having problems creating an entry that is not password protected. If I create an entry and do not select the Password Protect check box then the following error shoots up and the entry doesn't get saved to the database:
Notice: Undefined index: password in C:\htdocs\WebgorillaPHP\blog_Entry.php on line 21
Error: Could not process query because: Incorrect integer value: '' for column 'password' at row 1
I have password set as default '0' in MySQL and given the value of '1' in the input form. I'd greatly appreciate if you would let me know what I am doing wrong.
Many Thanks
Stuart
p.s. code is attached below
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
// if the submit button has been set
if(isset($_POST['submit'])){
// *** Get the input data and assign to variables. ***
// below code uses htmlspecialchars() to ensure any input into the blog is
// free of nasty code. The strip_tags() is also used to remove any html or php
// elements from a string, leaving only text entities.
$month = htmlspecialchars(strip_tags($_POST['month']));
$date = htmlspecialchars(strip_tags($_POST['date']));
$year = htmlspecialchars(strip_tags($_POST['year']));
$time = htmlspecialchars(strip_tags($_POST['time']));
$title = htmlspecialchars(strip_tags($_POST['title']));
$entry = $_POST['entry'];
$password = htmlspecialchars(strip_tags($_POST['password']));
// timestamps are more flexible forms of dates to work with. Strtotime() means
// 'string to timestamp.' It takes a regular date and turns it into a timestamp.
$timestamp = strtotime($month." ".$date." ".$year." ".$time);
// nl2br() adds line breaks. Here we will add line breaks into the $entry string.
$entry = nl2br($entry);
// Below detects whether the server will automatically escape quotes that we submit
// to the database. If it does then we won't do anything. If it doesn't then we
// must use addslashes() because it will cause an error when adding our blog entry to
// the database.
if(!get_magic_quotes_gpc()){
$title = addslashes($title);
$entry = addslashes($entry);
}
// assign connection data to variables
// Assign connection data to variables
$host = "localhost";
$user = "*****";
$pass = "*****";
$db = "*****";
// Connect to MySQL
$connection = mysql_connect($host, $user, $pass) or die('Error: Could not connect you to MySQL');
// Connect to database
mysql_select_db($db) or die('Error: Could not connect you to the database');
// create a query to insert the entry into the database.
$query = "INSERT INTO blog (timestamp, title, entry, password) values ('$timestamp', '$title', '$entry', '$password')";
// put the query through MySQL_Query()
$query_rs = mysql_query($query) or die('Error: Could not process query because: '.mysql_error());
// check to ensure that the query has been input into the database
if($query_rs != false){
echo "Your entry has been successfully written to the database.";
}
// close MySQL
mysql_close();
}
?>
<?php
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="month" id="month">
<option value="<?php echo $current_month; ?>"><?php echo $current_month; ?></option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<input type="text" name="date" id="date" size="2" value="<?php echo $current_date; ?>" />
<select name="year" id="year">
<option value="<?php echo $current_year; ?>"><? echo $current_year; ?></option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
</select>
<input type="text" name="time" id="time" size="5" value="<?php echo $current_time; ?>" />
<p><label for="title">Title: <input type="text" name="title" id="title" size="40" /></label>
<label for="password"> Password Protect?<input type="checkbox" name="password" id="password" value="1" /></label></p>
<p><label for="entry">Entry: <textarea cols="80" rows="20" name="entry" id="entry"></textarea></label></p>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>
