首页 > 解决方案 > 如何使用php从表中获取输入类型值

问题描述

为什么 $_POST['destination'] 没有值? 请帮忙

如何从输入类型中获取值

<?php while ($row = mysqli_fetch_array($result)){ ?>
    <tr>
        <td><?php echo $row['id']; ?></td>
        <td><input type="number" name="day" placeholder="<?php echo $row['day']; ?>"></td>
        <td><input type="text" name="destination" placeholder="<?php echo $row['destination']; ?>"></td>
        <td><a href="index24.php?delete=<?php echo $row['id']; ?>" class="btn btn-danger"> Edit </a></td>
    </tr>
<?php } ?>

//index24.php
$db = mysqli_connect("localhost", "root", "", "testing");
if (isset($_GET['delete'])){
    $id = $_GET['delete'];
    $destination = $_POST['destination']; //why $_POST['destination'] does not have a value?
    $day = $_POST['day'];
    $query = "UPDATE tbl_itinerary SET destination = '$destination', day = '$day' WHERE id =  '" . $id . "'";
    mysqli_query($db,$query);
    header('location: index4.php');
}

标签: php

解决方案


不要在占位符中回显值。你可以这样做:

value="<?php echo $row['day']; ?>


Not like this placeholder="<?php echo $row['day']; ?>"

“占位符旨在向用户显示在字段中输入内容的提示”</p>

编辑

<?php while ($row = mysqli_fetch_array($result)){ ?>
    <tr>
        <td><?php echo $row['id']; ?></td>
        <td><input type="number" name="day" value="<?php echo $row['day']; ?>"></td>
        <td><input type="text" name="destination" value="<?php echo $row['destination']; ?>"></td>
 <td><a href="index24.php?delete=<?php echo $row['id']; ?>" class="btn btn-danger"> Edit </a></td>
    </tr>
<?php } ?>

//index24.php
//covertion is important here

$id = intval($_GET['delete']);
$db = mysqli_connect("localhost", "root", "", "testing");
//submit - would be the name of button in your <form> tag.

if (isset($_GET['submit'])){

$destination = $_POST['destination'];
$day = $_POST['day'];

$query = "UPDATE tbl_itinerary SET destination = '$destination', day = '$day' WHERE id =  '" . $id . "'";
   mysqli_query($db,$query);
   header('location: index4.php');
}
Maybe this will help you...

您将 id 的值作为字符串。您需要将其转换为 int。

我建议您使用程序来防止 sql 注入。


推荐阅读