我使用php创建了一个搜索,这样当用户登录后,他们可以搜索其他用户并将其添加为朋友。当用户单击添加为朋友按钮时,我想将搜索结果中登录的用户的用户名和用户的用户名发布到名为friend_request的数据库表中。在php中发布搜索结果
这里是我的代码
<?php
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$search = "%$search%";
if ($stmt = $db->prepare("SELECT username, name, location, gender, date_of_birth, url FROM Users WHERE name LIKE ?")){
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->bind_result($username, $name, $location, $gender, $date_of_birth, $picture);
$stmt->store_result();
$count = $stmt->num_rows;
if ($count == 0) {
$output = "There was no search results!";
} else {
while ($stmt->fetch()) {
$output .='<form action="#" method="post"><div class="row"><div class="col-sm-3">'.$name.'<br>'.$location.'<br>'.$gender.'<br>'.$date_of_birth.'</div>';
$output2 = '<div class="col-sm-3"><img src="upload/'.$picture.'"width="180" height="144" /></div>';
$output3 = '<input type="submit" name="addfriend" value="Submit" /></div></form>';
}
}
}
}
if(isset($_POST['addfriend'])) {
$user_from = $_SESSION['username'];
$user_to = $_POST['username'];
if ($stmt = $db->prepare("INSERT INTO `friends_request`(`user_to`, `user_from`) VALUES (?,?)")){
$stmt->bind_param("ss", $user_to, $user_from);
$stmt->execute();
}
}
?>
当我运行我的代码,我得到以下信息
注意:未定义指数:用户名/应用程序/ MAMP/htdocs目录第51行的/student_connect/header.php
你是否开始在任何地方开会?做'session_start();'开始会话。 –
我在我的代码的开头有它,但我没有复制 – Rebekah
[PHP:“注意:未定义的变量”,“注意:未定义的索引”和“注意:未定义的偏移量”](https:/ /stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) –