Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
<?php
ob_start();
require_once("includes/connection.php");
include_once("includes/header.php");
//get category id according to selected record
$category_id = $_GET['cat_id'];
//fetch data to populate into form
$result = get_specific_category($category_id);
while($row = mysql_fetch_array($result))
{
$category_name = $row['category_name'];
$position = $row['position'];
$visible = $row['visible'];
}
//process update
if(isset($_POST['btn_submit']))
{
$errors = array();
//check all data if not set or empty
$required_fields = array($category_name, $position, $visible);
foreach($required_fields as $field_name)
{
if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && ($_POST[$fieldname] != 0)))
{
$errors[] = $field_name;
}
}
/*
//make sure input is less than or equal to 30chars
$fields_with_lengths = array($category_name => 30);
foreach($fields_with_lengths as $fieldname => $maxlength)
{
if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength)
{
$errors[] = $field_name;
}
}
*/
//redirect if invalid input
if(empty($errors))
{
//get all data input
$category_id = $_POST['cat_id'];
$category_name = mysql_prep($_POST['tb_category_name']);
$position = $_POST['position'];
$visible = $_POST['visible'];
//peform update
$query = "UPDATE categories
SET category_name='$category_name', position='$position', visible='$visible'
WHERE category_id='$category_id'";
$result = mysql_query($query);
}
else
{
//errors occured
foreach($errors as $error)
{
echo $error;
}
//redirect_to("category_view.php");
}
}
?>
<h1>Edit Category</h1>
<form action="category_edit.php" method="post">
Category Name: <input name="tb_category_name" type="text" value="<?php echo $category_name ?>" /> <br />
Position: <select name="position">
<?php
$result_category = get_categories();
$num_rows = mysql_num_rows($result_category);
for ($count = 1; $count <= $num_rows; $count++)
{
echo "<option value=\"{$count}\"";
if($position == $count)
{
echo " selected";
}
echo ">{$count}</options>";
}
?>
</select>
<br />
Visible: <input name="visible" type="radio" value="1"
<?php if($visible == 1){echo "checked";}?> /> Yes
<input name="visible" type="radio" value="0"
<?php if($visible == 0){echo "checked";}?>/> No<br />
<input name="cat_id" type="hidden" value="<?php echo $category_id ?>" />
<input name="btn_reset" type="reset" value="Reset" />
<input name="btn_submit" type="submit" value="Update" />
</form>
<?php
include_once("includes/footer.php");
ob_flush();
?>