I made a simple example in PHP, the objective is simply insert the values of the array to the database. The field names are the array keys and the field values are the array values. I chose object-oriented style for querying because I'm practising OOP. When I looped it, the issue was this: the code only accepts the first index of the array, which is the "name"; it is supposed to accept the four values since the table consists of four fields (name, email, username, password). Now the result in the database is only the name field has value, the rest of the fields are null. Here is the code:
<?php
class User {
public static function insert($table, $table_fields = array()) {
if(count($table_fields)) {
foreach($table_fields as $field_name => $field_value) {
return "INSERT INTO $table ($field_name) VALUES ('$field_value')";
}
}
return false;
}
}
$connect = mysqli_connect('localhost', 'root', '', 'sample');
mysqli_query($connect, User::insert('users', array(
'name' => 'Sample Name',
'email' => 'samplename@samplemail.com',
'username' => 'sampleusername',
'password' => '12345'
)));
?>
I want to know what's wrong with my code, and it would give me much more gratitude if you give me tips for improvement.