使用PHP post从HTML表中获取数据

时间:2016-05-02 14:23:08

标签: php post html-table

   <form action="book.php" method="post">
   <table>
    <thead>
    <tr>
    <td>FlightID</td>
    <td>From</td>
    <td>Destination</td>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td name="flightID" value="1">1</td>
    <td name="From" value="Sydney">Sydney</td>
    <td name="Destination" value="Bali">Bali</td>
    <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
    </tr>

    <tr>
    <td name="flightID" value="2">2</td>
    <td name="From" value="London">London</td>
    <td name="Destination" value="HongKong">Hong Kong</td>
    <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
    </tr>

    </tbody>
</table>
</form>

我创建了一个这样的表。在每行的末尾,它有一个书本按钮。

我要做的是当用户点击按钮时,所选行数据(ID,From,Des)将传递给'book.php',然后PHP文件将执行剩下的工作。 但我尝试使用'book.php'中的$ _POST ['name']来捕捉价值,就像这样

<?php
    if(isset($_POST['booking'])){
    $ID = $_POST['flightID'];
    $From = $_POST['From'];
    $To = $_POST['Destination'];
}
?>

它显示所有这些值都是未定义的。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

问题是<td>中的值不能自己从表单传递到PHP文件。你可以使用隐藏的输入。此外,表格中的每一行都应该是自己的表格,以确保所有数据不会同时提交。

试试这个:

<table>
<thead>
    <tr>
        <td>FlightID</td>
        <td>From</td>
        <td>Destination</td>
    </tr>
</thead>
<tbody>
    <tr>
        <form action="book.php" method="post">
            <td><input type="hidden" name="flightID" value="1">1</td>
            <td><input type="hidden" name="From" value="Sydney">Sydney</td>
            <td><input type="hidden" name="Destination" value="Bali">Bali</td>
            <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
        </form>
    </tr>

    <tr>
        <form action="book.php" method="post">
            <td><input type="hidden" name="flightID" value="2">2</td>
            <td><input type="hidden" name="From" value="London">London</td>
            <td><input type="hidden" name="Destination" value="HongKong">Hong Kong</td>
            <td class="tdBook"><button class="btnBook" type=submit name="booking"> Book </button>
        </form>
    </tr>

</tbody>

答案 1 :(得分:0)

我遇到了与您相同的问题,并试图创建一个答案,所以我想出了这段代码,使用循环使用特殊名称来指示HTML表格中的每一行,我现在可以接受指定的行并执行尽可能多的PHP我可以在不干扰整个表的情况下进行操作,并且它与数据库很好地同步,希望对您有所帮助!

整个“用特殊名称标记每一行”代码位于 usersTable.php

users.sql

create table users(
    id int,
    username varchar(50),
    password varchar(50)
);

users.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "wdl2hw4db";

$conn = mysqli_connect($host, $username, $password, $database);

if (mysqli_connect_errno()){
    die("can't connect to the Database" . mysqli_connect_errno());
}else{
    echo "Database is connected" . "<br>";
}

if (isset($_POST['insert'])){
    $idN1= $_POST['id'];
    $usernameN1 = $_POST['username'];
    $passwordN1 = $_POST['password'];
    $query = "insert into users(id, username, pass) values ('".$idN1."' , '".$usernameN1."' , '".$passwordN1."' )";
    $result = mysqli_query($conn, $query);
}else if (isset($_POST['update'])){
    $idN2 = $_POST['id'];
    $usernameN2 = $_POST['username'];
    $passwordN2 = $_POST['password'];
    $query = "update users set pass = '". $passwordN2 ."'where id = " . $idN2;
    $result = mysqli_query($conn, $query);
}else if (isset($_POST['Display'])){
    header('Location: usersTable.php');
}
echo "<br>";
?>

<form method="post">
    ID: <input type="text" name="id" ><br><br>
    username: <input type="text" name="username" ><br><br>
    password: <input type="password" name="password" ><br><br>
    <input type="submit" name="insert" value="insert">
    <input type="submit" name="Display" value="Display">
</form>

userTable.php

<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "wdl2hw4db";

$conn = mysqli_connect($host, $username, $password, $database);
$query = "select * from users";
$result = mysqli_query($conn, $query);
echo "<table border=\"6px\"><thead><tr><th>ID</th><th>username</th><th>password</th><th>Delete</th><th>Update</th></tr></thead>";
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr><form method='post'><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>" . $row['pass'] . "</td><td><input type='submit' name='Delete" . $i . "' value='Delete'></td><td><input type='submit' name='Update" . $i . "' value='Update'><input type='text' name='UpdateText" . $i . "' placeholder='insert new password here'></td></form></tr>";
    $i++;
}
echo "</table>";
$i = 1;
$result2 = mysqli_query($conn, $query);
while ($row2 = mysqli_fetch_assoc($result2)) {
    if (isset($_POST['Delete' . $i])) {
        $usernameN4 = $row2['username'];
        $query2 = "delete from users where username ='" . $usernameN4 . "'";
        $result2 = mysqli_query($conn, $query2);
        header("Refresh:0");
        break;
    }
    $i++;
};

$i = 1;
$result3 = mysqli_query($conn, $query);
while ($row3 = mysqli_fetch_assoc($result3)) {
    if (isset($_POST['Update' . $i]) && $_POST['UpdateText' . $i] != null ) {
        $id4 = $row3['id'];
        $Utext = $_POST['UpdateText' . $i];
        $query3 = "update users set pass ='" . $Utext . "' where id = " . $id4;
        $result3 = mysqli_query($conn, $query3);
        header("Refresh:0");
        break;
    }
    $i++;
};
mysqli_free_result($result);