Hi everyone, I'm trying to create a blog for myself and I'm having trouble right of the bat. I'm a beginner by the way. I'm trying to code a create new category page and check what information is put in by the user. It doesn't work somehow. Here're my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Welcome to my blog</title>
<link href="css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
require("config.php");
?>
<div id="main">
<h1>Hympert's blog</h1>
<div id="left">
<div id="categories">
<h3>Categories:</h3>
<select>
<option selected="selected"></option>
<?php
//dropdown menu to select a category
$categories = mysql_query("SELECT * FROM categories ORDER BY name ASC");
while($category = mysql_fetch_array($categories)){
echo "<option>" . $category['name'] . "</option>";
}
?>
</select>
<br /><br />
<form method="get">
<input name="new_cat" type="submit" value="Create new category" /><br /><br />
</form>
<?php
$submit = $_GET['new_cat'];
if(isset($submit)){
?>
<!-- create category form -->
<form method="get" action="">
Category name:<input name="cat_name" type="text" value="" /><br /><br />
<input name="submit" type="submit" value="Create" /><br />
</form>
<?php
}
$cat_name = $_GET['cat_name'];
//check category name to see if it is valid or not
//check the length, existed, blank of input category's nname
switch ($cat_name){
case trim($cat_name) == "":
echo "Please input a category name.";
break;
case strlen($cat_name) > 50:
echo "The length of a category name cannot exceed 50 characters.";
break;
case $r = mysql_query("SELECT name FROM categories"):
while($result = mysql_fetch_array($r)){
$cat_name = $result;
}
echo "Category name is already exist.";
break;
default:
mysql_query("INSERT INTO categories (name) VALUES ({$cat_name})");
}
function create_cat_form(){
echo "<form method=\"get\">
Category name:<input name=\"cat_name\" type=\"text\"/><br /><br />
<input name=\"submit\" type=\"submit\" value=\"Create\" /><br />
</form>";
}
?>
</div><!-- end div #categories -->
<div id="posts">
Posts
</div><!-- end div #posts -->
</div><!-- end div #left -->
<div id="contents">
Recently posted
</div><!-- end div #contents -->
</div><!-- end div #main -->
</body>
</html>
<?php
//database configuartion
$db_host = "localhost";
$db_user = "root";
$db_pass = "hoanghiepp";
$db_name = "blog";
//database connection
$connect = mysql_connect($db_host, $db_user, $db_pass) or die("Cannot connect to the server");
$select_db = mysql_select_db($db_name) or die("Cannot connect to the database");
?>
The CSS page
/*reset the window format */
* {margin: 0; padding: 0;}
body {
background-color: azure;
}
h1 {
text-align: center;
margin-top: 50px;
padding: 50px;
font-size: 50px;
color: violet;
}
/* style for the main div */
#main {
width: 800px;
height: 700px;
background-color: aqua;
margin: auto;
}
/* style for left div */
#left {
width: 250px;
height: auto;
float: left;
margin-right: 50px;
background-color: yellow;
}
/* style for categories div */
#categories {
width: 250px;
height: auto;
margin-bottom: 50px;
}
/* style for posts div */
#posts {
width: 250px;
height: auto;
background-color: antiquewhite;
margin-top: 50px;
}
/* style for contents div */
#contents {
width: 500px;
height: auto;
background-color: beige;
float: right;
}
a {
text-decoration: none;
}
Hope anyone can tell me the problem with the switch statement. I use if statement before and it works perfectly fine, but the code seems too long and repeated. Thank you very much for helping me out.
config.php
index.php
style.css