我使用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) –