douqiang4501 2018-04-07 06:48
浏览 72

如何通过与PHP中的选项值进行比较,将值从下拉值插入数据库表?

Table of database

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

Table #2- may

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

HTML form codes

<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> 
";
        } else {
            echo "<option value = " . $mn . ">" . $monthName[$i] . "</option> 
";
        }
    }
    ?>
</select>
<input type="text" name="rent" value="" placeholder="Enter rent input"/>
<input type="submit" name="selectmonth" value=" Go" /></form>

And here php code for insert into

<?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';
    }
}}?>

I want know how to insert in to specific month table with compare to drop-down month value april and may. or Any alternate solution ?

  • 写回答

2条回答 默认 最新

  • dpauxqt1281 2018-04-07 06:57
    关注

    With the below code you don't need to compare any month with the help of month id you can insert data to any table you want but i'm curious to know why you need seprate table of each month instead you can manage all data month wise in single table?

    i'm just creating an associative array so when we get month id we can get the table name from it and so on we can run our query without comparing anything.

    $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';
            }
        }}?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大