我不断收到错误消息
“注意:未定义的变量:第8行/Applications/MAMP/htdocs/project1/rate.php中的pos”。
我在第5行遇到了这个错误,但我已经包含了资产功能,我被卡住了,不知道如何解决这个错误。我将在下面发布代码,请帮助。
提前致谢
Rate.php页面
<?php
$mysqli = new mysqli("localhost","root","root", "rating") or die ("Couldnt connect to the database!");
if (isset($_POST['pos']))
$post_rating = $_POST['rating'];
$find_data = $mysqli->query("SELECT * FROM rates WHERE pos='$pos'");
while($row = $mysqli->fetch_assoc($find_data))
{
$id = $row['id'];
$current_rating = $row['rating'];
$current_hits = $row['hits'];
}
$new_hits = $current_hits + 1;
$update_hits = $mysqli->query("UPDATE rates SET hits = '$new_hits' WHERE id='$id'");
$pre_rating = $current_rating + $post_rating;
$new_rating = $pre_rating / $new_hits;
$update_rating = $mysqli->query("UPDATE rates SET rating = '$new_rating' WHERE id='$id'");
?>
**FGLRANK.php page**
<!DOCTYPE html>
<html lang="en">
<head>
<title>Football's Greatest Legends Ranking</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style1.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body class="body">
<header class="mainheader">
<center>
<img src="ball.jpg" width="400" height="150" alt="Ron"align="">
</center>
<center>
<font color="white" "arial" size= "6"> <b> Greatest Footballing Legends - You Decide!! </b> </font>
</center>
<nav><ul>
<li> <a href="home.html">Home</a></li>
<li> <a href="fgl.html">Football Legends</a></li>
<li><a href="fglrank.php">Football Legends Ranking</a></li>
<li><a href="contact.html">Contact</a></li>
</ul></nav>
</header>
<div class="mainContent">
<div class="content">
<article class="topcontent">
<header>
<h2> <center> <font color="black" "arial" size= "3"> <b> <i> Legends Ranking! </i> </b> </font> </center> </h2>
</header>
<center>
<content>
<?php
$mysqli = new mysqli("localhost","root","root", "rating") or die ("Couldnt connect to the database!");
$find_data = $mysqli->query("SELECT * FROM rates");
while($row = mysqli_fetch_assoc($find_data))
{
$id = $row['id'];
$nameofplayer = $row['nameofplayer'];
$pos = $row['pos'];
$current_rating = $row['rating'];
$hits = $row['hits'];
echo "
<form action='rate.php' method='POST'>
$nameofplayer: <select name='rating'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<input type='hidden' value='$nameofplayer' name='nameofplayer'>
<input type='submit' value='Rate!'> Current Rating: "; echo $current_rating; echo "
</form>
";
}
?>
</content>
</center>
<footer>
<p class="post-info">Greatest Legends of the game</p>
</footer>
</article>
<article class="bottomcontent">
<header>
<h2><a href="#" title="Second post">Second post</a></h2>
</header>
<footer>
<p class="post-info">This post is written by Naz</p>
</footer>
<content>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
</article>
</div>
</div>
<aside class="top-sidebar">
<article>
<h2>Top sidebar</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p>
</article>
</aside>
<aside class="middle-sidebar">
<article>
<h2>Middle sidebar</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</article>
</aside>
<aside class="bottom-sidebar">
<article>
<h2>Bottom sidebar</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</article>
</aside>
<footer class="mainFooter">
<p>Copyright © 2013<a href="#" title="1stwebdesigner">1stwebdesigner.com</a></p>
</footer>
</body>
</html>
答案 0 :(得分:0)
// line 8
$mysqli->query("SELECT * FROM rates WHERE pos='$pos'");
但是,您没有设置$pos
,因此通知。
我假设您打算使用$_POST['pos']
。
另外,请花点时间阅读SQL Injection并相应更新您的代码。