0

i know this must be only a small bug, but i cant find it.

My function:

function del_mysql($table,$id)
{
$id = $_GET['id'];
$exec = mysqli_query($con, "delete from $table where id = '$id'");
return $exec;
}

in Code:

if ($_GET['action'] == 'delete')
{
del_mysql("awsome","$id");
}

if make in function:

$id = $_GET['id'];
echo $table;
echo $id;

i get right table and id.

Somebody see the bug? I removed already the $exec and return part and leave only mysqli_query command. but dont want to work.

14
  • 11
    Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. Commented Jan 8, 2014 at 11:47
  • 2
    Turn error reporting ON and you will see your errors. Commented Jan 8, 2014 at 11:48
  • 1
    Why do you use Superglobals in your function? Using Superglobals is normally discouraged in functions and methods and also error-prone. Also "Somebody see the bug?" is not a valid question to ask for on Stackoverflow while you're only dumping some live code and not an isolated example that you created from scratch to demonstrate an isolated issue that you're not able to understand. Commented Jan 8, 2014 at 11:49
  • 1
    @Royal Bg: You did not agree nor disagree with me. You honor to some code you not further specify and take it's sole existance as an argument against what I wrote. Which is obviously not the case, well designed and popular frameworks do interact with Superglobals (PHP is a framework already), however they reduce the usage of these to the bare minimum and even work without these. So the framworks are normally without superglobals, your bootstrap code might use them, but that's not a function nor method in the sense of my comment. So you've not given any argument, sorry. Commented Jan 8, 2014 at 11:56
  • 1
    Also, in the context of this question, the hint that you should prevent superglobals usage in your code should be an easy to follow and think about guideline which should be embraced. There is no use to tell the OP about some "popular framework" and imply this would change anything to the code-smell. Commented Jan 8, 2014 at 11:57

1 Answer 1

0

The problem is that in your del_mysql function, you are referencing the connection object $con, which does not exist in the scope of the function. Either pass it into the function as a parameter like this:

function del_mysql($table, $id, $con) {

or access it as a global variable like this:

function del_mysql($table, $id) {
    global $con;

I hope that helps.

Regards, Ralfe

Sign up to request clarification or add additional context in comments.

6 Comments

Even this might be intended helpful, it does not qualify as an answer (as much as the question does not qualify as a question). Also the code is suboptimal.
@hakre, maybe it would be better to post self a answers instand of criticize other.
@user2933212, I'm glad I could help. As this answered your question, could I please ask that you mark the answer as correct.
@user2933212: I showed my opinion, also your non-question has been honored with comments to a great extend already which IMHO should be the place to reflect it, not per an answer. Also I do not think that criticizing each other is something bad as it sounds with your comment. Therefore I disagree with that especially.
@hakre, it seems that my question does really annoy you. i am a hard beginner in PHP and wanted a quick answer for a small problem. before posting here i was using google, but couldnt find a solution. imho you should keep cool because its not good for your heart if you get in rage, and probably you could post a answer if you find that ralfe answer is not so good. for me, the answer is good enough and it solved my problem. anyway this script is only local so it doesnt hurt somebody if the code smells like - what you sayed - And you can now ask all your friends to press - btn, like you.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.