0

May be i didn't ask well Let's say i have two forms

<form action="reg.php" method="post" enctype="multipart/form-data">
    <input type="text" name="reg_as" id="export" value="exporter">
    <input type="submit" name="reg">
</form>

<form action="reg.php" method="post" enctype="multipart/form-data">
    <input type="text" name="reg_as" id="import" value="importer">
    <input type="submit" name="reg">
</form>

How do i insert into multiple tables with if else conditions reg.php

$reg_as = strip_tags(@$_POST['reg_as']);
$reg = strip_tags(@$_POST['reg']);
$exporter = "exporter";
$importer = "importer";

if ($reg)
{
    if ($reg_as == $exporter)
                    {
                        $sql =<<<EOF
INSERT INTO exporter (reg_as) 
VALUES ('$reg_as');
EOF;

                        $stmt = $db->exec($sql);

                        if (!$stmt)
                        {
                            echo "<script>alert('There was an error some where!.. Please try again')</script>";
                        }
                        else
                        {
                            header("Location: index.php");
                        }
                        if ($reg_as == $importer) {
                            $sql = <<<EOF
INSERT INTO importer (reg_as) 
VALUES ('$reg_as');
EOF;
                        }

                        $stmt = $db->exec($sql);

                        if (!$stmt)
                        {
                            echo "<script>alert('There was an error some where!.. Please try again')</script>";
                        }
                        else
                        {
                            header("Location: index.php");
                        }

}

The issue here is that, the data dosn't insert to database. Please what could be wrong?

Tega Oke
  • 19
  • 1
  • 3
  • You're assigning `=` rather than comparing `==`. – Funk Forty Niner Mar 29 '17 at 14:53
  • `"I am aware of the security risk of the php code so please ignore that part"` - What if that part is what's causing a problem? When your code isn't working in some way, assuring us that everything is fine and doesn't need to be corrected is *not* a particularly effective way to diagnose and solve the problem. – David Mar 29 '17 at 14:56
  • This guy **Hubert Blaine Wolfeschlegelsteinhausenbergerdorff** would never be able to signup on your system – Masivuye Cokile Mar 29 '17 at 15:00

1 Answers1

1

This is an assignment, and the expression returns bool(true):

if ($reg_as = "Exporter")

To compare the two values, you should use two equal signs:

if ($reg_as == "Exporter")

Maybe your could do something like this:

<form action="reg.php" method="post" enctype="multipart/form-data">
    <select name="type" id="type">
        <option value="importer">Importer</option>
        <option value="exporter">Exporter</option>
    </select>

    <input type="text" name="reg_as" placeholder="Insert your data here..." />

    <input type="submit" name="submit" value="Save">
</form>

<?php

if (!empty($_POST['submit'])) {
    $type = in_array($_POST['type'], ['exporter', 'importer']) ? $_POST['type'] : false;
    $value = $_POST['reg_as'];

    switch ($type) {
        case 'exporter':
            $query = "INSERT INTO exporter (reg_as) VALUES (:reg_as)";
            break;
        case 'importer':
            $query = "INSERT INTO importer (reg_as) VALUES (:reg_as)";
            break;
        default:
            throw new Exception('Handle your error here!');
            break;
    }

    $statement = $db->prepare($query);
    $insert = $statement->execute([':reg_as' => $value]);

    if ($insert) {
        header("Location: index.php");
    } else {
        echo "<script>alert('There was an error some where!.. Please try again')</script>";
    }
}
fedeisas
  • 1,991
  • 2
  • 22
  • 34