Jump to content

Update all columns from html post. Simple? (MYSQL/PHP)


lindm

Recommended Posts

Hoping there is a simple solutions to this. I have a form with around 400 fields. When the form is submitted a php script handles this and updates/inserts the record into a mysql database. Now...is there a simple script to update all fields or must this be handled for every field like:

 

mysql_query("UPDATE tableX SET field1 = '$var', field2 = '$var2' etc..

 

Thanks

Are the form field names the same as your mysql's tables field names?

 

If so you can do something like...

 

<?php

   function updateTable($data){
        $q = "UPDATE your_table SET ";

        $num=count($data);
$count=0;								
foreach($data as $key=>$value){
$count++;
	if($count<$num){
		$q .=$key."='".$value."', ";
	}
	else{
		$q .=$key."='".$value."' ";
	}

            $q .="WHERE fieldyouchoose='whatever'";

}
?>


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.