3

I have 6 text fields

<input id="es_price" name="es_price" placeholder="$ (AUD)" class="form-control" type="text">
<input id="es_price2" name="es_price2" placeholder="$ (AUD)" class="form-control" type="text">
<input id="es_price3" name="es_price3" placeholder="$ (AUD)" class="form-control" type="text">
<input id="es_price4" name="es_price4" placeholder="$ (AUD)" class="form-control" type="text">
<input id="es_price5" name="es_price5" placeholder="$ (AUD)" class="form-control" type="text">
<input id="es_price6" name="es_price6" placeholder="$ (AUD)" class="form-control" type="text">

and my database structure is

CREATE TABLE `es_prices_outcall` (
`es_prices_outcall_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`es_price` VARCHAR(50) NOT NULL DEFAULT '0',
PRIMARY KEY (`es_prices_outcall_id`)
)

I want, when i will submit data it should store in same database like an array. i don't want to create 6 column (es_price, es_price2, es_price3, es_price4, es_price5, and es_price6).

I m not very good in php, but i want to implement it.

Please help me, will be thankful to all of you.

As above i have mentioned all 6 text fields and their name/id, also i have shown my db structure, i want to insert all 6 fields data in my database, I don't want to change my text field name as i don't want to create 6 column to insert 6 text fields values, i want to use only one column "es_price" and want to all 6 text fields in 6 rows of "es_price" column.

I m not good enough to fix this, so i need proper guidance,

Thanks, Arshi

5
  • @Arshi use serialize Commented Oct 30, 2015 at 12:34
  • insert the all values with comma separete (,)...like price1,price2,price3..so on,which act as an array Commented Oct 30, 2015 at 12:37
  • Just save the fields comma (or semicolon because it's a price) separated? Commented Oct 30, 2015 at 12:37
  • Why is your price field varchar type? Commented Oct 30, 2015 at 12:39
  • I was trying to put all 6 fields value in 6 row of that column "es_price", is there any way, as i said i m not good enough in php/MySQL, but need to fix it. Commented Oct 30, 2015 at 12:47

4 Answers 4

1

Make a variable first which contain all price with comma.

$allPrice=$_POST['price1'].','.$_POST['price2'].','.$_POST['price3'].','.$_POST['price4'].','.$_POST['price5'].','.$_POST['price6'];

Then fire general insert query.

.........(`es_price`) value($allPrice)
Sign up to request clarification or add additional context in comments.

Comments

1

What you're asking for isn't completely clear. From what I gather you want to add multiple rows of the same field into a database.

I recommend something like this:

 if(isset($_POST)){
        $qty = count($_POST['cat_id']);
        for($i = 0; $i < $qty; $i++){
            $wpdb->insert( 'pricelist', 
                array(
                    'es_price' => $_POST['es_price'][$i],

                ), 
                array(
                    '%d',
                )
            );
        }

}

2 Comments

Hidden, yes u r getting me, I want to add all 6 text field data in 1 column and in 6 rows, May clarify more, i mean some simple code ?
So the above code puts the data into an array, then loops round till each row has been inserted
1
 <?php
    if(isset($_REQUEST['submit']))
    {
    $q= "INSERT INTO User_Roll(name) VALUES 
    ('".$_REQUEST['es_price']."'),
    ('".$_REQUEST['es_price2']."'),
    ('".$_REQUEST['es_price3']."'),
    ('".$_REQUEST['es_price4']."'),
    ('".$_REQUEST['es_price5']."'),
    ('".$_REQUEST['es_price6']."'),
    ";
    }
    ?>

YOU CAN use $_POST , $_GET and $_REQUEST,if you declare method="post" than you can able to fetch data using $_POST or same as get where $_REQUEST support both method get and post

Comments

0

Try This:-

<?php

if(isset($_GET['submit']))
{
$q= "INSERT INTO User_Roll(name) VALUES 
('".$_GET['es_price']."'),
('".$_GET['es_price2']."'),
('".$_GET['es_price3']."'),
('".$_GET['es_price4']."'),
('".$_GET['es_price5']."'),
('".$_GET['es_price6']."'),
";
//Execute Query
}

?>

You can only use $_POST or $_GET and $_REQUEST not both($_POST,$_GET)...depend what you declare in form method

1 Comment

Yes, i have modified little bit now its working as i wanted @Himanshu!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.