Php Programming Code Examples
Php > Database Related Code Examples
Insert data into mysql table
Insert data into mysql table
<?php
function fixQuotes ($what = "")
{
$what = ereg_replace("'","''",$what);
$counter = 0;
while (eregi("\\\\'", $what) && $counter < 10)
{
$what = ereg_replace("\\\\'","'",$what);
#print "<p>counter=[$counter] what=[$what]</p>\n";
}
return $what;
};
function insertData($inputTable, $inputField, $inputValue)
{
$i = 1;
$fieldList = "";
$valueList = "";
$fieldCount = 0;
$fieldCount = count($inputField);
foreach ($inputField as $v)
{
$fieldList = $fieldList . $v;
$i++;
if ($i <= $fieldCount)
{
$fieldList = $fieldList . ", ";
};
};
# reset local vars
$i = 1;
reset($inputField);
foreach ($inputValue as $v)
{
$v = fixQuotes($v);
$valueList = $valueList . chr(39) . $v . chr(39);
$i++;
if ($i <= $fieldCount)
{
$valueList = $valueList . ", ";
};
};
$query = "INSERT INTO " . $inputTable . " (" . $fieldList . ") VALUES (" . $valueList . ");";
print "<br />";
#print $query;
$result = mysql_query($query);
if (!$result)
{
print "<p>Insert failed, dammit.</p>\n";
}
else
{
#print "<p>Inserted ok.</p>";
};
};
?>