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:&nbsp;<input name="tb_category_name" type="text" value="<?php echo $category_name ?>" /> <br />
		Position:&nbsp;<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:&nbsp;<input name="visible" type="radio" value="1"	
		<?php if($visible == 1){echo "checked";}?>	/> Yes
		&nbsp;<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();
?>

Recommended Answers

All 5 Replies

updated to this, still getting an error

<?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('tb_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
		$update = "UPDATE categories 
			SET category_name='$category_name', position='$position', visible='$visible' 
			WHERE category_id='$category_id'";
		$update_result = mysql_query($update);
		
	}
	else
	{
		//errors occured
		foreach($errors as $error)
		{
			echo $error;
		}
		//redirect_to("category_view.php");
	}
}
?>

<h1>Edit Category</h1>
<?php
/*
	if (!empty($message))
	{
		echo $message;
	}
	*/
?>
	<form action="category_edit.php" method="post">
		Category Name:&nbsp;<input name="tb_category_name" type="text" value="<?php echo $category_name ?>" /> <br />
		Position:&nbsp;<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:&nbsp;<input name="visible" type="radio" value="1"	
		<?php if($visible == 1){echo " checked";}?>	/> Yes
		&nbsp;<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();
?>

field_name has been renamed bt still getting an error

functions are included in connections for queries such as get_specific category and others.

please show your table structure for category table

category_id int 11 primary key auto increment
category_name varchar 30
position int 3
visible tinyint 1

all not null

im guessing that when i pass the hidden cat_id from form id doesnt catch by post method to be processed in submit button. if im wrong please enlighten me. thanks!

type

echo "<pre>";
print_r($_POST);

and share what you get

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.