0

My question is how to store string or integer array values into mysql table. inside a table all values are store into single cell, the values are separated by comma.

ex:

<input type="text" name="product[]" value="rose">
<input type="text" name="product[]" value="garlands">
<input type="text" name="product[]" value="marigold">
<input type="text" name="product[]" value="jasmine">
<input type="text" name="product[]" value="jasmine garlands">

The above values are inserted into mysql table

$sql=mysql_query(" insert into product_sale(product_name) values (?)");

how to store all values and separated by comma, the stored values like

(rose,garlands,marigold,jasmine,jasmine garlands) in a single cell.

3 Answers 3

2

This is just a demo. Then, you can bind $product_name into your insert query.

<?php
if(isset($_REQUEST['save'])) {
    $product_name = implode(",", $_REQUEST["product"]);
    print $product_name;

}
$query="insert into product (product_name) values('".$product_name."')";
$sql=mysql_query($query);
if($sql)
    echo "success";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form name="myform" method="post">
    <input type="text" name="product[]" value="rose">
    <input type="text" name="product[]" value="garlands">
    <input type="text" name="product[]" value="marigold">
    <input type="text" name="product[]" value="jasmine">
    <input type="text" name="product[]" value="jasmine garlands">
    <input type="submit" name="save" value="Save">
</form>

</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

I wrote query for insert values. @Lea Tano
you can up-vote (up arrow) and/or you can click the check mark (for answered question) (next to my answer - left)
Lea Tano: How retrieve these values and display into table each product_name display into each row
@karthik, for new questions,you need to create post. Unfortunately, I can not post here the solution for the new question, since it will conflict with stackoverflow standards. But if you open a new question, I will be more than happy to do it.
0

comma separated values in a single table cell is not a good practice, Do take an another table with fields id and product_id,

if you want to fetch this data than it will be easy to fetch and display, comma separated values will be very messy

3 Comments

I have table for product details (sno, product_name, item_code, rate) fetch product details from product table, but if i going to sale product to customer means the all product name should be store in one cell and same as quantity of products, rate of products and total for each produts. I know little bit English .
@karthik no no, you are doing it wrong, you cant save quantity, rate as comma separated values.
Nishant Solanki: The above code is work fine. Thanks for rply.
0
<?php
include 'connection/db_connection.php';
if(isset($_REQUEST['save'])) {
$product_name = implode(",", $_REQUEST["product"]);
print $product_name;
}
$query="insert into product (product_name) values('".$product_name."')";
$sql=mysql_query($query);
if($sql) 
echo "success";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form name="myform" method="post">
<input type="text" name="product[]" value="rose">
<input type="text" name="product[]" value="garlands">
<input type="text" name="product[]" value="marigold">
<input type="text" name="product[]" value="jasmine">
<input type="text" name="product[]" value="jasmine garlands">
<input type="submit" name="save" value="Save">
</form>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.