1

So i have a selector wich is based on my Database. Once people select something from that selector, select it and press next. They will see this page:

(Selected train02 in example here)

Selected something from the DB

Code for this is:

<form method="POST" action="add_to_summary.php?user_id=<?php echo $_SESSION['user_id']?>">
            <div id="customer_list_table">
                <table>
                    <tr>
                        <th>Train id</th>
                        <th>Train Name</th>
                        <th>Tare Weight</th>
                        <th>Numbers of bogies</th>
                        <th>Numbers of axles</th>
                    </tr>
                    <?php 
                        foreach($_POST["checkbox"] as $key=>$val){
                        $data = $database->getAllAssoc_id($val);
                        foreach($data as $data1) { ?>
                            <tr>
                                <input type='hidden' name='user_id[<?php echo $_GET['user_id']?>]' value='<?php echo $_GET['user_id']?>'>
                                <input type='hidden' name='train_id[<?php echo $data1['train_id']?>]' value='<?php echo $data1['train_id']?>'>
                                <td><?= $data1['train_id'] ?></td>
                                <td><?= $data1['train_name'] ?></td>
                                <td><?= $data1['tare_weight'] ?></td>
                                <td><?= $data1['number_of_bogies'] ?></td>
                                <td><?= $data1['number_of_axles'] ?></td>
                            </tr><?php
                        }
                    } ?>
                </table>
                </div>
            <input name="Add to list" type="submit" id="add_to_list" value="Add to list">
        </form>

When they press Add to list, an insert gets send to my database like this:

function summary_add($id) {
            $sql = "INSERT INTO user_train_information "
        . "(train_id, user_id)"
        . "VALUES (:train_id, :user_id) ";
            $sth = $this->pdo->prepare($sql);
            $sth->bindParam(':train_id', $id, PDO::PARAM_STR);
            $sth->bindParam(':user_id', $id, PDO::PARAM_STR);
            $sth->execute();
    }

Code on the add_to_summary.php:

<?php
        foreach($_POST['user_id'] as $id) {
            $test = $database->summary_add($id);
        }
?>

Now, info gets inserted in my DB. but double the one i selected. like this:

what it does

And when i change $_POST['user_id'] to $_POST['train_id'] it inputs 14 2 times.

When i just do foreach($_POST as $id), i get 2 array to string conversion errors.

How do i fix this???

I want the table to insert correctly so that the user_id column will be 1, and the train_id column 14 EDIT:
for the selector / selection boxes: form pass selected to next page

EDIT:
It is working right now!

What is changed:

Changed the to hidden fields:

<input type='hidden' name='user_id' value='<?php echo $_GET['user_id']?>'>
<input type='hidden' name='train_id' value='<?php echo $data1['train_id']?>'>

The function:

function summary_add($train_id, $user_id) {
            $sql = "INSERT INTO user_train_information "
        . "(train_id, user_id)"
        . "VALUES (:train_id, :user_id) ";
            $sth = $this->pdo->prepare($sql);
            $sth->bindParam(':train_id', $train_id, PDO::PARAM_STR);
            $sth->bindParam(':user_id', $user_id, PDO::PARAM_STR);
            $sth->execute();
    }

Where i insert it:

<?php
            $test = $database->summary_add($_POST['train_id'], $_POST['user_id']);
            print_r($_POST);
?>
2
  • so, where are your checkbox codes? Commented May 18, 2015 at 14:03
  • Those are not needed since they already work, but il edit them in anyway Commented May 18, 2015 at 14:05

1 Answer 1

1
function summary_add($train_id, $user_id) {
            $sql = "INSERT INTO user_train_information "
        . "(train_id, user_id)"
        . "VALUES (:train_id, :user_id) ";
            $sth = $this->pdo->prepare($sql);
            $sth->bindParam(':train_id', $train_id, PDO::PARAM_STR);
            $sth->bindParam(':user_id', $user_id, PDO::PARAM_STR);
            $sth->execute();
    }

Code on the add_to_summary.php:

<?php
            $test = $database->summary_add($_POST['train_id'][0], $_POST['user_id'][0]);
?>

Summary: you have to put both the train_id and the user_id in your summary_add function.

Sign up to request clarification or add additional context in comments.

8 Comments

What do you mean with: (/* Do your logic here */) ? I don't get it haha
I assume it was something like "Iterate over this the way you want to iterate", in other words your previous "foreach" or "for" loop
When i use my previous loop: foreach($_POST['train_id'] as $id) { $test = $database->summary_add($_POST['train_id'], $_POST['user_id']); } i get array to string conversions. This is because i don't know what else to put in the loop haha
Also when i remove the loop and just do this: $test = $database->summary_add($_POST['train_id'], $_POST['user_id']); i get array to string conversion
Try to do print_r($_POST) and see what this array looks like.
|

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.