Jump to content

Update using database field names


Scooby08

Recommended Posts

I have a dynamically generated form from the database.. How can I write a dynamically generated update query using the database fieldnames? Here is what I have to select the fieldnames:

 

<?php
$query_field_ci = "SELECT field_form_name FROM dw_fields WHERE field_category = 'Customer Information' ";
$result_field_ci = mysql_query($query_field_ci) or die(mysql_error());
while ($row_field_ci = mysql_fetch_array($result_field_ci)) {
    $customer_field_names[] = $row_field_ci['field_form_name'];
}
?>

 

The $customer_field_names array is like so:

 

Array
(
    [0] => customer_firstname
    [1] => customer_lastname
    [2] => customer_address1
    [3] => customer_address2
    [4] => customer_city
    [5] => customer_state
    [6] => customer_zip
    [7] => customer_phone
    [8] => customer_email
)

 

Now I just need to figure out how to put those into this update query:

 

<?php
$query_ci  = "UPDATE dw_customers ";
$query_ci .= "SET ";
$query_ci .= " "; // lost here???
$query_ci .= "WHERE customer_id = '$_POST[customer_id]' ";
?>

 

The final output would be something like so:

 

<?php
$query_ci  = "UPDATE dw_customers ";
$query_ci .= "SET ";
$query_ci .= "customer_firstname = '$_POST[customer_firstname]',customer_lastname = '$_POST[customer_lastname]', ....... "; // like this, but not hardcoded.. to be pulled from database
$query_ci .= "WHERE customer_id = '$_POST[customer_id]' ";
?>

 

Hope this makes sense cuz I'm outta ideas..  Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/122644-update-using-database-field-names/
Share on other sites

No need to generate the field names from a SELECT statement if your form elements have the same name. You could simply use something like....

 

<?php

  $sql = "UPDATE dw_customers SET ";
  foreach ($_POST as $k => $v) {
    if ($k != 'submit') {
      $sql .= "$k = '" . mysql_real_escape_string($v) . "',";
    }
  }
  $sql = rtrim($sql,',');
  $sql .= " WHERE customer_id = '" mysql_real_escape_string($_POST['customer_id']) . "'";

?>

Excellent idea.. I do have a couple of problems with this still.. First, I get this error:

 

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /home/content/.../edit-customer.php on line 347

 

Has something to do with this line:

 

<?php
$sql .= "$k = '" . mysql_real_escape_string($v) . "',";
?>

 

And the other problem is that there are a few different sections of fields on this page that need to be separate from each other.. Example, I have customer information that goes into the customers table, then company information that goes into another table, and a couple others as well.. How do I split up those $_POST into sections on this line:

 

<?php
foreach ($_POST as $k => $v) // need for each customer information fields value
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.