-2

I am trying to get my page to update a DB when there is $_GET data.

However even when www.myurl.com?status= is blank the page updates the DB with nothing.

Here is my code

$status=$_GET["status"];

$sql="UPDATE users SET status =$status WHERE personID='$user'" or die(mysql_error());
mysql_query($sql);

Can anyone help? I am trying to get the page to do nothing if the URL is just www.myurl.com

  • 1
    `if (!empty($_GET)) { ... do stuff ... } else { exit; }` – Amal Murali Feb 16 '14 at 17:27
  • 1
    soo.. why not just check the contents of that variable? – Nanne Feb 16 '14 at 17:27
  • 2
    Sidenote: You are **wide open** to SQL injection attacks, and you will be hacked if you haven't been already. Please use prepared / parameterized queries to prevent this from happening. See also: [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/) – Amal Murali Feb 16 '14 at 17:29
  • You first of all need a function that returns you the request URI. So far this is missing. – hakre Feb 16 '14 at 17:29
  • 1
    lol set site-wide user status's in 1 injection query – Lawrence Cherone Feb 16 '14 at 17:30
  • This is prone to a SQL attack as said above. Try switching to PDO or mysqli, prepares statements should help out. – ykykykykyk Feb 16 '14 at 17:37

2 Answers2

2
if(!empty($_GET['status']) { // Check if `status` is not empty
    $sql="UPDATE users SET status = $_GET['status'] WHERE personID='$user'";
    mysql_query($sql);  // Continue with sql query
}

empty - http://us2.php.net/empty

isset - http://us2.php.net/isset

ykykykykyk
  • 446
  • 1
  • 3
  • 10
0

Make use of empty() in PHP to achieve this.

if(!empty($_GET["status"]))  //<--- Control to the inside will be passed only if the status variable is not empty
{
$status=$_GET["status"];
$sql="UPDATE users SET status =$status WHERE personID='$user'" or die(mysql_error());
mysql_query($sql);
}

That was the first thing. Secondly you are using an obsolete deprecated API i.e. the mysql_* functions. You need to switch over to PreparedStatements.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126