Help - Search - Members - Calendar
Full Version: [resolved] Building a Blog Problem
Codegrrl.com Forums > Script Help > Tutorial Help
Skaterstu
Hi,

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>
Amelie
Change this line:

CODE
   $password = htmlspecialchars(strip_tags($_POST['password']));


To this:

CODE
if (isset($_POST['password'])) $password = htmlspecialchars(strip_tags($_POST['password']));


That should fix it.
Skaterstu
QUOTE(Amelie @ Jan 30 2007, 07:07 PM) *

Change this line:

CODE
   $password = htmlspecialchars(strip_tags($_POST['password']));


To this:

CODE
if (isset($_POST['password'])) $password = htmlspecialchars(strip_tags($_POST['password']));


That should fix it.


Hi Amelie,

Thanks for you reply... tried this but it still comes back with the same error message. Any other idea?

Cheers

Stuart
Amelie
Hmmm... Try changing that same line to:

CODE
if (isset($_POST['password'])) {
    if ($_POST['password'] == "1") $password = $_POST['password'];
    else $password = 0;
}

Skaterstu
QUOTE(Amelie @ Jan 30 2007, 11:00 PM) *

Hmmm... Try changing that same line to:

CODE
if (isset($_POST['password'])) {
    if ($_POST['password'] == "1") $password = $_POST['password'];
    else $password = 0;
}



Thanks again Amelie,

I have made changes, but a similar error appears with the INSERT query. It really doesn't like 'password.'

Notice: Undefined variable: password in C:\htdocs\WebgorillaPHP\blog_Entry.php on line 61
Error: Could not process query because: Incorrect integer value: '' for column 'password' at row 1


I created the table in phpmyadmin, I didn't create it using php to alter the table. One thing I have noticed is that in phpmyadmin, password is being displayed as the following:


Field: password
Type: tinyint(4)
Null: No
Default: 0


Would the (4) assigned to tinyint be causing problems here do you think?

I greatly appreciate your help. Many Thanks.

Stuart
Amelie
Try changing that field to a text or varchar...
Skaterstu
QUOTE(Amelie @ Jan 31 2007, 07:30 AM) *

Try changing that field to a text or varchar...


wow, that was quick... one minute



QUOTE(Amelie @ Jan 31 2007, 07:30 AM) *

Try changing that field to a text or varchar...


Hi Amelie,

I have changed to Varchar(1), kept default 0.

The entry is now being written to the database. But am still getting the following error.

Notice: Undefined variable: password in C:\htdocs\WebgorillaPHP\blog_Entry.php on line 63

This is the below query that seems to be kicking up the problem:

$query = "INSERT INTO blog (timestamp, title, entry, password) VALUES ('$timestamp', '$title', '$entry', '$password')";

I have noticed in phpmyadmin that even though it writes the record to the database, the password field is not displaying '0', whereas password protected entries display '1.' It is very strange because before I password protected the pages, I had no problems but after password protecting I kept getting errors.

Also, I have now noticed that my passworded blog entries are not displaying correctly. If I input their password, the last blog entry (that was not password protected) is displayed in the single display page. I guess this will probably clear up once the above error is sorted.

Any other ideas??

Thanks again.

Stuart
Skaterstu
QUOTE(Amelie @ Jan 30 2007, 11:00 PM) *

Hmmm... Try changing that same line to:

CODE
if (isset($_POST['password'])) {
    if ($_POST['password'] == "1") $password = $_POST['password'];
    else $password = 0;
}



I changed the code to as below and its working again:
CODE

if (isset($_POST['password'])) {
if ($_POST['password'] == "1"){

$password = $_POST['password'];

}
} else {
$password = 0;
}


Thanks for your help, Amelie. I still have the problem with my username and password displaying a single unpassworded entry, but I will have a look at this myself and see if I can resolve it on my own before calling on your expertise.

Thanks

Stuart
Amelie
I think that is the default function of the passworded entries - they only display one entry at a time (unlike, say, WordPress, which will show all entries protected with the same password). It's only a very basic password protection system, so I'm sure it can be improved. smile.gif

Edit: Sorry, I just re-read your problem. I'm not sure why that's happening, can you post your updated code for your single entry page?
Skaterstu
QUOTE(Amelie @ Feb 1 2007, 09:18 PM) *

I think that is the default function of the passworded entries - they only display one entry at a time (unlike, say, WordPress, which will show all entries protected with the same password). It's only a very basic password protection system, so I'm sure it can be improved. smile.gif

Edit: Sorry, I just re-read your problem. I'm not sure why that's happening, can you post your updated code for your single entry page?


Hi Amelie,

This has been resolved... I had some error in my sql query on the single entry page.

Thanks

Stuart
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.