我有两页。我将数据发送到第二页的第一个。我用来编辑我的数据库中发送的数据和其他数据的第二页。我呼吁我发送的数据如下:
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
问题是我在页面上有其他数据,每当我编辑任何这些信息时,我希望能够UPDATE query
保存新信息。但是,我的查询不起作用,我认为这是因为我的新输入字段(我没有从第一页发送的数据)数据未被识别。
每当我var_dump值时,我得到的数据(和格式相同)都是我发送到这个页面的方式。
以下是第二页的代码:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
$newId = filter_var($id, FILTER_SANITIZE_STRING);
try {
$host = 'localhost';
$name = '';
$user = '';
$password = '';
$dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);
}catch(PDOException $e) {
echo $e->getMessage();
}
$stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
//print_r($stmt);
while($row = $stmt->fetch()) {
$productAmount = $row['amount'];
$productAvailable = $row['available'];
?>
<form name="update" method="POST">
<input type="text" name="id" value="<?php echo $newId; ?>">
<input type="text" name="first" value="<?php echo $first; ?>">
<input type="text" name="last" value="<?php echo $last; ?>">
<input type="text" name="product" value="<?php echo $product; ?>">
<input type="text" name="amount" value="<?php echo $row['amount']; ?>">
<input type="text" name="available" value="<?php echo $row['available']; ?>">
<button type="submit">Update Product Information</button>
</form>
<?php
}
var_dump($_POST);
if(isset($_POST['update'])) {
$stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");
$stmt->bindParam(1, $_POST['first']);
$stmt->bindParam(2, $_POST['last']);
$stmt->bindParam(3, $_POST['product']);
$stmt->bindParam(4, $productAmount);
$stmt->bindParam(5, $productAvailable);
$stmt->bindParam(6, $newId);
$stmt->execute();
}
?>
当我var_dump($_POST);
时,我会收到以下值,但不包括amount
和available
数据。
array(5) { ["id"]=> string(1) "4" ["first"]=> string(3) "Tom" ["last"]=> string(5) "Brady" ["product"]=> string(3) "Tea" ["edit"]=> string(4) "Edit" }
为什么我的所有数据都没有被识别?
答案 0 :(得分:3)
您的脚本中没有任何地方:
$productAmount = $_POST['amount'];
$productAvailable = $_POST['available'];
在下面添加以下两行:$product = $_POST['product'];
<强>已更新强>
表单应该由数据库填充吗?还是形式本身?如果是数据库(最初),则需要将表单中的所有$变量更改为表中相应的字段名称,即。 $row['id']
,$row['first']
等
请参阅下面的更新...
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];
$productAmount = $_POST['amount'];
$productAvailable = $_POST['available'];
$newId = filter_var($id, FILTER_SANITIZE_STRING);
try {
$host = 'localhost';
$name = '';
$user = '';
$password = '';
$dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);
} catch(PDOException $e) {
echo $e->getMessage();
}
if (isset($_POST['update'])) {
$stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");
$stmt->bindParam(1, $_POST['first']);
$stmt->bindParam(2, $_POST['last']);
$stmt->bindParam(3, $_POST['product']);
$stmt->bindParam(4, $_POST['amount']);
$stmt->bindParam(5, $_POST['available']);
$stmt->bindParam(6, $newId);
$stmt->execute();
}
if ( !empty($newId) ) {
$stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while( $row = $stmt->fetch() ) {
?>
<form action="" method="post">
<input type="text" name="id" value="<?php echo $row['id']; ?>">
<input type="text" name="first" value="<?php echo $row['first']; ?>">
<input type="text" name="last" value="<?php echo $row['last']; ?>">
<input type="text" name="product" value="<?php echo $row['product']; ?>">
<input type="text" name="amount" value="<?php echo $row['amount']; ?>">
<input type="text" name="available" value="<?php echo $row['available']; ?>">
<input type="submit" name="update" value="Update Product Information">
</form>
<?php
}
}
var_dump($_POST);
对于 Undefined index ,只需在表单中使用三元组,如下所示:
... value="<?php echo !empty($row['amount']) ? $row['amount'] : ''; ?>">
... value="<?php echo !empty($row['amount']) ? $row['available'] : ''; ?>">
答案 1 :(得分:3)
更改这些行:
$stmt->bindParam(4, $productAmount);
$stmt->bindParam(5, $productAvailable);
为:
$stmt->bindParam(4, $_POST['amount']);
$stmt->bindParam(5, $_POST['available']);
因此,清理代码的第二部分的结果将是:
name="update"
进入按钮的标记。$_POST
数组现在包含新变量。<form method="POST">
<input type="text" name="id" value="<?php echo $newId; ?>">
<input type="text" name="first" value="<?php echo $first; ?>">
<input type="text" name="last" value="<?php echo $last; ?>">
<input type="text" name="product" value="<?php echo $product; ?>">
<input type="text" name="amount" value="<?php echo $row['amount']; ?>">
<input type="text" name="available" value="<?php echo $row['available']; ?>">
<button type="submit" name="update">Update Product Information</button>
</form>
<?php
}
if(isset($_POST['update'])) { // checking the button
$stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");
$stmt->bindParam(1, $_POST['first']);
$stmt->bindParam(2, $_POST['last']);
$stmt->bindParam(3, $_POST['product']);
$stmt->bindParam(4, $_POST['amount']); // now it is in the POST array
$stmt->bindParam(5, $_POST['available']); // same here
$stmt->bindParam(6, $newId);
$stmt->execute();
}