Hi,
I am facing some problems converting the PHP SQL insertion code below to ASP, I want to use the same concept in ASP, because it is more flexible, not tied to ASP owned keywords.
Wonder if anyone can help ASP newbie here.
$table = 'tbluser';
$array = array (
'name' => $name,
'email' => $email,
'ip' => $remoteIP
);
$newid = insert_query ($table, $array);
function insert_query ($table, $array)
{
global $mysql_errors;
$query = 'INSERT INTO ' . $table . ' ';
foreach ($array as $key => $value)
{
$fieldnamelist .= '' . '`' . $key . '`,';
if ($value == 'now()')
{
$fieldvaluelist .= 'now(),';
continue;
}
else
{
$fieldvaluelist .= '\'' . $value . '\',';
continue;
}
}
$fieldnamelist = substr ($fieldnamelist, 0, 0 - 1);
$fieldvaluelist = substr ($fieldvaluelist, 0, 0 - 1);
$query .= '' . '(' . $fieldnamelist . ') VALUES (' . $fieldvaluelist . ')';
$result = mysql_query ($query);
if (!$result)
{
$message = 'Invalid query: ' . mysql_error () . '<br>';
$message .= 'Whole query: ' . $query;
}
$id = mysql_insert_id ();
return $id;
}
it's classic ASP. No choice, company still need to maintain this for a specific project.
The main reason I want this specific code style in ASP is because I don't want to tie to the ASP style of querying database (using cmd and etc...)
By creating array collection with key(table fieldname) and value, then convert into a full sql string, I only need to run .execute(sql) , it means in future I can more easier convert the project to other framework, without needing to rewrite the way to insert/update data into DB.