数据库中的空值

编程入门 行业动态 更新时间:2024-10-25 20:28:51
本文介绍了数据库中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我的 PHP:

我的 HTML:

<table class="#"><tr><th colspan="2">Test</th></tr><tr><td>电子邮件地址:</td><td><input type="text" name="email"></td></tr><tr><td>类型:</td><td><input type="text" name="type"></td></tr><tr><td>猫:</td><td><input type="text" name="cats"></td></tr><tr><td></td><td><input type="submit" value="upload" name="upload"></tr></表单>

我的 SQL 配置:

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

解决方案

您的列名包含大小写混合的字母(Cats 和 cats 不一样)

我编辑了我的答案,从那里我改变了它:

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

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

我也犯了一个错误,因为 if(empty($_POST['email'] 缺少 ) 已修复.

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

您的表的原始结构:(

请参阅以下代码中的其余部分.

正如我在您的原始问题下的评论中所述,已为您整理了这些内容.

  • 不要使用这种方法 VALUES ('$_POST[email]','$_POST[type]','$_POST[cats]') 你愿意接受SQL 注入

  • 为避免重新提交导致空条目,您可以使用 header() 重定向到另一个页面,或使用 AJAX

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

即:代替 echo 添加 1 条记录";

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

您也可以使用条件语句:

if(empty($_POST['variable'])){ die(填写");}

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

另外,我修改了您的查询方式,将 POST 变量替换为 mysqli_real_escape_string()

脚注:

只是说,以下:

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

应该是:

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

但是,正如我已经提到的,强烈不鼓励使用这种方法.

I am trying to make a VERY simple PHP form that posts a form to MySQL Database, however I am having some issues, and would welcome a simple fix for this if possible:

My 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); ?>

My 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>

My SQL Configuration:

Even though I have not null set in the DB I am getting empty results, is it possible to stop the form resubmitting on refresh causing null results be entered into the DB. I will enter some form validation to stop null results passing into the post script in the future but refreshing the page still sends over null results.

解决方案

Edit:

Your column names have mixed-case letters (Cats and cats are not the same)

I edited my answer, where I changed it from:

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

to

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

I also made a mistake with a missing ) for if(empty($_POST['email'] which has been fixed.

Please make sure also, that your column names are indeed called Email Type Cats and not email type cats Change it to the letter-case that is in your DB.

Your table's original structure: (larger image)

  • You should have talked to me first, instead of posting a new question with my code

See the rest below in the code.


As I stated in my comments under your original question, have put this together for you.

  • Don't use this method VALUES ('$_POST[email]','$_POST[type]','$_POST[cats]') you're open to SQL injection

  • To avoid re-submissions causing an empty entry, you can use a header() to redirect to another page, or use AJAX

However, I am sure there are other ways of doing this in the query itself, I just don't remember how right now.

I.e.: In place of where you have echo "1 record added";

you can do header("Location: added.php"); exit();

You can also use a conditional statement:

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

Try the following. It will check for empty fields, as well as check if the upload submit-type button is set.

Plus, I modified the way your query was done, replacing POST variables with mysqli_real_escape_string()

<?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); ?>


Footnotes:

Just saying, the following:

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

should have been:

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

However, using this method is highly discouraged, as I already mentioned.

更多推荐

数据库中的空值

本文发布于:2023-10-25 22:23:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!