gpt4 book ai didi

php - 通过表单更新MySQL数据库

转载 作者:行者123 更新时间:2023-11-30 00:11:43 29 4
gpt4 key购买 nike

enter image description here我正在制作一个表单,可以用它在我的 MySQL 数据库中放入一些内容,但由于某种原因它不起作用。当我尝试提交表单时,它会运行 else 语句,这意味着我收到错误有人能告诉我我做错了什么吗???

我有两个文件,第一个是表单:

        <html>
<body>

<form name="form4" method="post" action="incident_submit.php">

<table>
<tr>
<td>Gebruiker</td>
<td>:</td>
<td>
<?php /* Include de pagina waar je config file staat */

include 'dbconnectie.php'; /* INCLUDE CONNECTION */

$query = $query = "SELECT naam FROM gebruikers"; /* YOUR QUERY */
$uitvoeren = mysqli_query($connectie,$query); /* EXECUTE QUERY */

echo "<select name='naam' id='naam'>";

while($rij = mysqli_fetch_array($uitvoeren)){ /* FETCH ARRAY */
/* De data in de dropdown zetten */
$naam=mysqli_real_escape_string($con,$rij['naam']);
echo "<option value='$naam'>".$naam."</option>";
}
/* De dropdown beëindigen */
echo '</select>';
?>

</td>
</tr>
<tr>
<td>Hardwaretypen</td>
<td>:</td>
<td>
<select name="typen" id="typen"> <?php
$query2 = "SELECT typen FROM hardware_typen"; /* YOUR SECOND QUERY */
/* Query uitvoeren */
$uitvoeren2 = mysqli_query($connectie,$query2); /* EXECUTE SECOND QUERY */
/* Begin van dropdown
While voor data in de dropdown */
while($rij2 = mysqli_fetch_array($uitvoeren2)) { /* SECOND FETCH */
$typen=mysqli_real_escape_string($con,$rij2['typen']); /* ESCAPE STRINGS */
/* De data in de dropdown zetten */
echo "<option value='$typen'>".$typen."</option>";
}
/* De dropdown beëindigen */
echo '</select>';
?>
</td>
</tr>
<tr>
<td>Melding</td>
<td>:</td>
<td><input name="melding" type="text" id="melding"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Incident melden"></td>
</tr>
</table>
</form>
</body>
</html>

第二个文件:

        <?php

include('dbconnectie.php');

/* Get values from form */
$naam=mysqli_real_escape_string($con,$_POST['naam']); /* PRACTICE ESCAPING STRINGS BEFORE USING THEM INTO A QUERY */
$typen=mysqli_real_escape_string($con,$_POST['typen']);
$melding=mysqli_real_escape_string($con,$_POST['melding']);

/* Insert data into mysql */
$sql="INSERT INTO incidenten(naam, typen, melding) VALUES ('$naam', '$typen', '$melding')";
$result=mysqli_query($con,$sql); /* EXECUTE QUERY */

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Uw melding is verwerkt in het systeem.";
echo "<BR>";
echo "<a href='gebruikerpagina.html'>Terug naar de startpagina</a>";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysqli_close($connectie);
?>

我的 MySQL 表 gebruikers:

CREATE TABLE IF NOT EXISTS `gebruikers` (
`naam` text NOT NULL,
`wachtwoord` text NOT NULL,
`level` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `gebruikers` (`naam`, `wachtwoord`, `level`) VALUES
('niek', 'P@password1', 1),
('Hansie', 'Hansie1', 3);

提前致谢!

最佳答案

答案摘要:

  • 将代码从已弃用的 MySQL 转换为 MySQLi
  • 太多包含dbconnectie.php的功能,您只能在页面中使用它一次。
  • 尝试清理 html 表。
  • 在变量上使用mysqli_real_escape_string函数来防止某些SQL注入(inject)。
  • 删除了不必要的和过多的<select>
  • 失踪>
  • 还有重要的是! <option> 中缺少值标签

我在我提供的代码中包含了/* */中引用的解释,以供进一步引用。

dbconnectie.php:

<?php

/* ESTABLISH CONNECTION */

$con=mysqli_connect("localhost","root","","helpdesk_middenpolder");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

?>

您的表单:

<html>
<body>

<form name="form4" method="post" action="incident_submit.php">

<table>
<tr>
<td>Gebruiker</td>
<td>:</td>
<td>
<?php /* Include de pagina waar je config file staat */

include 'dbconnectie.php'; /* INCLUDE CONNECTION */

$uitvoeren = mysqli_query($con,"SELECT * FROM gebruikers"); /* YOUR QUERY */

echo "<select name='naam' id='naam'>";

while($rij = mysqli_fetch_array($uitvoeren)){ /* FETCH ARRAY */
/* De data in de dropdown zetten */
$naam=mysqli_real_escape_string($con,$rij['naam']);
echo "<option value='$naam'>".$naam."</option>";
}
/* De dropdown beëindigen */
echo '</select>';
?>

</td>
</tr>
<tr>
<td>Hardwaretypen</td>
<td>:</td>

<td>
<select name="typen" id="typen">
<?php
$query2 = "SELECT typen FROM hardware_typen"; /* YOUR SECOND QUERY */
/* Query uitvoeren */
$uitvoeren2 = mysqli_query($con,$query2); /* EXECUTE SECOND QUERY */
/* Begin van dropdown
While voor data in de dropdown */
while($rij2 = mysqli_fetch_array($uitvoeren2)) { /* SECOND FETCH */
$typen=mysqli_real_escape_string($con,$rij2['typen']); /* ESCAPE STRINGS */
/* De data in de dropdown zetten */
echo "<option value='$typen'>".$typen."</option>";
}
/* De dropdown beëindigen */
echo '</select>';
?>
</td>

</tr>
<tr>
<td>Melding</td>
<td>:</td>
<td><input name="melding" type="text" id="melding"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Incident melden"></td>
</tr>
</table>
</form>
</body>
</html>

incident_submit.php:

<?php

include('dbconnectie.php');

/* Get values from form */
$naam=mysqli_real_escape_string($con,$_POST['naam']); /* PRACTICE ESCAPING STRINGS BEFORE USING THEM INTO A QUERY */
$typen=mysqli_real_escape_string($con,$_POST['typen']);
$melding=mysqli_real_escape_string($con,$_POST['melding']);

/* Insert data into mysql */
$sql="INSERT INTO incidenten (naam, typen, melding) VALUES ('$naam', '$typen', '$melding')";
$result=mysqli_query($con,$sql); /* EXECUTE QUERY */

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Uw melding is verwerkt in het systeem.";
echo "<BR>";
echo "<a href='gebruikerpagina.html'>Terug naar de startpagina</a>";
}

else {
echo "ERROR";
}

// close connection
mysqli_close($con);
?>

关于php - 通过表单更新MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23989070/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com