fastsol Posted July 15, 2014 Share Posted July 15, 2014 (edited) Well there are lots of things wrong with this script but to help with your issue at hand, you need to put `backticks` (it's the key next to the 1 on your keyboard) around the table names and table columns in the query. The word "order" is a mysql reserved keyword and is making the query think something else besides the table names. So like this $sql_insert = "INSERT INTO `order` (`name`, `quantity`, `price`) values('$name', '$quantity', '$price')"; In all honesty you may need to remove the backticks around the column names inside the () and just have the column names only. I haven't used that particular syntax for a query string in quite a while. Edited July 15, 2014 by fastsol Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485267 Share on other sites More sharing options...
Psycho Posted July 15, 2014 Share Posted July 15, 2014 $sql_insert = "INSERT INTO `order` (`name`, `quantity`, `price`) values('$name', '$quantity', '$price')"; In all honesty you may need to remove the backticks around the column names inside the () and just have the column names only. I haven't used that particular syntax for a query string in quite a while. There's nothing wrong with using backticks for the column names as used above. If any column name was a reserved word they would be required. Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485286 Share on other sites More sharing options...
thelee Posted July 15, 2014 Author Share Posted July 15, 2014 i have put `backticks` and the result still same, Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485381 Share on other sites More sharing options...
fastsol Posted July 15, 2014 Share Posted July 15, 2014 Do this and it will tell you why the query failed. mysql_query($sql_insert) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485383 Share on other sites More sharing options...
thelee Posted July 16, 2014 Author Share Posted July 16, 2014 fastsol.i have put it and this is the error: Notice: Undefined index: name in C:\xampp\htdocs\emakengku\cart.phpon line 5Notice: Undefined index: price in C:\xampp\htdocs\emakengku\cart.phpon line 7You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order ('name', 'quantity', 'price') values('', 'Array', '')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485403 Share on other sites More sharing options...
Psycho Posted July 16, 2014 Share Posted July 16, 2014 (edited) 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 'order ('name', 'quantity', 'price') values('', 'Array', '')' at line 1 You did NOT use backticks => `````````. You used regular single ticks => '''''''' fastsol even told you exactly where on the keyboard the character was! Also, the variables you are using to insert data are not defined! Edited July 16, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485413 Share on other sites More sharing options...
thelee Posted July 16, 2014 Author Share Posted July 16, 2014 ops,sorry.i have changed it, data is recorded but the data is wrong.i dont know why the data is like this Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485417 Share on other sites More sharing options...
Psycho Posted July 16, 2014 Share Posted July 16, 2014 Because the variables you are using to add the values to the query are either not defined or are defined incorrectly. THe other errors you reported clearly showed that. Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485420 Share on other sites More sharing options...
thelee Posted July 16, 2014 Author Share Posted July 16, 2014 how i can make the query can define all the value? i thought by add this code. $name=$_POST["name"]; $quantity=$_POST["quantity"]; $price=$_POST["price"]; Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485421 Share on other sites More sharing options...
fastsol Posted July 16, 2014 Share Posted July 16, 2014 Well yeah they are undefined cause you aren't posting any of those, at least based on the code you have provided to us. The only form input you have in the code is for the quantity and that's posting as an array which is why it shows ARRAY in your db. You don't have any inputs for name or price in the form being posted. Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485436 Share on other sites More sharing options...
mac_gyver Posted July 16, 2014 Share Posted July 16, 2014 the goal of the code you are working on is to take the contents of $_SESSION['cart'] and insert it into a database table. that the button that tells the code to do that is part of an exiting update form is coincidental. that you started a new thread for an existing problem, means that those reading this thread don't necessarily know what you are trying to accomplish. you must always keep in mine what goal you are trying to achieve when writing code. in fact, it wouldn't hurt to have that goal as a comment right above the block of code in your program. if you haven't defined what the inputs your code will receive (in this case you have a form button name/value, a $_SESSION['cart'] array with product id's and quantities, and a database table, products, that holds the name and price for the items in the cart), what goal the code is trying to accomplish, and what result or output the code will produce (rows in an `order`, that would be your first step before writing any code. btw - the reason i suggested in your previous thread to name the table order_details was to give it a meaningful name (it holds details about orders) and to avoid all the problems you have in this thread trying to use `order` as the table name. you also should not put the product name into this table, you should use the product_id (whatever you are calling it), so that the product name only exists in one place, in the products table. Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485442 Share on other sites More sharing options...
mac_gyver Posted July 17, 2014 Share Posted July 17, 2014 i previously gave you this advice - your current code is displaying the content of the cart. wouldn't you use similar code to store the content of your cart into a database table? this is your current code that is displaying the content of the cart - $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price']; $totalprice+=$subtotal; ?> <tr> <td><?php echo $row['name'] ?></td> <td><input type="text" name="quantity[]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td> <td>RM <?php echo $row['price'] ?></td> <td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td> </tr> <?php } make a COPY of this code and change it so that the name, quantity, and price values it has are used in an INSERT query. Quote Link to comment https://forums.phpfreaks.com/topic/289578-insert-data-button/page/2/#findComment-1485541 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.