Jump to content

Form posts but if fields are empty they overwrite existing fields


tribol

Recommended Posts

Okay, I have a form that pulls records for viewing on one page and editing on another. So on the edit page two of the fields (drop downs) that if left empty delete the existing data from the table. All of the other fields retain they're info though.

 

After searching and asking around someone told me to try :

 

if (is_null($_POST['field'])) { // skip entry }

 

Tried it but likely not properly. It breaks the form (and no i didn't include the {//skip entry}

 

Here is a shortened version of the page i'm editing, the two fields that are updating with null are consultantShop and consultantName ->

 

<?php
include("includes/header.php");
$section_db="RegistrationForms";

if ($_POST['action']=="Remove" && $_POST['id']) {
$query_update="UPDATE ".DB_DATABASE.".$section_db 
SET $section_db.active='N' 
WHERE $section_db.ID='".$_POST['id']."'";
secureSQLReport($query_update);

$messagegood="Event removed successfully!<br />";
} else if ($_POST['action']=="edit") {
foreach($_POST as $index=>$value) {
	$_POST[$index]=stripslashes(trim($value));
}

if (!$_POST['firstName1']) $error=$error."Missing first name.<br />";
if (!$_POST['lastName1']) $error=$error."Missing last name.<br />";
if (!$_POST['firstName2']) $error=$error."Missing first name.<br />";
if (!$_POST['lastName2']) $error=$error."Missing last name.<br />";


if (!$error) { 		
	$query_insert="UPDATE ".DB_DATABASE.".$section_db SET
	firstName1='".mysql_escape_string($_POST['firstName1'])."',
	lastName1='".mysql_escape_string($_POST['lastName1'])."',
	firstName2='".mysql_escape_string($_POST['firstName2'])."',
	lastName2='".mysql_escape_string($_POST['lastName2'])."',
	consultantShop='".mysql_escape_string($_POST['consultantShop'])."',
	consultantName='".mysql_escape_string($_POST['consultantName'])."',
	active='".mysql_escape_string($_POST['active'])."'
	WHERE $section_db.ID='".mysql_escape_string($_POST['id'])."'
	";
	secureSQLReport($query_insert);		
	$messagegood="Event successfully updated!";
}
}


?><link href="css/admin_style.css" rel="stylesheet" type="text/css">
<div id="edit-<?php echo $section_db; ?>-page" class="mod-container-big">
<?php if ($error) { ?><div class="message-bad"><?php echo $error; ?></div><?php } ?>
<?php if ($messagegood) { ?><div class="message-good"><?php echo $messagegood; ?></div><?php } ?>
  <h1>Edit » Event</h1>
  <div class="padding10"> 
<?php
if (($_GET['id'] && !$_POST['action']) || ($_POST['id'] && !$_POST['action']) || ($_POST['id'] && $_POST['action']=="edit" && $error) || ($_POST['id'] && $_POST['action']=="edit" && !$error)) {

		if ($_GET['id']) $id=$_GET['id'];
		if ($_POST['id']) $id=$_POST['id'];

		$query="SELECT * FROM ".DB_DATABASE.".$section_db WHERE $section_db.ID='$id'";
		$result=mysql_query($query);
		$record=mysql_fetch_object($result);
		$firstName1=$record->firstName1;
		$lastName1=$record->lastName1;
		$email1=$record->email1;
		$firstName2=$record->firstName2;
		$lastName2=$record->lastName2;
		$email2=$record->email2;
		$consultantShop=$record->consultantShop;
		$consultantName=$record->consultantName;
		$active=$record->active;
		secureSQLReport($query);

		if ($error) {
			$email1=$_POST['email1'];
			$firstName1=$_POST['firstName1'];
			$lastName1=$_POST['lastName1'];
			$email2=$_POST['email2'];
			$firstName2=$_POST['firstName2'];
			$lastName2=$_POST['lastName2'];
			$consultantShop=$_POST['consultantShop'];
			$consultantName=$_POST['consultantName'];
			$active=$_POST['active'];
		}

			//$consultantShop=(is_null($_POST['consultantShop']));


			//$consultantName=(is_null($_POST['consultantName']));

		if ($comments==0) $comments="no";
		if ($comments==1) $comments="yes";
?>
    <p><a href="index.php"><img src="icons/arrow_left.png" alt="Back" border="0" align="absmiddle"></a> 
      <a href="index.php">Back to entries</a> | <a href="<?php echo getenv("REQUEST_URI"); ?>"><img src="icons/arrow_refresh.png" alt="Refresh" border="0" align="absmiddle"></a> 
      <a href="<?php echo getenv("REQUEST_URI"); ?>">Refresh this page</a></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?=$id?>" method="post">
<input type="hidden" name="action" value="edit">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="hidden" name="date_added" value="<?php echo $date_added; ?>">
<table width="650" border="0" cellpadding="5" cellspacing="5">
      <tr>
        <td width="130" valign="top"> </td>
        <td valign="top"><strong class="required">*</strong> Required 
          fileds</td>
      </tr>

      <tr>
        <td valign="top"><input name="firstName1" type="text" class="medium-length-field" value="<?=$firstName1?>" maxlength="50" id="firstName1" />
            <em>maximum 50 characters</em></td>
      </tr>
      <tr>
        <td align="right" valign="top"><strong class="required">*</strong><strong><?=$brideGroomSelection1?>'s Last Name:</strong> </td>
        <td valign="top"><input name="lastName1" type="text" class="medium-length-field" value="<?=$lastName1?>" maxlength="50" id="lastName1" />
            <em>maximum 50 characters</em></td>
      </tr>
      <tr>
        <td align="right" valign="top"><strong class="required">*</strong><strong><?=$brideGroomSelection2?>'s First Name:</strong> </td>
        <td valign="top"><input name="firstName2" type="text" class="medium-length-field" value="<?=$firstName2?>" maxlength="50" id="firstName3" />
            <em>maximum 50 characters</em></td>
      </tr>
      <tr>
        <td valign="top"><input name="lastName2" type="text" class="medium-length-field" value="<?=$lastName2?>" maxlength="50" id="lastName2" />
            <em>maximum 50 characters</em></td>
      </tr>
  
  <?php if ($consultantShop) { ?>
  
  <tr>
  	<td align="right" valign="top"><strong>Shop Info:</strong> </td>
        <td valign="top">
		<?php
			$query2="SELECT * FROM ".DB_DATABASE.".shopData WHERE shopName='$record->consultantShop'";
			$result2=mysql_query($query2);
			$record2=mysql_fetch_object($result2);
			secureSQLReport($query2);
		?>
		<?=$consultantShop?><br />
		<?=$record2->address?>, <?=$record2->city?>, <?=$record2->province?>   <?=$record2->postalCode?><br />
		Phone: <?=$record2->phone?><br />
		Toll Free: <?=$record2->tollFree?><br />
		Fax: <?=$record2->fax?><br />
		Hours: <?=$record2->storeHours?><br />
		Pseudo: <?=$record2->pseudo?><br />		</td>
  </tr>
  <!--change shop-consultant worked here-->
  <tr>
  	<td align="right" valign="top"><strong>Consultant Info:</strong> </td>
        <td valign="top">
		<?php if ($consultantName) {
			$consultantName=explode(" ",$consultantName);
			$consultantFirst=$consultantName[0];
			$consultantLast=$consultantName[1];

			$query="SELECT * FROM ".DB_DATABASE.".shopsStaffDetails WHERE firstName LIKE '%$consultantFirst%' AND lastName LIKE '%$consultantLast%'";
			$result=mysql_query($query);
			$record=mysql_fetch_object($result);
			secureSQLReport($query);

			 if ($record->ID) {
			 	$query2="SELECT * FROM ".DB_DATABASE.".shopData WHERE shopName='$record->shopName'";
				$result2=mysql_query($query2);
				$record2=mysql_fetch_object($result2);
				secureSQLReport($query2);
			 }
		?>
		<?=$consultantFirst?> <?=$consultantLast?> <<a href="mailto:<?=$record->emailAddress?>"><?=$record->emailAddress?></a>><br />
		<?=$record->shopName?><br />
		<?=$record2->address?>, <?=$record2->city?>, <?=$record2->province?>   <?=$record2->postalCode?><br />
		Phone: <?=$record2->phone?><br />
		Toll Free: <?=$record2->tollFree?><br />
		Fax: <?=$record2->fax?><br />
		Hours: <?=$record2->storeHours?><br />
		Pseudo: <?=$record2->pseudo?><br />
		<?php } ?>		</td>
  </tr>
  <?php } ?>
	<tr>
  	<td align="right" valign="top"><strong>Change Shop:</strong></td>
  	<td valign="top"><select name="consultantShop" value="<?=$consultantShop?>">
	<option></option>
	<option value="Bank St">(DEBUG) Bank St</option>
	<option></option>
	<?php
		$query="SELECT * FROM ".DB_DATABASE.".shopData WHERE status='Open' AND shopType='Retail' ORDER BY shopName ASC";
		$result=mysql_query($query);
		while ($record=mysql_fetch_object($result)) {
			?><option value="<?=$record->shopName?>"><?=$record->shopName?></option><?php
		}
	?>
</select></td>
  	</tr>
  <tr>
  	<td align="right" valign="top"><strong>Change Consultant:</strong></td>
  	<td valign="top"><select name="consultantName" value="<?=$consultantName?>">
	<option></option>
	<option value="me@yahoo.com">(DEBUG) my name <me@yahoo.com></option>
	<option></option>
	<?php
		$query="SELECT * FROM ".DB_DATABASE.".shopsStaffDetails WHERE shopsStaffDetails.active='Y' AND shopsStaffDetails.position='Consultant' OR shopsStaffDetails.position='Team Leader' ORDER BY shopsStaffDetails.firstName ASC";
		$result=mysql_query($query);
		while ($record=mysql_fetch_object($result)) {
			?><option value="<?=$record->firstName?> <?=$record->lastName?>"><?=$record->firstName?> <?=$record->lastName?> <<?=$record->emailAddress?>></option><?php
		}
		secureSQLReport($query);
	?>
</select></td>
  	</tr>
      <tr>
  <tr>
		<td align="right" valign="top"><strong>Status:</strong></td>
      		<td valign="top">
			<select name="active">
				<option value="Active"<?php if ($active=="Active" || $active=="Y") echo " selected=selected";?>>Active</option>
				<option value="Pending"<?php if ($active=="Pending") echo " selected=selected";?>>Pending</option>
				<option value="Inactive"<?php if ($active=="Inactive" || $active=="N") echo " selected=selected";?>>Inactive</option>
				<option value="Closed"<?php if ($active=="Closed") echo " selected=selected";?>>Closed</option>
			</select>			</td>
      	</tr>
      <tr>
        <td align="right" valign="top"> </td>
        <td valign="top"><input type="submit" name="button" id="button" value="Submit" /></td>
      </tr>
    </table>
</form>
<?php
} else if ($_POST['id'] && $_POST['action']=="edit" && !$error) {
?>
<?php include("includes/action-menu.php"); ?>
<?php
}
	?>
</div>
</div>
<?php include("includes/footer.php"); ?>

 

Link to comment
Share on other sites

Before you update, pull the info for the page you are going to be updating into an array. If you encounter a null value fill that variable with whats there already then you will do a full rewrite of all the parts of that record in the table and will rewrite everything but rewriting the empty spots from the edit page with the data from the table that is already there... not that efficient but will get the job done.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.