我的网站查询localhost数据库并检索“菜单”的数据并将其显示在表格中。我为按钮设置了一列。这些按钮被分配了名称,这些名称按照数组的顺序排列 'ids'是唯一的,来自数据库。
$username="root";
$password="";
$database="login";
$con = mysql_connect("localhost", $username, $password);
$selectdb = mysql_select_db("login",$con);
// get results from database
$result = mysql_query("SELECT id, name, price, category FROM menu")
or die(mysql_error());
echo "<form action='' method='POST'>";
echo "<p>Select: <a href=admin_menu.php>Add</a> | <b>Edit</b> | <a href=admin_menu_delete.php>Delete</a> |</p>";
echo "<table border='0' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Price ($)</th> <th>Category</th> <th>Edit</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
if ($row['category'] == '1') {
$category = "Breakfast";
}
if ($row['category'] == '2') {
$category = "Drinks";
}
if ($row['category'] == '3') {
$category = "Snacks";
}
if ($row['category'] == '4') {
$category = "Main";
}
echo "<tr>"; // echo out the contents of each row into a table
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $category . '</td>';
echo '<td><button type="submit" value="Edit" name="id['.$row['id'].']" /></button>' . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
echo '</form>';
print_r($_POST);
那么,当我点击第30行的按钮时,你如何在另一个php页面中提取数字30?
答案 0 :(得分:0)
你可能会改变你的代码,
$username="root";
$password="";
$database="login";
$con = mysqli_connect("localhost", $username, $password);
$selectdb = mysqli_select_db("login",$con);
// get results from database
$result = mysqli_query("SELECT id, name, price, category FROM menu")
or die(mysql_error());
echo "<form action='' method='POST'>";
echo "<p>Select: <a href=admin_menu.php>Add</a> | <b>Edit</b> | <a href=admin_menu_delete.php>Delete</a> |</p>";
echo "<table border='0' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Name</th> <th>Price ($)</th> <th>Category</th> <th>Edit</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
if ($row['category'] == '1') {
$category = "Breakfast";
}
if ($row['category'] == '2') {
$category = "Drinks";
}
if ($row['category'] == '3') {
$category = "Snacks";
}
if ($row['category'] == '4') {
$category = "Main";
}
echo "<tr>"; // echo out the contents of each row into a table
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $category . '</td>';
echo '<td><button type="submit" value="'.$row['id'].'" name="Edit" />Edit</button>' . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
echo '</form>';
print_r($_POST);
然后,
使用$_POST['Edit']
访问按钮值。