Skip to main content
deleted 71 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This code inserts data into the database successfully. I havewas concerned about whether it is a considered good practise to go about inserting data into the following PHP function:database in this way. Is there something I am not considering?

function insertTest(){

    global $db;
    
    $field_names    = array("id", "first_name", "last_name", "email");
    $alias          = array(":id", ":fn", ":ln", ":em");
    $assignment     = array(":id" => "2k39dk38dk2k39dk38dk2k39dk38dk2k39dk38dk",
                            ":fn" => "Chris", 
                            ":ln" => "Moore", 
                            ":em" => "[email protected]");

    $array = array();
    $array['table'] = 'users';
    $array['field_names'] = $field_names;
    $array['alias'] = $alias;
    $array['assignment'] = $assignment;

    $q = "INSERT INTO `%s` (%s) VALUES (%s)";
    $q = sprintf($q, $array['table'], implode(", ",$array['field_names']), implode(", ",$array['alias']));
    $q = $db->prepare($q);
    $r = $q->execute($assignment);

    print_r($r);

    return $r;

}

The code inserts data into the database successfully. I was concerned about whether it is a considered good practise to go about inserting data into the database in this way. Is there something I am not considering? I'm a beginner at PDO.

Thanks.

I have the following PHP function:

function insertTest(){

    global $db;
    
    $field_names    = array("id", "first_name", "last_name", "email");
    $alias          = array(":id", ":fn", ":ln", ":em");
    $assignment     = array(":id" => "2k39dk38dk2k39dk38dk2k39dk38dk2k39dk38dk",
                            ":fn" => "Chris", 
                            ":ln" => "Moore", 
                            ":em" => "[email protected]");

    $array = array();
    $array['table'] = 'users';
    $array['field_names'] = $field_names;
    $array['alias'] = $alias;
    $array['assignment'] = $assignment;

    $q = "INSERT INTO `%s` (%s) VALUES (%s)";
    $q = sprintf($q, $array['table'], implode(", ",$array['field_names']), implode(", ",$array['alias']));
    $q = $db->prepare($q);
    $r = $q->execute($assignment);

    print_r($r);

    return $r;

}

The code inserts data into the database successfully. I was concerned about whether it is a considered good practise to go about inserting data into the database in this way. Is there something I am not considering? I'm a beginner at PDO.

Thanks.

This code inserts data into the database successfully. I was concerned about whether it is a considered good practise to go about inserting data into the database in this way. Is there something I am not considering?

function insertTest(){

    global $db;
    
    $field_names    = array("id", "first_name", "last_name", "email");
    $alias          = array(":id", ":fn", ":ln", ":em");
    $assignment     = array(":id" => "2k39dk38dk2k39dk38dk2k39dk38dk2k39dk38dk",
                            ":fn" => "Chris", 
                            ":ln" => "Moore", 
                            ":em" => "[email protected]");

    $array = array();
    $array['table'] = 'users';
    $array['field_names'] = $field_names;
    $array['alias'] = $alias;
    $array['assignment'] = $assignment;

    $q = "INSERT INTO `%s` (%s) VALUES (%s)";
    $q = sprintf($q, $array['table'], implode(", ",$array['field_names']), implode(", ",$array['alias']));
    $q = $db->prepare($q);
    $r = $q->execute($assignment);

    print_r($r);

    return $r;

}
edited tags
Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70
Tweeted twitter.com/#!/StackCodeReview/status/641624160905637888
Source Link
cwiggo
  • 153
  • 1
  • 5

PDO Insert function using PHP

I have the following PHP function:

function insertTest(){

    global $db;
    
    $field_names    = array("id", "first_name", "last_name", "email");
    $alias          = array(":id", ":fn", ":ln", ":em");
    $assignment     = array(":id" => "2k39dk38dk2k39dk38dk2k39dk38dk2k39dk38dk",
                            ":fn" => "Chris", 
                            ":ln" => "Moore", 
                            ":em" => "[email protected]");

    $array = array();
    $array['table'] = 'users';
    $array['field_names'] = $field_names;
    $array['alias'] = $alias;
    $array['assignment'] = $assignment;

    $q = "INSERT INTO `%s` (%s) VALUES (%s)";
    $q = sprintf($q, $array['table'], implode(", ",$array['field_names']), implode(", ",$array['alias']));
    $q = $db->prepare($q);
    $r = $q->execute($assignment);

    print_r($r);

    return $r;

}

The code inserts data into the database successfully. I was concerned about whether it is a considered good practise to go about inserting data into the database in this way. Is there something I am not considering? I'm a beginner at PDO.

Thanks.