0

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?

5 Answers 5

2

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]
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

+ for PDO. Please write the equivalent in using mysqli.
@AlexanderMP Isn't mysqli is designed the same? both has the same method names for prapering, executing, both could be objects.
true, but a copy/paste with mentions to mysqli wouldn't hurt, wouldn't you say? I mean I appreciate this answer as it is, but to get more appreciation from everyone else, you have to write a more complete solution. Some of them might end up as pages that people click from search engines, and take as a guide on "how to do stuff".
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]')");

}

2 Comments

I found it a great surprise, that answers on StackOverflow would contain advice to use outdated functionality, which the authors of PHP have warned us NOT to use. php.net/manual/en/function.mysql-query.php "This extension is deprecated as of PHP 5.5.0, and will be removed in the future."
We all know that but no matter how many times you tell them, people won't listen and by the way i appreciate your downvote.
0

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

Comments

0

Like this

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

Comments

0
//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.'&nbsp;'.$salary.'&nbsp;'.$job.'';
    }
}

echo $personsList;

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.