简体   繁体   English

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

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

I have PHP web pages with page ids such as following example:我有带有页面 ID 的 PHP 网页,例如以下示例:

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

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

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

and so on...等等...

The mysql table is having two columns with field names and values, for example: mysql 表有两列包含字段名称和值,例如:

id = 150 id = 150

email = test@mysite.com email = test@mysite.com

I am able to retrieve page id with the help of: echo $_GET['id'];我可以在以下帮助下检索页面idecho $_GET['id'];

How do I fetch the email based on the GET parameter?如何根据GET参数获取电子邮件? For example for www.mysite.com/page.php?id=150 I would like to echo the email test@mysite.com on page.php .例如对于www.mysite.com/page.php?id=150我想在test@mysite.com上回显电子邮件page.php

Okay, from your provided code you need to address the column you want the data from.好的,从您提供的代码中,您需要解决您想要从中获取数据的列。

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

You also should update to the PDO or mysqli drivers.您还应该更新到PDOmysqli驱动程序。 mysql_ is out of date. mysql_已过时。 It also can't handle prepared statements which is what you should be using.它也无法处理您应该使用的准备好的语句。 This will prevent SQL injections.这将防止 SQL 注入。 I've cast the user input to an integer here to avoid the injection hole.我在这里将用户输入转换为整数以避免注入孔。

How do I fetch and echo related email field on page.php?如何在 page.php 上获取和回显相关的电子邮件字段?

That (to me) seems like you're trying to fetch the input field, if so this could be one way:那(对我来说)似乎您正在尝试获取输入字段,如果是这样,这可能是一种方式:

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

and then on page.php:然后在 page.php 上:

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

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

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

EDIT:编辑:

Moving from from the comments, this is what you should try:从评论开始,这是您应该尝试的:

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

Notice: This will work for the time but it is strictly advised to move to either mysqli or PDO .注意:这暂时有效,但严格建议移至mysqliPDO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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