Learning Alone. Help Please!
Posted: Tue Sep 04, 2007 10:18 pm
I've been trying to write a function that inserts data to a mysql db. I want it completely automated so it can be used with multiple db's and even use it on other sites i design. I've got it to grab the fieldnames from the table and format them for sql syntax, when echoed they display properly to the page. however when i change the echo to a return it only returns the index fieldname. Same thing with the values function the code is as follows:
PLEASE HELP!!!
PLEASE HELP!!!
Code: Select all
//--Function To Insert Data to a MySql Database
function Insert2_Db($iPick_Db, $table, $value_arr){
//--Gets Field Names For SQL Query
function getFields($iPick_Db, $table){
$sql="SELECT * FROM `".$table."`";
$result = mysql_query($sql) or die(mysql_error());
$rowcount=mysql_num_rows($result);
$y=mysql_num_fields($result);
for($x=0;$x<$y;$x++){
$fields = array("`".mysql_field_name($result, $x)."`,");
//CONTINUE HERE
}
return $return;
}
//--Formats The Values for SQL Syntax
function cleanValues($value_arr){
$y = count($value_arr);
ob_start();
for($x=0;$x<$y;$x++){
return "`".$value_arr[$x]."`,";
$values = ob_end_clean();
}
return $values;
}
//--Executes
$fields=getFields($iPick_Db, $table);
$values=cleanValues($value_arr);
$sql="INSERT INTO `".$iPick_Db."`,`".$table."` (".$fields.")
VALUES (
NULL , ".$values.");";
$insert = mysql_query($sql);
if(!insert){
die("Could Not <u>Insert</u> Information");
}else{echo $sql;}
}