UPDATING A RECORD ONLY WHERE AN INPUT FIELD MATCHES A RECORD FIELD VALUE - Dec 17th, 2011


If you need to update a record in a table I’ll call customer_uploads only if the value of a variable (I’ll call that
$order_code for this example) matches the value in the existing record’s order_code field, here’s how:

First create a field in your form where the value can be entered. (There are other variables and fields but I left them
out for clarity.)


<tr>
<td valign="top" class="title" height="30">Activation Code:</td>
<td><input type="text" name="order_code">
</td>
</tr>


Then in your update code add a where clause:



mysql_query("UPDATE `{$TABLE_PREFIX}customer_uploads` SET

first_name = '".mysql_escape( $_REQUEST['first_name'] )."',
last_name = '".mysql_escape( $_REQUEST['last_name'] )."',
street_address = '".mysql_escape( $_REQUEST['street_address'] )."'

// Add WHERE clause to filter results
WHERE order_code = '".mysql_escape($order_code)."'")

or die("MySQL Error updating Record:\n". htmlspecialchars(mysql_error()) . "\n")
$userNum = mysql_insert_id();


And don’t forget that there’s no comma after the last field to be updated.





The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service