...

View Full Version : If blank then dont Update



bucket
11-04-2009, 11:42 PM
How do I do it so when this page gets run by itself it doesn't update the table, since if it does it will just make all the fields blank.


<?php
require_once ('inc/config.php');
$firstname = mysql_real_escape_string ($_POST['firstname']);
$lastname = mysql_real_escape_string ($_POST['lastname']);
$middlename= mysql_real_escape_string ($_POST['middlename']);
$id = 2;
$sql = mysql_query ("
UPDATE `testing` SET
`FirstName` = '".$firstname."',
`LastName` = '".$lastname."',
`MiddleName` = '".$middlename."'
WHERE `id` = '".$id."'
")
OR die (mysql_error());
?>

Instead make it update the table only when the form updates it?

Fumigator
11-05-2009, 12:34 AM
Check to see if $_POST is set, or is an array, and additionally, a popular method is to check the SUBMIT button's $_POST index to see if it's set and even if the value of it equals the submit button value on the html form.

Fou-Lu
11-05-2009, 12:40 AM
Yes, check a particular value. I always go for the submission button. You can also check for the request method, though that does not guarentee that the values will be set.
_POST is however always available regardless of if it is a posted form, and will always be an array, so you cannot check for isset on post, or if is_array, though you can check for !empty. I'm fairly certain about this, I remember a previous CLI run on phpinfo(INFO_VARIABLES); pulled up an empty array from post. Everything actually, except for sessions.

Fumigator
11-05-2009, 04:30 PM
_POST is however always available regardless of if it is a posted form, and will always be an array, so you cannot check for isset on post, or if is_array

Crap you're absolutely right... :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum