-3

When I try to enter a page on my site I get this error

Notice: Undefined variable: id in C:\Users\Name\Desktop\Folder\htdocs\include\Earth.php on line 14

The php file looks like this in the editor

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

session_start();

$verbindung = mysql_connect("localhost","root","Palmen162");
mysql_select_db("lan");

if(isset($_SESSION['id'])) {
// do the following query
}

mysql_query("UPDATE users SET ally='3' WHERE id='{$id}'");  
 ?>
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 2
    What does mysql_error() say? Your answer is in its output. – John Conde Sep 30 '14 at 12:57
  • 1
    Your code has a lot of problems (built using deprecated database functions, and SQL injection for example), but your biggest one is you're not doing any error checking or reporting at all. Any of mysql_connect, mysql_select_db or mysql_query could fail but you're not bothering to check for any failure. – GordonM Sep 30 '14 at 12:57
  • You never define `$id` nor set, before trying to use its value – Rowland Shaw Nov 14 '14 at 15:58

1 Answers1

2

Make sure that you have session_start() on top of your PHP file:

And always check the existence of that value:

if(isset($_SESSION['id'])) {
    // do the following query
}

Obligatory note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

And always don't forget to turn on error reporting:

error_reporting(E_ALL);
ini_set('display_errors', '1');
Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • The code could be failing for any other reason related to mysql because there's no error checking in it. You might be right that this is the problem but it's possible that the OP simply didn't put the session_start call in the question. – GordonM Sep 30 '14 at 13:00
  • @GordonM thats what i was thinking too since its not added on the OP's code, the session_start is missing – Kevin Sep 30 '14 at 13:01