Jump to content

mysql error


ggw

Recommended Posts

Hello I was just wondering if anyone could tell me what this error meas:

 

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 1

 

Any help would be appreciated! Thanks!!

Link to comment
Share on other sites

<?php


if (isset($_POST['save'])){

echo "<br /> edit value is true";
	$item_id =$_POST['item_id'];
	$name = $_POST['name'];	
	$description = $_POST['description'];	
	$price = $_POST['price'];
	$image = $_FILES['images']['name'];
	$category_id = $_POST['category_name'];

	include('../includes/image-upload.php');


	$query = ("UPDATE item SET item_name = '$name', item_description = '$description', item_image = '$image', item_price = '$price' WHERE item_id = ". $item_id);

	mysql_query($query) or die(mysql_error());


	$item_id = mysql_insert_id();
	echo "item edited: " .$item_id;
	$query.= "UPDATE links SET item_id = '$item_id', category_id = '$category_id',";

	header('Location: item-edit.php');


if(isset($_GET['item_id'])) {
	$outcome = mysql_query("
	SELECT *
	FROM item
	WHERE item.item_id = " .$_GET['item_id']);

$row = mysql_fetch_array($outcome);
}	}
?>

 

This is the form:

 

<form action="item-edit.php" method="POST" enctype="multipart/form-data">
            
            <div>
            	<label for="name">Name:</label>
                <input id="name" name="name" type="text" value="<?php echo $row['item_name']; ?>" />
		</div>
            <div>
            	<label for="images">Image:</label>
                <input id="images" name="images" type="file" />
            </div>        
            <div>
            	<label for="category_name">Category:</label>
                
               
                <?php
                $query=("SELECT category_id, category_name FROM category");


			$result = mysql_query ($query);
			echo "<select id='category_name' name='category_name'>";


			while($nt=mysql_fetch_array($result)){
			echo "<option selected='selected' value=$nt[category_id]>$nt[category_name]</option>";
			/* Option values are added by looping through the array */
			}
			echo "</select>";// Closing of list box ?>

                	
            </div>
            <div>
            	<label for="item_description">Description</label>
                <textarea id="description" name="description"><?php echo $row['item_description']; ?></textarea>
            </div>
            <div>
            	<label for="item_price">Price:</label>
                <input id="price" name="price" type="text" value="<?php echo $row['item_price']; ?>" />
            </div>
            <div class="bottom_div">
            	<input name="item_id" type="hidden" value="<?php echo $row['item_id']; ?>" />
            	<input name="save" value="Save" type="Submit" />
            </div>
            
        </form>

 

Thanks!

Link to comment
Share on other sites

You have several queries, do you know which one is throwing this error? It looks like the first one you've posted.

Based on the error, I'd say one of your variables is empty.

When you throw the error, (the or die(mysql_error()); party), add in the query string to your die.

or die(mysql_error().': '.$query);

 

Other problems:

This line:

$query.= "UPDATE links SET item_id = '$item_id', category_id = '$category_id',";

 

Will not only never run because you redirect afterwards, that query cannot be appended to any other one, and you end it with a comma which should also produce an error.

 

you also need to sanitize user input, and you don't need to put parentheses around your strings.

Link to comment
Share on other sites

This is the error that appears now:

 

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 1: UPDATE item SET item_name = 'test edited', item_description = 'test edited', item_image = 'Hydrangeas.jpg', item_price = '22' WHERE item_id =

Link to comment
Share on other sites

So you can tell that one of your variables has no value at the time you try to run that query. Looks like your form is not posting the item_id. Try adding print_r($_POST); before you set those variables from the form. But it is obvious from your form html that you never send that value.

Link to comment
Share on other sites

I have added

if (isset($_POST['save'])){
 print_r ($_POST);

 

This is what happens:

 

Array ( [name] => test edited [category_name] => 3 [description] => test edited [price] => 28 [item_id] => [save] => Save )

 

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 1: UPDATE item SET item_name = 'test edited', item_description = 'test edited', item_image = 'Jellyfish.jpg', item_price = '28' WHERE item_id =

 

Thanks

Link to comment
Share on other sites

No, that line will take the value FROM the posted data. Data will only be posted if it is inside the HTML form element, and has a form element with a name.

So $item_id= $_POST['item_id']; will get you the value that was entered within the form in an input with the name "item_id". It can be a hidden input.

Link to comment
Share on other sites

Your code errors because, as pointed out, you have no item_id being POSTED to the header upon form submission.

 

$row['item_id'] is not printing anything as the page is outputted. If you show us your query that gets this we can go from there.

Link to comment
Share on other sites

In ourworkadmin.php we have:

 

<?php
if(isset($_GET['remove']))	{
		header('Location: ../includes/item-delete.php?item_id='.$_GET['item_id']);
}

if(isset($_GET['edit']))	{
	header('Location: ../includes/item-edit.php?item_id='.$_GET['item_id']);
}
?>

 

item-edit.php the code is:

 

<?php



if (isset($_POST['edit'])){
 print_r ($_POST);

echo "<br /> edit value is true";
	$name = $_POST['name'];	
	$description = $_POST['description'];	
	$price = $_POST['price'];
	$image = $_FILES['images']['name'];
	$category_id = $_POST['category_name'];
	$item_id = $_GET['item_id'];


	include('../includes/image-upload.php');


	$query = ("UPDATE item SET item_name = '$name', item_description = '$description', item_image = '$image', item_price = '$price' WHERE item_id = ".$_GET['item_id']);

	mysql_query($query)
	or die(mysql_error().': '.$query);


	$item_id = mysql_insert_id();
	echo "item edited: " .$_GET['item_id'];
	$query.= "UPDATE links SET item_id = $item_id, category_id = $category_id,";

	header('Location: item-edit.php');

if(isset($_GET['item_id'])) {
	$result = mysql_query("
	SELECT *
	FROM item
	WHERE item.item_id = " .$_GET['item_id']);

	$row = mysql_fetch_array($result);


}}
?>

 

And this is the form:

 

	<div id="mid-col">
	  <h2 id="homecol3">
          <form action="item-edit.php" method="POST" enctype="multipart/form-data">
            
            <div>
            	<label for="name">Name:</label>
                <input id="name" name="name" type="text" value="<?php echo $itemname ?>" />
		</div>
            <div>
            	<label for="images">Image:</label>
                <input id="images" name="images" type="file" />
            </div>        
            <div>
            	<label for="category_name">Category:</label>
                
               
                <?php
                $query=("SELECT category_id, category_name FROM category");


			$result = mysql_query ($query);
			echo "<select id='category_name' name='category_name'>";


			while($nt=mysql_fetch_array($result)){
			echo "<option selected='selected' value=$nt[category_id]>$nt[category_name]</option>";
			/* Option values are added by looping through the array */
			}
			echo "</select>";// Closing of list box ?>

                	
            </div>
            <div>
            	<label for="item_description">Description</label>
                <textarea id="description" name="description"><?php echo $row['item_description']; ?></textarea>
            </div>
            <div>
            	<label for="item_price">Price:</label>
                <input id="price" name="price" type="text" value="<?php echo $row['item_price']; ?>" />
            </div>
            <div>
            	<input name="item_id" type="hidden" value="<?php echo $item_id; ?>" />
            	<input name="edit" value="Edit" type="submit" />
            </div>
            
        </form>
</h2>
	</div>

 

Thanks!!

Link to comment
Share on other sites

Guys I have changed the code a bit and now this is the error I am getting:

 

Click here to logout Array ( [name] => TEST EDITED [category_name] => 3 [description] => GGGGG [price] => 35 [item_id] => 90 [save] => Save )

edit value is true

 

The file has been uploaded!

 

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 1: UPDATE item SET item_name = 'TEST EDITED', item_description = 'GGGGG', item_image = 'Koala.jpg', item_price = '35' WHERE item_id =

 

In the array it has picked up the item_id that is being edited is 90. However in the UPDATE statement it is not finding the item_id.

 

Can anyone help?

 

 

Thanks!!

 

Link to comment
Share on other sites

I have a new problem!! I have it editing all my items however on the form there is a drop down menu to select which category the item belongs to, I've just realised the category isn't changing when being edited. Can anyone help?

 

The code I have for the category drop down box is :

 

<label for="category_name">Category:</label>
                
               
                <?php
                $query=("SELECT category_id, category_name FROM category");


			$result = mysql_query ($query);
			echo "<select id='category_name' name='category_name'>";


			while($nt=mysql_fetch_array($result)){
			echo "<option selected='selected' value=$nt[category_id]>$nt[category_name]</option>";
			/* Option values are added by looping through the array */
			}
			echo "</select>";// Closing of list box ?>

                	

 

Any help would be much appreciated!

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.