lalith458
lalith458

Reputation: 695

How to insert the multiple values in mysql using php

i have the mysql table 'persons' with 3 columns as,

Name   Salary  Profession

I am sending 3 parameters with values using php get method as,

 $name = raj|lokesh|amar
 $salary = 10000|20000|30000
 $job = telecom|marine|shipyard

I have to insert them in 'persons' table as,

Name     Salaray  Profession

raj      10000     telecom
lokesh   20000     marine
amar     30000     shipyard

Can any one tell me how can i do this?

Upvotes: 0

Views: 300

Answers (5)

Dev_x7xClownx7x
Dev_x7xClownx7x

Reputation: 42

//ASSIGN THE VARIABLES TO INSERT
    $name= '';
    $salary='';
    $job='';
//INSERT DATA INTO DATABASE
    $Input = mysql_query('INSERT INTO persons (name, salary, profession) VALUES ($name, $salary, $job)');
//SEARCH FOR PERSONS
    $output= mysql_query('SELECT * FROM persons ORDER BY id ASC');
    $personsCount = mysql_num_rows($output); //count output amount
//LOOP PERSONS OUTPUT
    if($personsCount > 0){
        while($row=mysql_fetch_array($output)){
                $id = $row['id'];
            $name = $row['name'];
            $salary = $row['salary'];
            $job = $row['job'];

    $personsList .=  $name.' '.$salary.' '.$job.'';
    }
}

echo $personsList;

Upvotes: 0

Jony Kale
Jony Kale

Reputation: 979

You can turn string into an array using the explode function. You can surely use this in your case, using my little demonstration:

$name = "raj|lokesh|amar";
$salary = "10000|20000|30000";
$job = "telecom|marine|shipyard";

You just set the variables. Now turn them into exploded arrays:

$name = explode("|", $name);
$salary = explode("|", $salary);
$job = explode("|", $job);

You basically want to get all of the words between the character | and turn each word into an array item, so each word will have it's own index.

now, $name[0] (the first array index),

echo $name[0]; // echoes 'raj'
echo $name[1]; // echoes lokesh'
echo $job[3]; // echoes 'shipyard';

And now you have to loop trough these arrays and insert it in the query:

for ($i = 0; $i < count($name); $i++) {
    echo $name[$i];
}

So final solution will look like this:

for ($i = 0; $i < count($name); $i++) {
    $query = $pdoObject->prepare("INSERT INTO table (name, salary, profession) VALUES (:name, :salary, :jobs)");
    $query->execute(array(
        ":name" => $name[$i], 
        ":salary" => $salary[$i], 
        ":jobs" => $jobs[$i]
    );
}

Upvotes: 2

Nes
Nes

Reputation: 304

Like this

INSERT INTO `persons`(`Name`, `Salaray`, `Profession`) values('raj', '10000','telecom'),('lokesh', '20000','marine'),('amar', '30000','30000')

Upvotes: 0

eyettea
eyettea

Reputation: 1446

Try to use

$names = explode("|", $name);
$salaries = explode("|", $salary);
$professions = explode("|", $profession);

and then loop through the arrays ($names, $salaries, $professions) to insert the values in your database. http://php.net/manual/fr/function.explode.php

Upvotes: 0

This is not a direct answer , since you haven't showed us any code. Try something like this.

<?php
$name = "raj|lokesh|amar";
$salary = "10000|20000|30000";
$job = "telecom|marine|shipyard";

$name = explode("|",$name);
$salary=explode("|",$salary);
$job=explode("|",$job);

for($i=0;$i<count($name);$i++)
{
    $q = mysql_query("INSERT INTO `yourtable` VALUES ('$name[$i]','$salary[$i]','$job[$i]')");

}

Upvotes: 0

Related Questions