|
|
|
Many times an application will need to only update certain fields based on data entered by a user...for example a page that uses checkboxes to let the user choose what fields to update. The trick here is to check the various check boxes for a value and only add those items to the sql statement. In effect you are writing a SQL statement on the fly based on those values
In the below example, status1, status2...statusN are checkboxes that allow the user to choose what field to update. The status1date, status2date..statusNdate are the actual values to be added to the Db. In this example I make no effort to test the data before insertion. I am merely demonstrating how to effectively build SQL statements on the fly. In a production evironment, various checks should be run on the data to ensure safety of the Db.
| //base sql statement
$sql = update pipeline set ";
if (!empty($_POST['status1']))
$sql .= " ,status1date = '{$_POST['status1date']}' ";
}
if (!empty($_POST['status2']))
$sql .= " ,status1date = '{$_POST['status2date']}' ";
}
if (!empty($_POST['status3']))
$sql .= " ,status3date = '{$_POST['status3date']}' ";
}
if (!empty($_POST['status4']))
$sql .= " ,status1date = '{$_POST['status4date']}' ";
}
...
$sql .= " WHERE id= ". $_POST['loannum'];
$result = mysql_query ($query) or die(mysql_error(); // Run the query.
if ($result) // If it ran OK.
{
echo '<h3>Thank you for Submitting to the Pipeline</h3>';
} | | |
|
| email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Convert a File database into MySQL Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides | | | Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0. Categories : Databases, PHP, mSQL, Databases | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Newbie Notes #4 - Trapping dumb MySQL query errors Categories : PHP, Databases, MySQL, Debugging, Beginner Guides | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | for each record, do this to the first record, and do that to any subsequent record Categories : PHP, Databases, MySQL, Beginner Guides | | | How to Insert a Date Format Into MySQL from PHP Categories : PHP, Databases, MySQL, Date Time, Beginner Guides | | | Newbie Notes #10 - Generating drop downs Categories : PHP, MySQL, HTML, Beginner Guides, Databases | | | Making a simple Hit-Log using PHP and MySql Categories : PHP, Log Files, Beginner Guides, Databases, MySQL | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | |
|
|
|