简体   繁体   English

表单值不发布

[英]Form values not posting

I have two pages. 我有两页。 The first one I am sending data over to the second page. 我将第一个数据发送到第二页。 The second page I am using to edit the data sent over and other data within my database. 我正在使用第二页来编辑通过数据库发送的数据和其他数据。 I call for the data I sent over like this: 我需要这样发送的数据:

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

The issue is I have other data on the page that and whenever I edit any of this information, I want to be able to do an UPDATE query to save the new information. 问题是页面上还有其他数据,每当我编辑任何此信息时,我都希望能够执行UPDATE query以保存新信息。 However, my query isn't working and I believe it is because my new input field's (data I didn't send over from the first page) data is not being recognized. 但是,我的查询无法正常运行,我相信这是因为无法识别新输入字段(不是我从第一页发送过来的数据)的数据。

Whenever I var_dump the values, I get the data (and in the same format) as how I sent it over to this page. 每当我var_dump值时,我都会获得数据(格式相同),就像我将其发送到此页面一样。

Here is my code for the second page: 这是我第二页的代码:

<?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();
}

?>

When I do var_dump($_POST); 当我做var_dump($_POST); I get the following values, clearly not including the amount and available data. 我得到以下值,显然不包括amountavailable数据。

array(5) { ["id"]=> string(1) "4" ["first"]=> string(3) "Tom" ["last"]=> string(5) "Brady" ["product"]=> string(3) "Tea" ["edit"]=> string(4) "Edit" }

Why isn't all my data being recognized? 为什么无法识别我的所有数据?

Nowhere in your script do you have: 您的脚本中无位置:

$productAmount = $_POST['amount'];
$productAvailable = $_POST['available'];

Add those 2 lines just below: $product = $_POST['product']; 在下面添加这两行: $product = $_POST['product'];

UPDATED 更新

Is the form supposed to be populated by the database? 表单应该由数据库填充吗? Or the form itself? 还是表格本身? If by the database (initially), then you'll need to change all the $variables in the form to their corresponding field names from the table, ie. 如果是通过数据库(最初),那么您需要将表格中的所有$ variables更改为表中它们对应的字段名称,即。 $row['id'] , $row['first'] , etc. $row['id']$row['first']等。

See below for update... 请参阅下面的更新...

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

For the Undefined index just use a ternary in your form like so: 对于未定义索引,只需在表单中使用三元,如下所示:

... value="<?php echo !empty($row['amount']) ? $row['amount'] : ''; ?>">
... value="<?php echo !empty($row['amount']) ? $row['available'] : ''; ?>">

Change these lines: 更改这些行:

$stmt->bindParam(4, $productAmount);
$stmt->bindParam(5, $productAvailable);

to: 至:

$stmt->bindParam(4, $_POST['amount']);
$stmt->bindParam(5, $_POST['available']);

So the result of cleaning up second part of your code would be this: 因此,清理代码第二部分的结果将是:

  • No need for a form name. 无需表单名称。
  • name="update" goes in the button's markup. name="update"放在按钮的标记中。
  • $_POST array now contains the new variables. $_POST数组现在包含新变量。
  • We check to make sure update is set before running the query. 在运行查询之前,我们将检查以确保已设置更新。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM