PHP/MySQL

how to capture all fields and values of a form at once in php?

Sometime we may have many fields in a form. We can get all field names and corresponding values at once in php nicely and field names which can be used as variables later.

For eg, you would like to save into database then create a table and make all database filed names same as form’s field names.

CREATE TABLE `student` (
`id` INT NOT NULL AUTO_INCREMENT ,
`first_name` INT NOT NULL ,
`last_name` INT NOT NULL ,
PRIMARY KEY ( `id` )
)

I have a form as given below:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name : <input type="text" name="first_name"><br />
Last Name : <input type="text" name="last_name"><br />
<input type="submit" name="submit" value="Submit" >
</form>

While you submit the form, capture all variables, values and create SQL query to save into database:

if(!empty($_POST)){
	foreach($_POST as $variable => $key){
		${$variable} = trim($key);
	}
        $sql = "INSERT INTO `student` (`first_name` ,`last_name`)
       VALUES ( '".$first_name."', '".$last_name."');"
}

How to define dynamic variable in php?

If you need to define variable dynamically in php then this can be done easily.
Say, your variable name will be ‘dynamic_variable_name’

Generally we define php variable normally as $normal_variable; but in this case we would like to create dynamically.
Then we have to write something like following way:

$normal_variable = 'dynamic_variable_name';
${$normal_variable} = 'New dynamic variable created';
echo $dynamic_variable_name;
// prints New dynamic variable created.

To explain it better I am giving an instance which will be very helpful to develop PHP sites and reduce some hard work.

I have a form as given below:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name : <input type="text" name="first_name"><br />
Last Name : <input type="text" name="last_name"><br />
<input type="submit" name="submit" value="Submit" >
</form>

While you submit the form, capture all variables and values:

if(!empty($_POST)){
	foreach($_POST as $variable => $key){
		${$variable} = trim($key);
	}
        echo $first_name . '  ' . $last_name;
}

How to upgrade magento from ver.1.3.x to ver.1.4.x

Today, I am going to explain how I have upgraded magento from version 1.3.0 (pretty old :) ) to version 1.4.1.0 using the same old database.

As we know its a pain while we upgrade magento each time from one version to another.

Well follow the steps as written below, hope your magento site will run without any errors.

  1. First of all, take backups, and run magento on testing server/ local machine, in my case it was version 1.3.0 and installed directory was “old_magento”
  2. Now unzip fresh version of magento 1.4.1.0 in a separate directory, say, “new_magento” directory
  3. Copy all directory/files from new_magento to old_directory, obviously replace everything with new files
  4. Now run this old_magento directory (http://127.0.0.1/old_magento), this will take time as it will reconfigure your database.
  5. In order to avoid time-out error, increase php execution time, I set it to 3600 seconds, memory 2048, I had around 1000 products with many custom attributes, categories and product images
  6. After successful database upgrade, site should run, but we may see some php fatal errors on both admin panel and frontend respectively given below
    Fatal error: Undefined class constant 'Mage_Customer_Model_Group::ENTITY' in xxx\app\code\core\Mage\Core\Model\Config.php  on line 1206
    
    Fatal error: Call to a member function toHtml() on a non-object in xxx\app\code\core\Mage\Core\Model\Layout.php on line 529
  7. Now delete everything from old_magento directory except media directory, rename media to media_old
  8. Again copy fresh files / directories from new_magento to old_magento directory, rename directories media to media_temp and media_old to media
  9. Run again (http://127.0.0.1/old_magento your path may be different)
  10. Now you would see magento installation screen, use old database name, user and password etc properly.
  11. This installation should not take too much time, after installation go to frontend and admin panel

This trick worked for me smoothly with 1000 records, although you have to check your custom theme so that it becomes compatible with version 1.4.x that is another story :)