Page 1 of 1

Notice: Undefined variable error

Posted: Thu Apr 09, 2015 6:42 pm
by marvela
I keep getting an error message

"Notice: Undefined variable: pos in /Applications/MAMP/htdocs/project1/rate.php on line 8".
I was getting this error on line 5 but i had included the asset function, i am stuck and do not know how to fix this error. I will post the code below of the two related pages, please help.

Thanks in advance

Code: Select all


**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 &copy; 2013<a href="#"    title="1stwebdesigner">1stwebdesigner.com</a></p>
      </footer>
    </body>

       </html>

 

Re: Notice: Undefined variable error

Posted: Thu Apr 09, 2015 7:49 pm
by requinix
Even if you had a $pos in the other page, the one in rate.php is the one that matters.

Values in $_POST do not automatically get variables. You need a

Code: Select all

$pos = $_POST['pos'];
before you can use $pos.

But that alone leaves you open to SQL injection, where a malicious person like me can come to your site and do Very Bad Things.
You're using mysqli, which is great. Learn about its prepared statements: they're easy to use and will protect you from SQL injection.