Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果我的帖子是空的,我如何用我的数据库阻止更新?_Php_Mysql_Post_Is Empty - Fatal编程技术网

Php 如果我的帖子是空的,我如何用我的数据库阻止更新?

Php 如果我的帖子是空的,我如何用我的数据库阻止更新?,php,mysql,post,is-empty,Php,Mysql,Post,Is Empty,我在phpmyadmin数据库上的mysql有一个问题,我想更新我的数据库,例如更改用户名,但是用我现有的代码,如果我留下一些空的东西,它会用空值更新数据库,我想更改这个问题,如果帖子是空的,它不会将数据库更新为空空间 这是我的密码: <!doctype html> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "eerstedatabase"; //create

我在phpmyadmin数据库上的mysql有一个问题,我想更新我的数据库,例如更改用户名,但是用我现有的代码,如果我留下一些空的东西,它会用空值更新数据库,我想更改这个问题,如果帖子是空的,它不会将数据库更新为空空间

这是我的密码:

<!doctype html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "eerstedatabase";

//create connection
$connection = new mysqli($servername, $username, $password, $dbname);

if ($_POST) {

//check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}
//if these posts are empty it updates them to empty in the database aswell
$sql = "UPDATE gebruikers SET Gebruikersnaam='" . $_POST['Gebruikersnaam'] . "',
Wachtwoord='" . $_POST['Wachtwoord'] . "',
Email='" . $_POST['Email'] . "' 
WHERE ID='" . $_POST['ID'] . "' ";

if ($connection->query($sql) === TRUE) {
    echo "Record updated successfully";

    include 'Opdracht1.php';
} else {
    echo "Error updating record: " . $conn->error;
}
}
else
{

?>
<html>
<head>
<meta charset="utf-8">
<title>Naamloos document</title>
</head>

<body>
<center>
<table>
<form name="update" action="OpdrachtDW6.php" method="POST">
  <tr>
    <td>ID</td>
    <td><input type="text" name="ID" rquired /></td>
  </tr>
  <tr>
    <td>Gebruikersnaam</td>
    <td><input type="text" name="Gebruikersnaam" required /></td>
  </tr>
  <tr>
    <td>Wachtwoord</td>
    <td><input type="text" name="Wachtwoord" required /></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input type="text" name="Email" required /></td>
  </tr>
  <tr>
    <td><input type="submit" value="Updaten" /></td>
  </tr>
  </td>
</form>
</center>
</body>
</html>

<?php
}
?>

Naamloos文件
身份证件
格布鲁伊克斯纳姆酒店
瓦赫特伍德
电子邮件

您需要检查服务器端的$\u POST字段的值

$errs = false;

if ($_POST['ID'] == "") { 
   $errs = true;
   echo "ID Field Missing";
}
if ($_POST['Gebruikersnaam'] == "") { 
   $errs = true;
   echo "Gebruikersnaam Field Missing";
}

/* And so on... */

if (!$errs) {
    //check connection
    if ($connection->connect_error) {
       die("Connection failed: " . $connection->connect_error);
    }

    $sql = "UPDATE gebruikers SET Gebruikersnaam='" . $_POST['Gebruikersnaam'] . "',
    Wachtwoord='" . $_POST['Wachtwoord'] . "',
    Email='" . $_POST['Email'] . "' 
    WHERE ID='" . $_POST['ID'] . "' ";

    if ($connection->query($sql) === TRUE) {
       echo "Record updated successfully";

       include 'Opdracht1.php';
    } else {
       echo "Error updating record: " . $conn->error;
    }
}

您可以检查POST值是否为空:

if(empty($_POST['Email'])
    // Do something
还有其他选项,如检查字符串的长度,如果名称太短,则可能不可接受。希望对您有所帮助。

您可以使用此逻辑

if(isset($_POST['Email'])){
    //post email is not empty! and you can insert in database!
    if ($connection->query($sql) === TRUE) {
        echo "Record updated successfully";
        include 'Opdracht1.php';
    } else {
        echo "Error updating record: " . $conn->error;
    }
}
下面是检查(非)空字段的(基本)逻辑

旁注:请参阅代码中的注释以根据需要进行处理

if( !empty($_POST['ID']) 

 && !empty($_POST['Gebruikersnaam']) 
 && !empty($_POST['Wachtwoord']) 
 && !empty($_POST['Email']) 

  )

 {

 // Execute the query

 }

 else {

 // kill the process or do something else

 }
如果您只想在电子邮件为空时执行更新,则只需执行以下操作:

if(!empty($_POST['Email'])) {

    $sql = "UPDATE gebruikers ...

    // Rest of your code

}
有关PHP的逻辑运算符,请参阅以下内容:

empty()


脚注:

您当前的代码对用户开放。使用,或与

另外,在执行更新并检查更新是否确实成功时,请使用
mysqli\u infected\u rows()

因为
如果($connection->query($sql)==TRUE)
可能会给您一个假阳性

您还可以更改列以不接受空值

咨询

并使其/它们不为空


在添加的注释中,此
if($\u POST){
可以更改为
if(isset($\u POST['submit']){
,并将
submit
name属性添加到提交按钮

<input type="submit" name="submit" value="Updaten" />

我之前所做的是将查询构建到变量中,然后如果变量为空,它将跳过查询的该部分

比如说

if($_POST['fielda']) {
    $fieldasql = "fielda = '".$_POST['fielda']."',";
} else {
    $fieldasql = "";  
}

if($_POST['fieldb']) {
    $fieldbsql = "fieldb = '".$_POST['fieldb']."',";
} else {
    $fieldbsql = "";  
}
那么您的查询将是:

$sql = "UPDATE gebruikers SET " . $fieldasql . " " . $fieldbsql;

您可以使用此代码进行SQL注入。
if(condition){do something}else{well,do something}
使用可选的
和|或
。您还可以更改列,使其不接受空值。
如果为空($var)和|或为空($var)和|或为空($var)
等等,如果不为空,则使用该列的相反的形式($var)和| OR not empty($var)和| OR not empty($var)。现在,您已经了解了如何构建它的基本结构。请参阅手册@Fred ii-我是通过phpmyadmin来实现的吗?还是在php代码中实现的?这是我想要做的,但是,如果(empty($\u POST['Email'),我如何告诉php代码不要更新)不要更新Eyes,但是,这意味着您需要填写这些表单,我希望它是,如果您不填写这些表单,那么代码不会更新数据库,您可以在HTML代码中按要求设置它们。大多数web浏览器无论如何都不会给用户选择权。您必须从表单字段中删除
必需的
。如果您希望将其删除在没有错误消息的情况下失败,只需删除向用户反映错误的行。谢谢您的帮助,我正在尝试这个atm,这不会更新我数据库中的字段,如果它们是empty@JakeImhoff完全正确,不客气。在附加说明中,正如我在评论中所发布的,您也可以更改您的专栏不接受空值,只处理级别错误。另外,我将添加到我的答案中的一点是,如果更新真的成功,请使用
mysqli\u impacted\u rows()
以确保真实性。编辑旁注:我感谢对我的答案所做的编辑,但这很好,谢谢。我将编辑第一行;-)@JakeImhoff那么,你过得怎么样?我也对我的答案做了一些编辑,所以你可能想重新加载它。