Nothing is guaranteed to work!
<?php
function fSQL_INSERT($D,$T,$FV){ // ( Database, Table, Fields & Values Array )
if( !is_array( $FV ) ){ return false; } // <- Key-Value Array. Keys are Field Names, Values are values
$a = $D -> prepare( 'SHOW TABLES' );
$a -> execute();
$A = $a -> fetchAll( PDO::FETCH_COLUMN ); // <- Gets all Tables of your Database
if( !in_array( $T, $A ) ){ return false; } // <- Makes sure your Table exists in your Database
$a = $D -> prepare( "DESCRIBE $T" );
$a -> execute();
$A = $a -> fetchAll( PDO::FETCH_COLUMN ); // <- Gets all Fields of your Table
$i = 0;
$R = array();
$FIELDS = '';
$VALUES = '';
$F = '';
$V = '';
foreach( $FV as $F => $V ){
if( !in_array( $F, $A ) ){ return false; } // <- Makes sure all Fields exists in your Table
$FIELDS .= " `$F` "; // <- Sets up the String we will be using
$VALUES .= " :$i "; // <- Sets up the String we will be using
$R[":$i"] = $V; // <- Sets up the Array to execute
$i++; // <- Used for Array key values and adding a comma in String
if( $i < count( $FV ) ){
$FIELDS .= ',';
$VALUES .= ',';
} // <- Adds a comma to the String if needed
}
$a = $D -> prepare( "INSERT INTO `$T` ($FIELDS) VALUES ($VALUES)" );
while( $i > 0 ){
$i--;
$a -> bindValue( ":$i", $R[":$i"], PDO::PARAM_STR );// <- Use a bindValue thing - i think its supposed to be safer
}
$a -> execute(); // <- Execute
return $D -> lastInsertId();
}
?>
Example:
<?php
/*
$LAST = fSQL_INSERT( $DataB, 'account', array(
'password' => $Password,
'email' => $Email,
'registered' => time()
) );
*/
//if( $LAST ){ echo 'Your ID whatever is ' . $LAST; }
?>