DB中的空值

时间:2014-01-06 21:57:35

标签: php mysql sql

我正在尝试制作一个非常简单的PHP表单,将表单发布到MySQL数据库,但是我遇到了一些问题,如果可能的话,欢迎对此进行简单修复:

我的PHP:

<?php
$con=mysqli_connect("serveraddress","db","password","dbname");
// Check connection

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO Persons (email, type, cats)
VALUES
('$_POST[email]','$_POST[type]','$_POST[cats]')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

我的HTML:

<form action="personuploader.php" method="post">

        <table class="#"> 

            <tr>

                <th colspan="2">Test</th>

            </tr>

            <tr>

                <td>Email Address:</td>

                <td><input type="text" name="email"> </td>

            </tr>

            <tr>

                <td>Type:</td>

                <td><input type="text" name="type"> </td>

            </tr>

            <tr>

                <td>Cats:</td>

                <td><input type="text" name="cats"> </td>

            </tr>

            <tr>

                <td></td>

                <td><input type="submit" value="upload" name="upload">

            </tr>

        </table>  

    </form>

我的SQL配置:

SQL

即使我没有在数据库中设置空值我得到空结果,是否可以停止表单重新提交刷新导致空结果输入数据库。我将输入一些表单验证以阻止将来的结果传递到post脚本,但刷新页面仍会发送空结果。

3 个答案:

答案 0 :(得分:1)

编辑:

您的列名称是大写字母(Catscats不一样)

我编辑了我的答案,我将其改为:

$sql="INSERT INTO `Persons` (`email`, `type`, `cats`)

$sql="INSERT INTO `Persons` (`Email`, `Type`, `Cats`)

我还错过了) if(empty($_POST['email']已修复的问题。

请确保,您的列名称确实称为Email Type Cats而不是email type { {1}}将其更改为数据库中的字母大小写。

你桌子的原始结构:(larger imageenter image description here


请参阅下面的代码中的其余部分。


正如我在原始问题的评论中所说的那样,已经把它放在一起。

  • 请勿使用cats

  • 开放的VALUES ('$_POST[email]','$_POST[type]','$_POST[cats]')方法
  • 为避免重新提交导致空条目的问题,您可以使用SQL injection重定向到其他网页,或使用header()

但是,我确信在查询本身中还有其他方法可以做到这一点,我现在还不记得了。

即:代替echo "1 record added";

你可以header("Location: added.php"); exit();

您还可以使用条件语句:

if(empty($_POST['variable'])){ die("Fill this in.");}

请尝试以下操作。它会检查空字段,并检查是否设置了upload提交类型按钮。

另外,我修改了查询的完成方式,用AJAX替换POST变量

<?php
$con=mysqli_connect("serveraddress","db","password","dbname");
// Check connection

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if(isset($_POST['upload'])){

// You can replace the || with && if required
// depending on what you want to check for.
    if(empty($_POST['email']) || empty($_POST['type']) || empty($_POST['cats']))
    {
    die("You need to fill in all the fields.");
    }

$email = mysqli_real_escape_string($con, $_POST['email']);
$type = mysqli_real_escape_string($con, $_POST['type']);
$cats = mysqli_real_escape_string($con, $_POST['cats']);

$sql="INSERT INTO `Persons` (`Email`, `Type`, `Cats`) 
VALUES ('$email','$type','$cats')";

    if (!mysqli_query($con,$sql))
    {
    die('Error: ' . mysqli_error($con));
    }

// Uncomment out if you're going to use echo, but not with header.
// echo "1 record added";

header("Location: redirect_to_other_page.php");
exit();

} // end of if(isset($_POST['upload']

// else conditional statement for if(isset($_POST['upload']
else{ echo "You cannot do this operation from here."; }

mysqli_close($con);
?>

<强>脚注:

只是说,以下内容:

('$_POST[email]','$_POST[type]','$_POST[cats]')

应该是:

('$_POST['email']','$_POST['type']','$_POST['cats']')

然而,正如我已经提到的,使用这种方法非常气馁

答案 1 :(得分:0)

您需要检查提交是否确实发生:

if ($_SERVER["REQUEST_METHOD"] == 'POST') {
    ... submit occured, do DB stuff
}

请注意,空字符串与SQL null不同。空字符串就是那个 - 一个恰好是空/零长度的字符串。 SQL null实际上是“未知”。您的代码无法插入实际的null - 它只能插入空字符串。

答案 2 :(得分:0)

您应该检查“personuploader.php”文件中是否已单击“上传”按钮。

// Initializing Variables
$email = '';
$type  = '';
$error = false;    

if ( isset ( $_POST['upload'] ) ) {

    // Then capture the POST Variables
    $email = $_POST['email'];

    // Do Some Validations
    if ($email == '') {
        $error = true;
    }

    // Process the Form if NO Errors

    if ($error == false) {

        // Insert The Data into DB
    }

}
相关问题