[英]Update form, what am I doing wrong?
我为MySQL数据库制作了一个更新表格,但是由于某种原因它没有更新我的数据库。 它可以正常工作,但不会发生任何错误.....有人可以告诉我我在做什么错吗?
***编辑
更新了脚本,但仍然无法正常工作.....
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/layout.css"/>
</head>
<body>
<div id="menu">
<div id="menu_wrapper">
<ul>
<li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
<ul>
<li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
<li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
<li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
</ul>
</li>
</ul>
</div>
</div>
<?php
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);
$id=$_GET['id'];
$q="SELECT * FROM hardware WHERE hardwareID=$id";
$r=mysql_query($q);
echo "<form method='post'>";
echo "<table border='1'>";
echo "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
while ($x=mysql_fetch_array($r)){
echo "<tr>";
echo "<td>";
echo "<input type='text' value='".$x['merknaam']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['producttype']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['hardwaretype']."'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
if(isset($_POST['updatehardware'])){
$query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID=".$id."";
mysql_query($query);
}
?>
<?php
mysql_close($connect);
?>
<input type="submit" name="updatehardware" value="Hardware updaten">
</form>
</body>
</html>
提前致谢!
尝试这个。 在表单中包括文本字段。
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/layout.css"/>
</head>
<body>
<div id="menu">
<div id="menu_wrapper">
<ul>
<li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
<ul>
<li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
<li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
<li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
</ul>
</li>
</ul>
</div>
</div>
<?php
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);
$id=$_GET['id'];
$q="SELECT * FROM hardware WHERE hardwareID=$id";
$r=mysql_query($q);
echo "<form method='post'>";
echo "<table border='1'>";
echo "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
while ($x=mysql_fetch_array($r)){
echo "<tr>";
echo "<td>";
echo "<input type='text' value='".$x['merknaam']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['producttype']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['hardwaretype']."'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connect);
?>
<?php
if(isset($_POST['updatehardware'])){
$query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID='".$id."'";
}
mysql_query($query);
?>
<input type="submit" name="updatehardware" value="Hardware updaten">
</form>
</body>
</html>
并使用$_POST[]
获取输入字段中的值。
您需要将查询更改为POST值:
if(isset($_POST['updatehardware'])){
$query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID='".$id."'";
}
mysql_query($query);
还为您的表单元素设置名称
<input type='text' value='".$x['merknaam']."' name="merknaam">
您正在使用mysql_close($ connect); 并且比尝试更新,执行mysql_close($ connect); 更新后。
此外,您的表单除了“提交”按钮之外,不包括其他字段。
您的表单输入值与以前相同,因此即使您使用这些值进行更新-您也不会看到差异。
您正在发布,但是在这里您从GET $id=$_GET['id'];
,则需要在表单BTW上添加“ id”字段。
更新查询时,请使用POSTed变量,而不要使用从数据库中检索到的值。
所以看看区别:
<?php
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);
$id = $_POST['id'];
$q="SELECT * FROM hardware WHERE hardwareID = $id";
$r=mysql_query($q);
echo "<form method='post'>";
echo "<table border='1'>";
echo "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
while ($x=mysql_fetch_array($r)){
echo "<tr>";
echo "<td>";
echo "<input type='text' value='".$x['merknaam']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['producttype']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['hardwaretype']."'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="hidden" name="id" value="' . $id . '">';
echo '<input type="submit" name="updatehardware" value="Hardware updaten">';
echo "</form>";
if(isset($_POST['updatehardware'])){
$query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID='".$id."'";
}
mysql_query($query);
mysql_close($connect);
?>
还要注意的是使用表内形式是困难的,因为2号答案最好的解释在这里
请记住,如果硬件ID不是varchar,则需要用单引号将varchars和其他类型(数字类型除外)括起来,请执行此操作
$query = "UPDATE hardware SET merknaam='$_POST['merknaam']', producttype='$_POST['producttype']', hardwaretype='$_POST['hardwaretype']' WHERE hardwareID=$id ";
其他
$query = "UPDATE hardware SET merknaam='$_POST['merknaam']', producttype='$_POST['producttype']', hardwaretype='$_POST['hardwaretype']' WHERE hardwareID='$id' ";
您没有在form标记内输入元素。
在输入标签上方移动开始表格标签
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/layout.css"/>
</head>
<body>
<div id="menu">
<div id="menu_wrapper">
<ul>
<li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
<ul>
<li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
<li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
<li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
</ul>
</li>
</ul>
</div>
</div>
<form method="post">
<?php
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);
$id=$_GET['id'];
$q="SELECT * FROM hardware WHERE hardwareID=$id";
$r=mysql_query($q);
echo "<table border='1'>";
echo "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
while ($x=mysql_fetch_array($r)){
echo "<tr>";
echo "<td>";
echo "<input type='text' value='".$x['merknaam']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['producttype']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['hardwaretype']."'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connect);
?>
<?php
if(isset($_POST['updatehardware'])){
$query = "UPDATE hardware SET merknaam='".$x['merknaam']."', producttype='".$x['producttype']."', hardwaretype='".$x['hardwaretype']."' WHERE hardwareID='".$id."'";
}
mysql_query($query);
?>
<input type="submit" name="updatehardware" value="Hardware updaten">
</form>
</body>
</html>
您错误地包含了form
。
输入元素必须被表单元素包围(例如<input />
, <select />
...等)
像这样改变
<form method="post">
<!-- while loop stuff -->
<input type="submit" name="updatehardware" value="Hardware updaten">
</form>
将控制器与视图分开。 至少,将所有PHP代码移到HTML之前。
在输入字段中使用名称,因此您可以轻松区分不同的内容。
名称中使用方括号可提交相同的输入,但可用于不同的行/项目。
总是类型转换值,是整数和浮点数,它们将自动消除输入错误的问题。 如果您没有使用PDO和绑定参数,请使用mysql_real_escape_string
来确保用户不会破坏您的数据库。
将GET和POST保留在函数之外,将它们发送到函数中代码的不同部分。
对于数据库交互,请使用PDO或MySQLi,以避免出现旧问题。
如果不解决第六个问题,我的解决方案将如下所示:
<?php
// This is your "library" of functions
function getConnection() {
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);
return $connect;
}
function endConnection() {
mysql_close($connect);
}
function updateProducts($products) {
$query = '';
foreach ($products as $productId => $productData) {
$query .= "UPDATE hardware SET merknaam='".mysql_real_escape_string($productData['merknaam'])."', producttype='".mysql_real_escape_string($productData['producttype'])."', hardwaretype='".mysql_real_escape_string($productData['hardwaretype'])."' WHERE hardwareID='".(int) $productId."'; ";
}
mysql_query($query);
}
function getProducts($id) {
$q = "SELECT * FROM hardware WHERE hardwareID=$id";
$r = mysql_query($q);
$products = array();
while ($x=mysql_fetch_array($r)) {
$products[] = $x;
}
return $products;
}
// This is your controller
$connect = getConnection();;
if (isset($_POST['products'])) {
updateProducts($_POST['products']);
}
$input_id = (isset($_GET['id']) ? (int) $_GET['id'] : 0);
if ($input_id > 0) {
$products = getProducts($input_id);
}
endConnection();
// This is your view
?><html>
<head>
<link rel="stylesheet" type="text/css" href="css/layout.css" />
</head>
<body>
<div id="menu">
<div id="menu_wrapper">
<ul>
<li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
<ul>
<li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
<li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
<li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
</ul>
</li>
</ul>
</div>
</div>
<form method="get">
<h2>Get product by ID</h2>
<div>
<label for="productId">ID</label> <input id="productId" type="text" name="id" value="<?php echo ($input_id > 0 ? $input_id : ''); ?>" />
</div>
<input type="submit" value="Get item" />
</form>
<?php if (isset($products) && !empty($products)): ?>
<h2>Products found by ID</h2>
<form method="post">
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>merknaam</th>
<th>producttype</th>
<th>hardwaretype</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $product): ?>
<tr>
<td><?php echo $product['hardwareID']; ?></td>
<td><input type="text" name="product[<?php echo $product['hardwareID']; ?>][merknaam]" value="<?php echo $product['merknaam']; ?>" /></td>
<td><input type="text" name="product[<?php echo $product['hardwareID']; ?>][producttype]" value="<?php echo $product['producttype']; ?>" /></td>
<td><input type="text" name="product[<?php echo $product['hardwareID']; ?>][hardwaretype]" value="<?php echo $product['hardwaretype']; ?>" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="submit" value="Hardware updaten" />
</form>
<?php endif; ?>
</body>
</html>
尚未测试此代码,但它应该使您理解应该开始的方向,应该完成的过程,或者至少是路径。
尝试编写简洁的代码,不要害怕使用函数和类,并始终从错误中学习。
代码中有很多错误:
<form>
标签一起放置 UPDATE
查询中使用$x['merknaam']
,请使用$_POST['merknaam']
(其他字段相同); 提防SQL注入。 hardwareID
可能是一个INT
列; 不需要单引号将UPDATE查询中的值引起来 $id = $_GET['id']
附近有逻辑错误; 将表单发布为POST
方法将丢失$id
; 再次进行 SQL注入。 在表单标签内添加<input type="hidden" name="id" value="<?php echo $id; ?>" />
mysql_*
库; 请改用PDO / MySQLi。 <title>
标签在哪里? 还有整个<head>
! border='1'
已弃用; 改用CSS mysql_close()
mysql_query()
之后执行mysql_query()
mysql_close()
,这将导致错误 ini_set('display_errors', '1');
将其重新打开ini_set('display_errors', '1');
mysql_connect()
的返回值来检查mysql连接是否成功建立 name
,但是可以使用它。 <th>
没有被<tr>
包围 修复这些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.