Forum Moderators: coopster
This is the code I have right now:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing Writing to Database Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
// includes
include("config.php");
// connection to server and selection of database
$connect = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_errno().": ".mysql_error()."<br
/>Unable to connect to server.");
$db = mysql_select_db($db_name,$connect) or die (mysql_errno().": ".mysql_error()."<br />Unable to
select database.");
?>
<form action="newentry.php" method="post">
Title: <input type="text" name="entry_title" /><br /><br />
Entry: <br /><textarea rows="30" cols="60"></textarea><br /><br />
Category: <!-- HEREIN LIES THE PROBLEM --><br /><br />
Allow Comments? <input type="checkbox" name="entry_comments" /><br />
</form>
<?php
mysql_close($connect);
?>
</body>
</html>
Any brilliant ideas?
Thanks!
while($row = mysql_fetch_object($cat)){
echo "<option value=\"", $cat->ID,"\">", $cat->Name, "</option>";
Now, I understand while statements. I get that whole thing. But as far as the $cat->ID thing goes...I'm not sure exactly how that works. My main table is called "blog_entries", the column is called "category". The second table is called "blog_categories", and the columns are "category_id", "category", and "description". So what values do I put in that option statement to make the drop down?
On top of all this silliness, I'm not sure how to make the "category" column in the "blog_entries" table reference the "category" column in the "blog_categories" table. Does that make any sense? Do I even need to make them reference each other, if the drop-down box pulls the categories straight out of the "blog_categories" table? I'm such a n00b.
I really appreciate the help.
Anyway -
while($row = mysql_fetch_object($cat)) - This just gets the results from the query, and goes through them row by row.
$row->ID & $row->Name -- get the column value for that particular row
So in your case, that particular line would be:
echo "<option value=\"", $row->category_id,"\">", $row->category, "</option>";
In html code, this would like like:
<option value="1">Cool Stuff</option>
<option value="2">More stuff</option>
...
As far as linking the two together, what does your blog_entries table have for columns?
First of all, I haven't even started THINKING about the comments program (*is afraid, is very afraid*), but besides that, the rest of those elements are either auto-entered or should be on this form.
Okay, I think that makes sense (at least to me *lol*).
Thank you very very much for your help. I appreciate it.
Good luck :)
-sned
<?php// includes and requires
require('config.php');
// set the globals
$entry_title = $_POST[entry_title];
$entry_text = $_POST[entry_text];
$entry_category = $_POST[entry_category];
$entry_mood = $_POST[entry_mood];
$entry_music = $_POST[entry_music];
$entry_month = $_POST[entry_month];
$entry_date = $_POST[entry_date];
$entry_year = $_POST[entry_year];
$entry_timeofday = $_POST[entry_timeofday];
// set the pre-inserted timestamp parts
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
// process the form
if($submit) {
// convert the timestamp
$entry_time = strtotime("$entry_month $entry_date $entry_year $entry_timeofday");
// add line breaks
$entry_text = nl2br($entry_text);
// connect to the database host and select the database
mysql_connect($db_host,$db_user,$db_pass) or die("Unable to connect to the host.<br />" . mysql_errno() . ": " . mysql_error() . "<br /><br />\n");
mysql_select_db($db_name) or die("Unable to select the database.<br />" . mysql_errno() . ": " . mysql_error() . "<br /><br />\n");
// add values
$addentry = "INSERT INTO bloggart_entries (entry_time,entry_title,entry_text,entry_category,entry_mood,entry_music) VALUES ('$entry_time','$entry_title','$entry_text','$entry_category','$entry_mood','$entry_music')";
$result = mysql_query($addentry) or print ("Unable to add entry to the table 'bloggart_entries' in the database.<br />" . mysql_errno() . ": " . mysql_error() . "<br /><br />");
mysql_close();
}
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<p>Month:
<select name="entry_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>
Date: <input type="text" name="entry_date" size="2" value="<?php echo $current_date?>" />
Year: <input type="text" name="entry_year" size="4" value="<?php echo $current_year?>" />
Time: <input type="text" name="entry_timeofday" size="5" value="<?php echo $current_time?>" /><br /><br />
Title: <input type="text" name="entry_title" size="40" /><br /><br />
Entry:<br /><textarea cols="80" rows="20" name="entry_text"></textarea><br /><br />
<?php
$catQ = "SELECT * FROM bloggart_categories";
$cat = mysql_query($catQ);
?>
Category:
<select name="entry_category">
<?php
while($row = mysql_fetch_object($cat)){
echo "<option value=\"", $row->category_id,"\">", $row->category_title, "</option>";
}
?>
</select>
<br /><br />
Mood: <input type="text" name="entry_mood" size="40" /><br /><br />
Music: <input type="text" name="entry_music" size="40" /><br /><br />
<input type="submit" name="submit" value="Post" />
</form>
The new names for the bloggart_categories table (renamed, obviously) are category_id, category_title, and category_description...I have no idea what I did wrong...it worked before.