用php中的if语句插入数据库表

这个问题在这里已经有了答案:

  • 3个不同等于4个答案

  • 这是一个赋值,表达式返回bool(true)

    if ($reg_as = "Exporter")

    为了比较这两个值,你应该使用两个等号:

    if ($reg_as == "Exporter")

    也许你可以做这样的事情:

    <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/58467.html

    上一篇: Insert into database table with if else statement in php

    下一篇: Identical Strings in PHP not matching