简体   繁体   中英

How to get input type value from a table using php

why $_POST['destination'] does not have a value? Please Help

How to get value from input type

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

Put values on input value="" attribute not in placeholder.

    <?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']; ?>" placeholder=""></td>
    <td><input type="text" name="destination" value="<?php echo $row['destination']; ?>" placeholder=""></td>
    <td><a href="index24.php?delete=<?php echo $row['id']; ?>" class="btn btn-danger"> Edit </a></td>
</tr>

Don't echo out values in placeholder. You can do it in this way:

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


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

“Placeholder is meant to show hints to the users what to type in the field”

EDIT

<?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...

You were getting value of id as string. You need to convert it to int.

I would recommend you to use procedures to prevent sql injection.

Looking at you code snippet, you can handle this scenario in following different ways :

  1. $_POST is part of form handing, so if you need to use $row['destination'] in a different page you need to have it added with in <form> tag and mention the page in 'action' attribute. Read Here
  2. Since you are using $_GET to get the id value in index24.php page you can query to get all the data with WHERE clause.
  3. Use Ajax Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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