无法使用SELECT * FROM表WHERE变量LIKE'$ variable'将相关的mysql信息输出到单击的链接 [英] Can't output relevant mysql information to clicked link using SELECT *FROM table WHERE variable LIKE '$variable'
问题描述
我有一个名为"drinks"的mysql数据库,在该数据库中,我有一个名为"persons"的表,在"persons"中,我有两个人,Bryan(fname)Fajardo(lname)21(age)和Andross H Age :20.
I have a mysql database named "drinks", in that database I have one table named "persons" and in "persons" I have two people, Bryan(fname) Fajardo(lname) 21(age) and Andross H Age:20.
在我的index.php中,我建立了表人中所有人员的链接. 我试图使我的链接正常工作,以便当我单击任何一个名称时,与该人相关的信息都将输出到我的另一个页面(链接指向该页面)中:insert.php.
In my index.php I have links set up from all of the people in table persons. I am trying to get my links to work so that when I click on either name, the information relevant from that person is outputted into my other page (where the link goes to) which is: insert.php.
我一直在试图通过单击Bryan链接并仅输出他的姓氏等来进行一些测试.我的目标:是为了能够将人"中的人与人联系起来表格,然后点击进入insert.php并在该处输出该人的信息.
I have been trying for hours to run some test by clicking on the Bryan link and outputting only his last name etc. etc. My objective: is to be able to link the people from "persons" table and then upon click go to insert.php and output that person's information there.
这是我当前从Index.php获得的代码.
Here is my current code from Index.php.
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
//Connect to the database
$con = mysqli_connect("localhost","username","password","drinks");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo '<span style = "background: red ;">MSQL Connected.</span>' ;
}
$result = mysqli_query($con,"SELECT * FROM persons");
while($row = mysqli_fetch_array($result)) {
Print '<dd><a href=insert.php?
fname="'.$row['fname'].'">'.$row['fname'].'</a></dd>';
}
mysql_close();
?>
</body>
</html>
这是我的Insert .php,我要在其中打印相关信息.
and here is my Insert .php where I want the relevant information to be printed.
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class = "full-header">
<div class = "mid-header span12 center">
</div>
</div>
<div class = "main-content-container full_w">
<div class = "span12 main-content center">
<?php
$cont = mysqli_connect("localhost","username","password","drinks");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo '<span style = "background: red ;">yay</span>' ;
}
$fname = $_GET['fname'];
$sel = ($cont,"SELECT * FROM persons WHERE fname ='%$fname%'");
while($rower = mysqli_fetch_array($sel)) {
Print $rower['lname'];
//why is this not printing Bryan's last name?
}
?>
</div>
</div>
</body>
</html>
在此先感谢您,感谢您的帮助,最近我才开始学习php和数据库构建/召唤.
Thank you in advance, I appreciate the help, I have just recently gotten into php and database building/summoning.
我也一直在阅读它已被弃用,现在将要使用PDO,如果您有涉及PDO的解决方案,我也将不胜感激,但是我对PDO还是很陌生. 在insert.php查询中将表"更改为人".仍然没有解决.
I also have been reading that this is becoming deprecated and PDO is going to be used now, if you have a solution that involves PDO, I would appreciate that as well, but I am very new to PDO. EDIT 2: Changed "table" to "persons" in insert.php query.Still did not fix.
推荐答案
您有两个主要错误.在index.php上,您将查询字符串值括在引号中
You have two main errors. On index.php you are wrapping your query string values in quotes
Print '<dd><a href=insert.php?
fname="'.$row['fname'].'">'.$row['fname'].'</a></dd>';
这应该是
Print '<dd><a href="insert.php?
fname='.$row['fname'].'">'.$row['fname'].'</a></dd>';
接下来,在第二页上,您需要在查询中使用LIKE.
Next, on your second page, you need to use LIKE on your query.
$sel = ($cont,"SELECT * FROM persons WHERE fname LIKE '%$fname%'");
也就是说,您确实应该使用参数,因为当前方法将打开您的脚本到SQL注入,则应考虑在查询字符串中使用主键,而不要传递此人的姓名.
That said, you really should use parameters because the current method is going to open your script up to SQL Injection, and you should consider using a primary key in your querystring instead of passing the person's name.
Print '<dd><a href="insert.php?
id='.$row['id'].'">'.$row['fname'].'</a></dd>';
您的查询
$id = intval($_GET['id']);
$sel = ($cont,"SELECT * FROM persons WHERE id = $id");
最后一点说明,在索引页上,您正在使用mysql_close
而不是mysqli_close
来关闭数据库连接.
One final note, on your index page, you are using mysql_close
instead of mysqli_close
to close your database connection.
这篇关于无法使用SELECT * FROM表WHERE变量LIKE'$ variable'将相关的mysql信息输出到单击的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!