user3639768
user3639768

Reputation: 121

Inserting data in table by using function arguments

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");

Upvotes: 1

Views: 35

Answers (2)

Sainath Krishnan
Sainath Krishnan

Reputation: 2139

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!

Upvotes: 0

kero
kero

Reputation: 10638

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);

Upvotes: 2

Related Questions