We are moving from ASP over to PHP and we had code that would grab the text fields of a form and then cycle through and insert the values of the forms fields into a SQL table with the same names.
We also had some management fileds in the table so we wanted to skip the last 7 fileds in the table. The ASP code was something like this:
'Dynamically builds fields and values to insert into database
Set rs = cn.Execute("select top 1 * from tablename")
sql = "insert into tablename ( "
'Exclude the first 7 fields
for i = 0 to rs.fields.count -8
sql = sql + rs.fields(i).name + ", "
next
sql=left(sql,len(sql)-2)
sql = sql + " ) values( "
'Will not include the first 7 column fields
for i = 0 to rs.fields.count -8
if rs.fields(i).type = 2 then
sql = sql + ChkString(request.form(rs.fields(i).name)) + ", "
else
sql = sql + "'" + ChkString(request.form(rs.fields(i).name)) + "', "
end if
next
sql=left(sql,len(sql)-2)
sql = sql + " ) "
rs.Close
Set rs = Nothing
cn.Execute(sql)
Could any one asist with the best way to convert this over to PHP?
We started with something like:
$sql = "INSERT INTO TABLENAME( " ;
// Identify SQL Table Columns
foreach ($_POST as $key => $value)
{ // Cycle through all the Form Data Submitted
if (!is_array($value))
{ // If not an Array (e.g. Textbox)
$sql .= $value . ", " ;
}
else
{ // Account for Array Form Data (e.g. Checkboxes)
foreach ($_POST[$key] as $itemvalue)
{
$sql .= $itemvalue . ", " ;
}
}
}
// PENDING HERE: ADD CODE TO TRIM RIGHT SIDE
$sql .= $sql . " ) values( " ;
// Identify and Store SQL Table Column Values
foreach ($_POST as $key => $value)
{ // Cycle through all the Form Data Submitted
if (!is_array($value))
{ // If not an Array (e.g. Textbox)
$sql .= $value . ", " ;
}
else
{ // Account for Array Form Data (e.g. Checkboxes)
foreach ($_POST[$key] as $itemvalue)
{
$sql .= $itemvalue . ", " ;
}
}
}
// PENDING HERE: ADD CODE TO TRIM RIGHT SIDE
$sql .= $sql . " ) " ;
Having some issues with ignoring the last 7 management fields that are in the table. Thanks.