Hi guys,
I was coding up a function that will automate my INSERT INTO query in a sense that i will have to type only the columns i want to populate..
here's the function
// INSERT INTO (doesnt quite work yet)
function db_insert($table,$vars){
$var = explode(",",$vars);
$totalVars = count($var) - 1;
$sql_a = "INSERT INTO $table (";
$sql_b .= ") \n Values(";
foreach($var as $curVar => $curVal){
$sql_a .= trim($curVal);
$sql_b .= "'$" . trim($curVal) . "'";
if($curVar != $totalVars){ //if this isnt the last array yet, add comma (,)
$sql_a .=",";
$sql_b .=",";
}
}
$sql = $sql_a . $sql_b . ")";
return $sql;
}
the script works fine and when echoed i can see that it produces the correct sql sytanx.
for example:
if a call the function and echo it like this:
echo db_insert("exams","exam_date,examiner,remarks");
the result is the follwoing:
INSERT INTO exams (exam_date,examiner,remarks)
Values('$exam_date','$examiner','$remarks')
exactly what i wanted. however, when i run this directly into a query, it doesnt work.. eg:
$results = mysql_query(echo db_insert("exams","exam_date,examiner,remarks"));
please help..
tea