-1

Database

table: "country" 
coloums:"id_country,country_name"

i have only one text feild:

Country: <input type=text name="country_name" >
$conn->query("insert into country");

i want to fill this feild with

"india,australia,canada,etc."

from the above line the (,) has to treat like new value.! so it has to store in db with id_country increment..

Mr world wide
  • 4,696
  • 7
  • 43
  • 97

5 Answers5

3

You can do it like below:-

<?php

$data = "india,australia,canada";
$exploded_data = explode(',',$data);
$data_to_insert = "('".implode("','",$exploded_data)."')";

echo $data_to_insert;

Output:-

https://eval.in/636118

and

https://eval.in/636131

Note:- Your id_country column must be Auto-incremented primary key.

If so then just one query:-

"INSERT INTO country (country_name) VALUES ".$data_to_insert;

Or

"INSERT INTO country (country_name) VALUES $data_to_insert";

will do every-thing

Better to do this with prepared statements.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
2

Best would be to use PHP's explode() function on the input text, using ", " as a delimiter, and then adding each country in the resulting array to your database via a foreach loop.

More info: http://php.net/manual/en/function.explode.php

Iain
  • 387
  • 2
  • 12
2

I'm not sure if I understand you, but I can explain you, EXPLODE. It'll return an array of strings.

$values = "value1, value2";
$explode = explode(",", $values);

You can continue with the following after that;

$explode[0];
$explode[1];
Slatyoo
  • 130
  • 15
Pascal Boschma
  • 187
  • 1
  • 14
2
$countries = ['india','australia','canada']

foreach ($countries as $country) {
     // Run/build you sql query
} 

SQL example query

INSERT INTO `country` (`country_name`)
VALUES ('india');
Spholt
  • 3,724
  • 1
  • 18
  • 29
2

To process the posted string firstly ensure the variable is actually set and passes rudimentary tests and use explode to generate an array containing the country names. Using mysqli and prepared statements allows you to construct the sql statement once, bind the necessary parameter(s) and variables once but execute multiple times very quickly.

$dbhost =   'localhost';
$dbuser =   'xxx'; 
$dbpwd  =   'xxx';
$dbname =   'xxx';
$db =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );

/* make sure the posted variable isn't empty */
$countries=!empty( $_POST['country_name'] ) && stristr( $_POST['country_name'], ',' ) ? $_POST['country_name'] : false;
if( $countries ){

    /* explode the string to create the array */
    $countries=explode(',',$countries);

    /* construct sql statement for use as a prepared statement */
    $sql='insert into `country` set `country_name`=?';

    /* prepare the statement object */
    $stmt=$db->prepare( $sql );

    /* bind the param to the variable */
    $stmt->bind_param( 's', $country );

    /* iterate through all countries from POSTed string and execute the statement */
    foreach( $countries as $country ){
        $stmt->execute();
    }
}

$stmt->close();
$db->close();
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46