【问题标题】:having problems making a program with PHP使用 PHP 编写程序时遇到问题
【发布时间】:2017-04-30 19:04:27
【问题描述】:

我创建了两个表单,一个是注册表单,另一个是选择用户来去的日期。我在第二页中调用了用户的用户名.. 虽然我收到了它,但我收到一条消息“错误”并且我的数据库上没有任何更新。这是我的第二页文件。我做错了什么?

<?php
session_start();
$EntryError=$ExitError="";
    if (isset($_POST['submit'])){
        $entrydate = $exitdate = "";
        $errorOccured = false;

        if (isset($_POST['tsmdate'])){
            $entrydate = trim($_POST['tsmdate']);
            if (strlen($entrydate) == 0){
                $EntryError = "date is missing";
                $errorOccured = true;
            }
        }
        else{
            $EntryError = "date is missing";
        }

        // checking for last name
        if (isset($_POST['tsmexit'])){
            $exitdate = trim($_POST['tsmexit']);
            if (strlen($exitdate) == 0){
                $ExitError = "First Name is missing";
                $errorOccured = true;
            }
        }
        else{
            $ExitError = "last Name is missing";
        }
        $ids=$_SESSION['tsmUserName'];
        var_dump($_SESSION);
        if(!$errorOccured){
            require_once("connection.php");
            $my_query="INSERT INTO timing (No, Entry Date and Time, Exit Date and Time, user_id) VALUES (NULL,'$EntryError','$exitdate','$ids')";
            $result=mysqli_query($connection,$my_query);
            if($result)
            {
                echo 'thank you';
            }
            else
            {
                echo 'error';
            }
            mysqli_close($connection);
        }
    }   
?>
<html>
<head>
</head>
<body>
<form name="dates" id="dates" method="POST" action="">
<table cellpadding="5" border="0" width="100%">
            <tr>
                <td colspan="3" align="center">
                <h1> select dates </h1>
                </td>
            </tr>

        <tr>
                <td width="30%" align="right">
                    <label for="tsmdate">Entry date and time</label>
                </td>
                <td align="left">
                    <input type="text" name="tsmdate" id="tsmdate" required="required">
                </td>
        </tr>
        <tr>
            <td width="30%" align="right">
                    <label for="tsmexit">Exit date and time</label>
                </td>
                <td align="left">
                    <input type="text" name="tsmexit" id="tsmexit" required="required">
                </td>
        </tr>

        <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" value="dates">
                </td>
            </tr>
        </table>
</form>
</body>
</html>

【问题讨论】:

  • 您的插入语句无效。
  • 因为您的数据库字段有空格,所以您必须在它们周围加上反引号,就像在@Shri 的回答中一样
  • 您的查询和数据库字段名称中存在明显错误,显示空格无效
  • 好习惯是数据库字段名不要加空格

标签: php html mysql database xampp


【解决方案1】:

由于列名中的空格,您的插入查询不是有效查询,我建议您将空格更改为“_”字符,这样您就不会遇到更多麻烦。如果您想保留空格,则必须使用“`”字符转义列名。

示例

$query="INSERT INTO timing (`No`, `Entry Date and Time`, `Exit Date and Time`, `user_id`) VALUES (NULL, '$EntryError', '$exitdate', '$ids')";

您的表单很容易受到 SQL 注入的影响,为防止这种情况,您必须使用 mysqli::real_escape_string 函数转义变量。

示例

$EntryError = mysqli_real_escape_string($connection, $EntryError);
$exitdate = mysqli_real_escape_string($connection, $exitdate);
$query="INSERT INTO timing (`No`, `Entry Date and Time`, `Exit Date and Time`, `user_id`) VALUES (NULL, '$EntryError', '$exitdate', '$ids')";

【讨论】:

    【解决方案2】:

    将 INSERT 查询更改为此

    $my_query="INSERT INTO timing (`No`, `Entry Date and Time`, `Exit Date and Time`, `user_id`) VALUES (NULL,'$EntryError','$exitdate','$ids')";
    

    确保任何数据库字段名称的名称中都有空格,那么它应该在 ` (back tic) 内

    【讨论】:

    • 除了代码是易受攻击的。您应该真正绑定参数而不是插入 $var;
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 2020-09-15
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多