我对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
}
?>
答案 0 :(得分:1)
这是检查(不)空字段的(基本)逻辑。
旁注:查看代码中的注释以根据需要进行处理。
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()
:
脚注:
您目前的代码向SQL injection开放。使用prepared statements或PDO与prepared statements。
此外,在进行更新并检查真正是否成功时,请使用mysqli_affected_rows()
:
因为if ($connection->query($sql) === TRUE)
会给你误报。
您也可以将列更改为不接受NULL值。
咨询http://dev.mysql.com/doc/refman/5.7/en/working-with-null.html
并将它们设为NOT NULL。
在添加的备注中,此if ($_POST) {
可以更改为if(isset($_POST['submit'])){
并将submit
名称属性添加到您的提交按钮。
<input type="submit" name="submit" value="Updaten" />
答案 1 :(得分:0)
您需要检查服务器端$ _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;
}
}
答案 2 :(得分:0)
您可以检查POST值是否为空:
if(empty($_POST['Email'])
// Do something
还有其他选项,例如检查字符串的长度,如果名称太短,可能是不可接受的。希望这有帮助。
答案 3 :(得分:0)
您可以使用此逻辑
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;
}
}
答案 4 :(得分:0)
我之前所做的是构建我的查询变量,然后如果变量为空,它将跳过查询的那一位。
例如
if($_POST['fielda']) {
$fieldasql = "fielda = '".$_POST['fielda']."',";
} else {
$fieldasql = "";
}
if($_POST['fieldb']) {
$fieldbsql = "fieldb = '".$_POST['fieldb']."',";
} else {
$fieldbsql = "";
}
然后你的查询将是:
$sql = "UPDATE gebruikers SET " . $fieldasql . " " . $fieldbsql;