Jump to content

sql query problems


blueman378

Recommended Posts

hi guys i have,

<?php

$action = $_GET['action'];
$cat = $_GET['cat'];

if ($action == "Browse")
{
function doSomething()
{
// browse script here
echo "You Are Now Browsing";
}
}
else if ($action == "Delete")
{
function doSomething()
{
// delete script here
echo "Delete Complete";
}
}
else if ($action == "Create")
{
function doSomething()
{
// delete script here
$sql = ("INSERT INTO `game`.`gsubcat` 
(`cId`, `cName`) VALUES ('NULL','$cat')");
die("SQL:<br>$sql");
echo "$_GET[cat] Creation Complete";
}
}
else {
function doSomething()
{
echo "No Action Specified!";
}
}

doSomething();

 

but when i run it using this url:

http://localhost/admin/catprocess.php?action=Create&cat=Hello

 

the output is

SQL:
INSERT INTO `game`.`gsubcat` (`cId`, `cName`) VALUES ('NULL','')

 

any ideas?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/83111-sql-query-problems/
Share on other sites

This is because the function can not find $cat as it is set outside the functions variable scope.

 

For a function to pickup a variable outside of its scope you need to use global before the variable inside your function.

 

IE:

 

function doSomething()
{
global $cat;
// create script here
$sql = ("INSERT INTO `game`.`gsubcat` 
(`cId`, `cName`) VALUES ('NULL','$cat')");
die("SQL:<br>$sql");
echo "$_GET[cat] Creation Complete";
}

 

Please also note your insert statement may not work correctly with quotes around your NULL value  ;)

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/83111-sql-query-problems/#findComment-422781
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.