1

Here's what I want to do in three steps.

1-) Get names from the form. Names are being entered into the form using a comma between them.

2-) Get the ID's of the names from the database.

3-) Insert these names into a table.

I hope I made clear what I want to do.

Here's what I've done so far. (I am working with a single page, excluded the form here)

$writer_name = "Franz Kafka, Agatha Christie, George Bernard Shaw"; // I used a
//variable instead of using a form value here
$thing_id = 10;

$values_select = explode(",", $writer_name);
echo $values_select[0]; // returns Franz Kafka, our explode function worked

$who = "SELECT writer_id FROM writer_name WHERE writer_name = '$values_select[]'"; 

// I stuck here, I need to get all the writer's ID's and store it in an array 
// in order to insert below X times (depends on how many writers are entered), right? But how?

$who_result = $sqli->query($who);
$who_row = $who_result->fetch_array();
$who_finish = $who_row['writer_id']; 

$values = explode(",", $writer_name);
$control = count($values) - 1;
$new_values = $sqli->prepare("INSERT INTO book(thing_id, person_id) VALUES(?,?)");
for ($i = 0; $i < count($values); $i++) {
    if (!empty(trim($values[$i]))) {
        $new_values .= "('" . trim($values[$i]) . "')";
    }
    if ($control == $i) {
        $new_values .= ";";
    } else $new_values .= ",";
}
    $thing->bind_param("ii", $thing_id, $values[$i]); // using integer because I insert data into a junction table that accepts only ID values)
    $thing->execute();
4
  • 1
    Use WHERE writer_name IN ($writer_name) You can pass IN comma separated lists and it'll return those records writer_name is in the list. See example:techonthenet.com/sql/in.php Commented Jan 2, 2015 at 14:18
  • 1
    Also, SQL is much more powerful than you think. Most of your PHP code can be ditched and the work offloaded to the DB. Commented Jan 2, 2015 at 14:19
  • 1
    ... = '$values_select[]' is "impossible" code. You're generating the literal query text ... = 'Array[]', which will match nothing. Commented Jan 2, 2015 at 14:41
  • @Marc B, I used it as a dummy text to show it, didn't know what to put here. Commented Jan 2, 2015 at 14:57

2 Answers 2

1

You may query all array values:

$who_finish = array();
while(list($k, $v)=each($values_select)) {
    $v = $sqli->real_escape_string(trim($v));
    $who = "SELECT writer_id FROM writer_name WHERE writer_name = '$v'";
    $who_result = $sqli->query($who);
    $who_row = $who_result->fetch_array();
    $who_finish[] = $who_row['writer_id'];
}

or better (btw do you check for typos etc.?) is to query all at once like this: SELECT writer_id, writer_name FROM writer_name WHERE writer_name IN ("Franz Kafka", "Agatha Christie", "George Bernard Shaw")

To insert values into table (I do not understand your code well so assuming here):

$new_values_q = $sqli->prepare("INSERT INTO book(thing_id, person_id) VALUES(?,?)");

while(list($k, $v)=each($who_finish)) {
    $new_values_q->bind_param("ii", $thing_id, $v); // $v should be author_id
    $new_values_q->execute();
}
$new_values_q->close();
Sign up to request clarification or add additional context in comments.

3 Comments

I don't check typos for now(but will), all records are being entered correctly since I copy/paste values in bulk from the same resource. I am trying your solution now.
This query works, but I can't still make the insert. echo $who_finish[0]; // gives first writer's ID echo $who_finish[1]; // gives second writer's ID echo $who_finish[2]; // gives third writer's ID But how can I insert these using a loop?
@salep I added insert piece to answer, but i do not understand your code well so I made assumption of what you need.
1

it may be helpful, not tested.

 $writer_name = "Franz Kafka, Agatha Christie, George Bernard Shaw";

$values_select = explode(",", $writer_name);

foreach($values_select as $sel){
    $query = "INSERT INTO book (thing_id, person_id)
SELECT $thing_id, location
FROM   writer_name
WHERE  writer_name  = ".trim($sel);
$sqli->query($query);
}

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.