通过表单更新MySQL数据库

时间:2014-06-02 07:12:38

标签: php mysql forms

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

提前致谢!

4 个答案:

答案 0 :(得分:0)

尝试

$sql="INSERT INTO {$tbl_name}(naam, typen, melding)VALUES('$naam', '$typen', '$melding')";
$result=mysql_query($sql) or die(mysql_error()); // will display the error

当添加 - 或死(mysql_error())时,它将显示与MySql引擎相关的错误(例如错误的用户名和密码,拼写错误等)

答案 1 :(得分:0)

添加名称和ID以选择naam和typen

echo '<select id="naam" name="naam">';//add name and id
//While voor data in de dropdown
while($rij = mysql_fetch_assoc($uitvoeren)) {
//De data in de dropdown zetten
echo '<option>'.$rij['naam'].'</option>';
}
//De dropdown beëindigen
echo '</select>';

在这里

echo '<select id="typen" name="typen">';//add name and id
//While voor data in de dropdown
while($rij = mysql_fetch_assoc($uitvoeren)) {
//De data in de dropdown zetten
echo '<option>'.$rij['typen'].'</option>';
}
//De dropdown beëindigen
echo '</select>';?>

答案 2 :(得分:0)

将您的表单选择更改为(您还有一个选择选项)也不需要包含两次文件: -

<td>
<?php //Include de pagina waar je config file staat
include 'dbconnectie.php';
//Query
$query = "SELECT naam FROM gebruikers WHERE level='3'";
//Query uitvoeren
$uitvoeren = mysql_query($query);
?>
<select name="naam" id="naam" >
<?php while($rij = mysql_fetch_assoc($uitvoeren)) {?>
  <option value="<?php echo $rij['naam'];?>"><?php echo $rij['naam'];?></option>
<?php }?>
</select></td>
</tr>
<tr>
<tr>
<td>Hardwaretypen</td>
<td>:</td>
<td>
<?php $query = "SELECT typen FROM hardware_typen";
//Query uitvoeren
$uitvoeren = mysql_query($query);?>
<select name="typen" id="typen">
<?php while($rij = mysql_fetch_assoc($uitvoeren)) {?>
<option value="<?php echo $rij['typen'];?>"><?php echo $rij['typen'];?></option>
<?php }?>
</select></td>

然后将查询插入: -

$sql="INSERT INTO incidenten (naam, typen, melding)VALUES('$naam', '$typen', '$melding')";

答案 3 :(得分:0)

答案摘要:

  • 将已弃用的 MySQL 转换为 MySQLi
  • 太多包含 dbconnectie.php 的功能,你可以在页面中使用它一次。
  • 试图清理html表。
  • 对变量使用 mysqli_real_escape_string 函数以防止某些SQL注入。
  • 删除了不必要且过多的<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);
?>