时间未插入数据库表

时间:2014-06-03 00:52:20

标签: php html mysql

我有一个我正在尝试制作的网络表格,其中一个人能够填写所述人正在接收的咨询信息。我知道如何花时间处理文本字段,但出于某种原因,当我将其更改为下拉菜单并更改其插入数据库的方式时,它不再有效。有什么想法吗?这就是php的样子。好像。

//if submit is pressed
        if(isset($_POST['submit'])) {

                //Create empty error array
                $error = array();

                //Check for names
                if(empty($_POST['providerName'])) {
                    $error['providerName'] = 'Please enter your name';
                }

                if(empty($_POST['clientName'])) {
                    $error['clientName'] = 'Please select the client\'s name';
                }

                //Check for services
                if(empty($_POST['service'])) {
                    $error['service'] = 'Please select a service';
                }

                if(empty($_POST['receiverGroup'])) {
                    $error['receiverGroup'] = 'Please select who is receiving the service';
                }

                //Check for time
                if(empty($_POST['hours'])) {
                    $error['hours'] = 'Please enter a value for number of hours';
                }

                if(empty($_POST['minutes'])) {
                    $error['minutes'] = 'Please enter a value for number of minutes';
                }

                //Check for method
                if(empty($_POST['method'])) {
                    $error['method'] = 'Please select a method';
                }

                //If there are no errors
                if(sizeof($error) == 0) 

                    {

                    $i = 0;
                    $temp = $_POST['receiverGroup'];
                    foreach($temp as $each) {
                        if($i==0){
                            $receiver = $each;
                        }else{
                            $receiver = $receiver + " , " + $each;
                        }
                        $i++;
                    }

                    $elapsedTime = $_POST['hours'] + ($_POST['minutes']/60);



                    //Insert a record into the database
                    $query = "INSERT INTO dosage (
                        entry_id,
                        providerName,
                        clientName,
                        services,
                        receiver,
                        elapsedTime,
                        method,
                        activity_date
                    ) VALUES (
                        null,
                        '{$_POST['providerName']}',
                        '{$_POST['clientName']}',
                        '{$_POST['service']}',
                        $receiver,
                        $elapsedTime,
                        '{$_POST['method']}',
                        NOW()
                    )";

                    //echo $query;

                    mysqli_query($dbc, $query) or die('Query failed: ' . mysqli_error($dbc)); 

                    /* $result = mysqli_query($dbc, $query) or die('Query failed: ' . mysqli_error($dbc)); */

                    //Display a confirmation
                    echo "<div class=\"alert alert-success\">Thank You. Your entry has been submitted.</div>";

                } else {

                        foreach($error as $value) 
                        {
                                echo "<div class=\"alert alert-danger\">";
                                echo $value;
                                echo "</div>";
                        }
                }
            }

这是HTML

    

            <div class="col-md-6 col-xs-12 background"> 
            <label class="col-md-12 col-xs-12 color h4">Total Time Providing Services (Hours, Minutes)</label><br />
            <select name = "hours">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
            </select>

            <select name = "minutes">
                <option value=":00">:00</option>
                <option value=":01">:01</option>
                <option value=":02">:02</option>
                <option value=":03">:03</option>
                <option value=":04">:04</option>
                <option value=":05">:05</option>
                <option value=":06">:06</option>
                <option value=":07">:07</option>
                <option value=":08">:08</option>
                <option value=":09">:09</option>
                // ...

表格

CREATE TABLE IF NOT EXISTS `dosage` 
( `entry_id` int(11) NOT NULL AUTO_INCREMENT, 
`providerName` text NOT NULL, 
`clientName` text NOT NULL, 
`services` text NOT NULL,
 `receiver` text NOT NULL,
 `elapsedTime` text NOT NULL,
 `method` text NOT NULL, 
`activity_date` datetime NOT NULL, PRIMARY KEY (`entry_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。首先,最重要的是,您的代码容易受到SQL注入攻击,因为您正在将值合并到SQL查询中。相反,您应该使用带参数的预准备语句。

关于你的问题,

$elapsedTime = $_POST['hours'] + ($_POST['minutes']/60);

您的minutes变量如下所示::00:09等。您正在尝试将字符串(前导冒号(:)除以60。删除该冒号。

此外,您值添加到一起,而不是连接。如果要连接字符串,请使用句点(.)而不是加法运算符(+

$elapsedTime = $_POST['hours'] . ($_POST['minutes']/60);

这仍然无法解决您遇到的另一个问题 - 小数结果给您的部门。例如,如果您传入minute 10值(假设您删除了前导冒号),那么您现在(假设小时为01

$elapsedTime = "01" . ":" .  (10/60);

您的elapsedTime变量如下:01:0.16666666666667。那是无效的。

您拥有的最佳选择可能是修改数据库结构本身。您可以将经过的时间存储为一个简单的整数,并执行所需的数学运算,以便在存储之前以分钟为单位获得正确的时间:

$elapsedTime = ($hours * 60) + $minutes;

这提供了一个简单的数值,可用于在数据库中执行数学运算(即MAX()MIN()SUM())。使用TEXT数据类型无法完成这些操作。它还允许您通过颠倒上述公式轻松地将BACK转换为您的HH:MM格式:

$original_hours = (int)($elapsedTime / 60);
$original_min = $elapsedTime % 60;