过去几天我一直在为一个学校项目开发一个twitter克隆,但是我一直在设置独特的个人资料网址,我做了一些谷歌搜索,我知道我需要使用$ _GET但是除了似乎没有更多的信息。这是我的profile.php的开始。希望有人能帮我理解$ _GET更多。提前致谢
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['userSession']))
{
header("Location: index.php");
}
$query = $MySQLi_CON->query("SELECT * FROM users WHERE
user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$MySQLi_CON->close();
?>
我当前的GET
尝试:
$query = $MySQLi_CON->query("SELECT * FROM users WHERE user_name=".$_GET['user_name']);
和我的错误:
注意:未定义的索引:第10行的C:\ xampppp \ htdocs \ network \ profile.php中的user_name致命错误:未捕获错误:在C:\ xampppp \ htdocs \ network中调用boolean上的成员函数fetch_array() \ profile.php:11堆栈跟踪:在第11行的C:\ xampppp \ htdocs \ network \ profile.php中抛出#0 {main}
尝试时:
<a href="profile.php?user=<?php echo $userRow['user_name']; ?>"><?php echo $userRow['user_name']; ?></a>
答案 0 :(得分:0)
$_GET
通过获取参数的左值并将其用作索引来工作,正确的值是值。例如:
home.php?id=1
$_GET['id']
将是1
。在SQL中:
Select * from table where id = $_GET['id']
将是有效的(非常不安全,但SQL有效),因为它将以DB:
的形式发送到DBSelect * from table where id = 1
然而,使用
加载home.php?username=chris
select * from table where username = $_GET['username']
会失败,因为字符串需要在SQL中引用而$_GET['username']
以chris
形式出现:
select * from table where username = chris
所以
select * from table where username = '$_GET['username']'
是有效的,但这些方法都不合适,而应使用parameterized queries。
$prepared = $MySQLi_CON->prepare('SELECT * FROM users WHERE user_name = ?');
if (!$prepared->bind_param("s", $_GET['username'])) {
echo "Binding parameters failed: (" . $prepared->errno . ") " . $prepared->error;
}
if (!$prepared->execute()) {
echo "Execute failed: (" . $prepared->errno . ") " . $prepared->error;
}
$returned = $prepared->get_result();
$userRow = $returned->fetch_array();
$MySQLi_CON->close();
?>