php - 如何通过比较PHP中的选项值将下拉值中的值插入数据库表?

标签 php mysql

数据库表

Table #1- april
id  | month  | rent
-------------------
1   |  null  | null

Table #2- may

id  |  month  |rent
-------------------
1   |  null  | null

HTML 表单代码

<form  name="SelectPeriod" method="post" action="monthselect.php">
Select month:
<select name="month">
    <?php
    $monthName = array("JANUARY", " FEBRUARY", " MARCH", " APRIL", " MAY", " JUNE", " JULY", " AUGUST", " SEPTEMBER", " OCTOBER ", " NOVEMBER ", " DECEMBER ");
    for ($i = 0; $i < count($monthName); $i++) {
        $mn = 1 + $i;
        if ($mn == date("m")) {
            echo "<option selected value = " . $mn . ">" . $monthName[$i] . "</option> \n";
        } else {
            echo "<option value = " . $mn . ">" . $monthName[$i] . "</option> \n";
        }
    }
    ?>
</select>
<input type="text" name="rent" value="" placeholder="Enter rent input"/>
<input type="submit" name="selectmonth" value=" Go" /></form>

这里是用于插入的 php 代码

<?php   if (isset($_POST['selectmonth'])) {
$month=$_POST['month'];//name of drop-down
$rent = $_POST['rent'];//name of textbox
if ($month == 'APRIL') { //<---here is the problem how to compare with table
    $sql = "INSERT INTO april (month, rent) VALUES('$month','$rent')";
    $result = $con->query($sql);
    if ($result === TRUE) {
        echo 'Successfully Inserted';
    } else {
        echo 'There is a problem';
    }
} elseif ($month == 'MAY') {
    $sql = "INSERT INTO may (month, rent) VALUES('$month','$rent')";
    $result = $con->query($sql);
    if ($result === TRUE) {
        echo 'Successfully Inserted';
    } else {
        echo 'There is a problem';
    }
}}?>

我想知道如何将四月和五月的下拉月份值与比较值插入到特定的月份表中。或任何替代解决方案?

最佳答案

使用下面的代码,您不需要在月份 ID 的帮助下比较任何月份,您可以将数据插入您想要的任何表格,但我很想知道为什么您需要每个月的单独表格而不是您可以管理单个表中的所有数据月明智?

我只是创建一个关联数组,这样当我们获得月份 ID 时,我们可以从中获得表名,依此类推,我们可以运行我们的查询而不比较任何东西。

$monthName = array(1 => "JANUARY", 2 => "FEBRUARY", 3 => "MARCH", 4 => "APRIL", 5 => "MAY", 6 => "JUNE", 7 => "JULY", 8 => "AUGUST", 9 => "SEPTEMBER", 10 => "OCTOBER ", 11 => "NOVEMBER ", 12 => "DECEMBER");    
<?php   if (isset($_POST['selectmonth'])) {
    $month=$_POST['month'];//name of drop-down
    $rent = $_POST['rent'];//name of textbox
    $table_name_where_data_to_insert = $monthName[$month];
        $sql = "INSERT INTO " . $table_name_where_data_to_insert . " (month, rent) VALUES('$month','$rent')";
        $result = $con->query($sql);
        if ($result === TRUE) {
            echo 'Successfully Inserted';
        } else {
            echo 'There is a problem';
        }
    }}?>

关于php - 如何通过比较PHP中的选项值将下拉值中的值插入数据库表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49704725/

相关文章:

php - 将最高价格的变化设置为 Woocommerce 可变产品中的默认值

PHP PDO lastInsertId 返回字符串而不是整数

php mail onclick on link执行mysql命令

PHP/MySQL - 电子邮件验证不起作用

php - Web 开发人员是否值得学习 C?我会使用它吗?

php - 在 php echo 中重新加载页面不起作用

PHP 数字格式 - 将数字与括号对齐

java - 实体/行仅有时使用 Spring/Hibernate 更新?

PHP for every 在 while 循环中从表中选择

php - Json_encode 二进制字符串