<?php
ConnectToDb($server, $username, $password, $database);
@$sql = mysql_query("SELECT COUNT(c_id) FROM categories") or die (mysql_error());
if(@mysql_num_rows($sql) > 0)
{
$row = mysql_fetch_array($sql);
$count = $row['COUNT(c_id)'];
if (isset($_POST['order']))
{
//Add data to the database.
$order = mysql_real_escape_string($_POST['order']);
$name = mysql_real_escape_string($_POST['name']);
$image = mysql_real_escape_string($_POST['image']);
if (empty($order) || empty($name) || empty($image)) {
echo"Missing field data. Go <a href=\"javascript:history.go(-1)\">back</a>.";
}
else
{
if ($order < $count)
{
// Got to change all the orders
while ($count >= $order)
{
$neworder = $count + 1;
mysql_query("UPDATE `categories` SET `c_order` = '$neworder' WHERE `c_order` = $count");
$count = $count - 1;
}
mysql_query("INSERT INTO categories VALUES (NULL ,'$order','$name','$image')");
echo"Category <b>$name</b> added. You can now <a href=\"add_product.php\">add</a> products to this category, or <a href=\"add_category.php\">create</a> another category.";
}
else
{
// Nothing special here, just add it on the end.
mysql_query("INSERT INTO categories VALUES (NULL ,'$order','$name','$image')");
echo"Category <b>$name</b> added. You can now <a href=\"add_products.php\">add</a> products to this category.";
}
}
}
else
{
$order = $count + 1;
?> <form action="add_category.php?add" method="post">
Order:<br/>
<select name="order">
<?
$i=1;
while ($i <= $count)
{
echo("<option value=\"$i\">$i</option><br/><br/>\n");
$i++;
}
echo("<option value=\"$order\" selected>$order</option><br/><br/>");
?>
</select><br/><br/>
Title:<br/><input type="text" size="35" name="name"/><br/><br/>
Image:<br/><input type="text" size="25" name="image"/><br/><br/>
<input type="submit" name="send" value="Add" onclick="this.disabled=true;"/>
</form>
<?
}
mysql_close();
}
else
{
echo "no orders";
}
?>