Jump to content

insert data button


thelee

Recommended Posts

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 by fastsol
Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

fastsol.i have put it and this is the error:

 

Notice: Undefined index: name in C:\xampp\htdocs\emakengku\cart.phpon line 5

Notice: Undefined index: price in C:\xampp\htdocs\emakengku\cart.phpon line 7
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

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.