1

Why isn't this inserting the data into applications table?

function setAppData($url_id, $name, $version) {
        $query = "INSERT INTO applications(url_id, naam, versie)
                         VALUES( '$url_id',
                                 '$name',
                                 '$version'
                               )";
        mysql_query($query, SQLConnection());
}

however if I do it like below, then it seems to work.

function setAppData($url_id, $name, $version) {
    $u = $url_id;
    $n = $name;
    $v = $version;
    $query = "INSERT INTO applications(url_id, naam, versie)
                     VALUES( '$u',
                             '$n',
                             '$v'
                           )";
    mysql_query($query, SQLConnection());
}

The way I call the function:

setAppDate(1,"apache","2.4.9");
5
  • I still doubt on the 2nd query since '$v', you have an extra comma. Commented May 20, 2014 at 14:10
  • Updated post, accidently added that. I still do encounter same problem. Commented May 20, 2014 at 14:12
  • 1
    Could work if you properly concatenate the variables like so VALUES( '".$url_id."', '".$name."', ... Commented May 20, 2014 at 14:16
  • This is strange... could you try echo the query in first case and see if this works on mysql directly. Commented May 20, 2014 at 14:17
  • @kingkero post that as answer that does seem to work for me :D Commented May 20, 2014 at 14:20

2 Answers 2

2

It seems that PHP is unable to interpret your variables correctly (maybe because of the underscore), so it would be best to explicitly concatenate them like so

function setAppData($url_id, $name, $version) {
    $query = "INSERT INTO applications(url_id, naam, versie)
                         VALUES( '".$url_id."',
                                 '".$name."',
                                 '".$version."'
                               )";
    mysql_query($query, SQLConnection());
}

Or to make it more readable you can use sprintf()

$query = sprintf("INSERT INTO applications (url_id, naam, versie)
                      VALUES ('%s', '%s', '%s')",
                 $url_id, $name, $version);
Sign up to request clarification or add additional context in comments.

Comments

0

If you could add in the following at the end of your code, it will give you more details at to what the exact problem is :

 mysql_query($query, SQLConnection()) or die(mysql_error());

If you get an error along the lines of "..supplied argument is not a valid MySQL-Link resource.." etc, it is a problem of variable scope. Using global tags can possibly fix this, but I can give you a more tailored response once you try this!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.