簡體   English   中英

如何借助mysql表中的id值回顯其他字段(列)值

[英]How to echo other field (column) value with the help of id value from mysql table

我有帶有頁面 ID 的 PHP 網頁,例如以下示例:

www.mysite.com/page.php?id=150

www.mysite.com/page.php?id=151

www.mysite.com/page.php?id=152

等等...

mysql 表有兩列包含字段名稱和值,例如:

id = 150

email = test@mysite.com

我可以在以下幫助下檢索頁面idecho $_GET['id'];

如何根據GET參數獲取電子郵件? 例如對於www.mysite.com/page.php?id=150我想在test@mysite.com上回顯電子郵件page.php

好的,從您提供的代碼中,您需要解決您想要從中獲取數據的列。

<?php 
$id = (int)$_GET['id'];
$result = mysql_query("SELECT email FROM table_name WHERE id =$id"); 
while ($row = mysql_fetch_array($result)) { 
     echo $row['email'];
     //$emails[] = $row['email']; or if you want to use them later store them.
} 
?> 

您還應該更新到PDOmysqli驅動程序。 mysql_已過時。 它也無法處理您應該使用的准備好的語句。 這將防止 SQL 注入。 我在這里將用戶輸入轉換為整數以避免注入孔。

如何在 page.php 上獲取和回顯相關的電子郵件字段?

那(對我來說)似乎您正在嘗試獲取輸入字段,如果是這樣,這可能是一種方式:

<form method = "POST" action = "page.php">
Enter the Email:
<input type = "text" name = "email">
<input type = "hidden" name = "check">
<input type = "submit" value = "Goto Next Page">
</form>

然后在 page.php 上:

<?php
if(isset($_POST['check'])){

echo $email = $_POST['email'];

$que = "SELECT email from tablename WHERE email = '$email'";
$res = mysqli_query($con, $que);
?>

編輯:

從評論開始,這是您應該嘗試的:

<?php 
$id = (int)$_GET['id'];
$result = mysql_query("SELECT email FROM table_name WHERE id ='$id'"); 
 while ($row = mysql_fetch_array($result)) {
 echo $row['email'];
 // write more this to fetch the required data
echo $row['Your_Next_Column_Name_Here']; 
} 
?>

注意:這暫時有效,但嚴格建議移至mysqliPDO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM