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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.