ggw Posted April 2, 2012 Share Posted April 2, 2012 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!! Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 Post the code Quote Link to comment Share on other sites More sharing options...
ggw Posted April 2, 2012 Author Share Posted April 2, 2012 <?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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 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. Quote Link to comment Share on other sites More sharing options...
ggw Posted April 2, 2012 Author Share Posted April 2, 2012 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 = Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 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. Quote Link to comment Share on other sites More sharing options...
ggw Posted April 2, 2012 Author Share Posted April 2, 2012 I'm not too sure what you mean. I am just starting out in php :-\ Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 Okay, so do you know what the $_POST array is? Do you have knowledge of HTML? I assume you got this code from a tutorial? Quote Link to comment Share on other sites More sharing options...
ggw Posted April 2, 2012 Author Share Posted April 2, 2012 No the code isn't from a tutorial. I just thought that $item_id = $_POST['item_id']; this line of code would post the item_id. Quote Link to comment Share on other sites More sharing options...
ggw Posted April 2, 2012 Author Share Posted April 2, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 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. Quote Link to comment Share on other sites More sharing options...
ggw Posted April 2, 2012 Author Share Posted April 2, 2012 How do we get it so it knows the item_id? I am completely clueless as to why its not working. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 2, 2012 Share Posted April 2, 2012 Actually I'm sorry, I see you did put the hidden input, but it looks like IT has no value. When you select your $row, can you print_r() it, so we can see what is in $row when it is first created? Quote Link to comment Share on other sites More sharing options...
cpd Posted April 2, 2012 Share Posted April 2, 2012 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. Quote Link to comment Share on other sites More sharing options...
ggw Posted April 4, 2012 Author Share Posted April 4, 2012 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!! Quote Link to comment Share on other sites More sharing options...
ggw Posted April 4, 2012 Author Share Posted April 4, 2012 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!! Quote Link to comment Share on other sites More sharing options...
ggw Posted April 4, 2012 Author Share Posted April 4, 2012 I've done it guys!!!!!!! Thanks for all the help!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Quote Link to comment Share on other sites More sharing options...
ggw Posted April 4, 2012 Author Share Posted April 4, 2012 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.