Insert into database table with if else statement in php

This question already has an answer here:

  • The 3 different equals 4 answers

  • 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>";
        }
    }
    
    链接地址: http://www.djcxy.com/p/58468.html

    上一篇: Php如果其他人不工作

    下一篇: 用php中的if语句插入数据库表