-1

im running a php script. I want the php script to send a sql code to my database that updates the current users "ally" when a user enters MYSITE.COM/include/fire.php. But I get this error

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

Line 14: mysql_query("UPDATE users SET ally='3' WHERE id='{$id}'");

The whole fire.php

<?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}'");  
?>

Please help me with this error. Don't give me a link to someone with similar problem. Just help me

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • This error is telling you that the variable $id has no value. – Mark Oct 02 '14 at 16:29
  • how do I fix that then? – Elvisp Helt enkelt Oct 02 '14 at 16:32
  • `$id = $_SESSION['id'];` – Daniel W. Oct 02 '14 at 16:35
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Oct 02 '14 at 16:53
  • This is practically the same question as your first question. We're not going to code everything for you. Please learn about the PHP basics first, and learn about PHP globals. – Matheno Oct 06 '14 at 07:14

2 Answers2

0

declare var $id and assign its value. you should be good.

johnny
  • 2,032
  • 1
  • 25
  • 45
  • how do it do that? sorry im not a very good coder. But please help me anyway – Elvisp Helt enkelt Oct 02 '14 at 16:32
  • You need to assign $id a value. $id = 'useridtobeallied'; – Mark Oct 02 '14 at 16:34
  • put this $id=$_SESSION['id']; before mysql_query("UPDATE users SET ally='3' WHERE id='{$id}'"); – johnny Oct 02 '14 at 16:35
  • you need to make sure that $_SESSION['id'] is returning the correct id. – johnny Oct 02 '14 at 16:35
  • That assumes he wants the same user that's logged in to be the one incremented. There is not really enough logic in the code to infer that, or in his explanation. – Mark Oct 02 '14 at 16:36
  • Oh, thanks the error is gone. But nothing change in the database. I want this to change http://prntscr.com/4sega0. And my fire.php looks like this http://prntscr.com/4seglm – Elvisp Helt enkelt Oct 02 '14 at 16:38
0

It seems that variable $id is not defined in the current context. If you mean to use the session id as your users' table id, you need to use the {$_SESSION['id']} in your query instead of {$id}.

Tala
  • 909
  • 10
  • 29
  • But then I get this error Notice: Undefined index: id in C:\Users\Name\Desktop\Folder\htdocs\include\Fire.php on line 16 – Elvisp Helt enkelt Oct 02 '14 at 16:45
  • Build your query in your `if(isset($_SESSION['id']))` clause as you mentioned in your question and it won't bother you anymore. – Tala Oct 02 '14 at 16:50
  • Sorry dude I don't really understand. Now my Fire.php looks like this http://prntscr.com/4senf3 – Elvisp Helt enkelt Oct 02 '14 at 16:56
  • @jay-balanchard is right by the way. But to make this mysql_* work, cut the `mysql_query()` statement and paste it over `// do the following query`, inside `if` clause. – Tala Oct 02 '14 at 17:05