Jump to content

updating mysql field through a form


marksie1988

Recommended Posts

im having a problem with a script ive done where a user can enter information into their profile like their skype or website etc, but if a user wants to remove their skype address or website it will not let them here is the php that writes the information to mysql

   function updateUserField($username, $field, $value){
      $q = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE username = '$username'";
      return mysql_query($q, $this->connection);
   }

 

and i also have the part of the form that the information is changed in

 

		<table width="550" border="0" cellspacing="3" cellpadding="0" class="signuptable">
		<tr class="on">
			<td width="200">
				<label for="skype">Skype</label>
			</td>
			<td>
				<input type="text" name="skype" maxlength="50" value="
					<?
						if($form->value("skype") == ""){
   								echo $session->userinfo['skype'];
						}else{
   								echo $form->value("skype");
						}
					?>">

			</td>	
		</tr>
		<tr class="on">
			<td width="200">
				<label for="myspace">MySpace</label>
			</td>
			<td>
				<input type="text" name="myspace" maxlength="50" value="
					<?
						if($form->value("myspace") == ""){
   								echo $session->userinfo['myspace'];
						}else{
   								echo $form->value("myspace");
						}
					?>">
			</td>	
		</tr>	
		<tr class="on">
			<td width="200">
				<label for="www">Website</label>
			</td>
			<td>
				<input type="text" name="www" maxlength="50" value="
					<?
						if($form->value("www") == ""){
   								echo $session->userinfo['www'];
						}else{
   								echo $form->value("www");
						}
					?>">
			</td>	
		</tr>						  			  
		<tr>
		    <td colspan="3"  align="center">
				<input type="hidden" name="subedit" value="1">
				<input type="submit" value="Edit Account">
			 </td>
		</tr>
			</table>

 

what i want is for someone to please look at the php that changes the database and tell me what i need to do to make it work when the value is changed back to null from the form.

 

i think i need something that says if $value = "" then set $field to null

 

Thanks

Steve

Link to comment
https://forums.phpfreaks.com/topic/73854-updating-mysql-field-through-a-form/
Share on other sites

Does the update work if they are changing their details to something other than nothing?

 

Also if you change:

mysql_query($q, $this->connection)

To:

mysql_query($q, $this->connection) or die(mysql_error());

 

And see if it returns an error.

 

Otherwise can you post the rest of the php script which calls the function.

mysql_query($q, $this->connection) or die(mysql_error());

is already set and no errors are shown

 

users can update details to something else, they just cant remove something completley.

 

the php which calls the function is:

 

first you fill in the form

		<table width="550" border="0" cellspacing="3" cellpadding="0" class="signuptable">
		<tr class="on">
			<td width="200">
				<label for="skype">Skype</label>
			</td>
			<td>
				<input type="text" name="skype" maxlength="50" value="
					<?
						if($form->value("skype") == ""){
   								echo $session->userinfo['skype'];
						}else{
   								echo $form->value("skype");
						}
					?>">

			</td>	
		</tr>
		<tr class="on">
			<td width="200">
				<label for="myspace">MySpace</label>
			</td>
			<td>
				<input type="text" name="myspace" maxlength="50" value="
					<?
						if($form->value("myspace") == ""){
   								echo $session->userinfo['myspace'];
						}else{
   								echo $form->value("myspace");
						}
					?>">
			</td>	
		</tr>	
		<tr class="on">
			<td width="200">
				<label for="www">Website</label>
			</td>
			<td>
				<input type="text" name="www" maxlength="50" value="
					<?
						if($form->value("www") == ""){
   								echo $session->userinfo['www'];
						}else{
   								echo $form->value("www");
						}
					?>">
			</td>	
		</tr>						  			  
		<tr>
		    <td colspan="3"  align="center">
				<input type="hidden" name="subedit" value="1">
				<input type="submit" value="Edit Account">
			 </td>
		</tr>
			</table>

 

then the form calls process.php

 

<?
/**
* Process.php
* 
* The Process class is meant to simplify the task of processing
* user submitted forms, redirecting the user to the correct
* pages if errors are found, or if form is successful, either
* way. Also handles the logout procedure.
*
* Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
* Last Updated: August 19, 2004
*/
include("include/session.blc");

class Process
{
   /* Class constructor */
   function Process(){
      global $session;
      /* User submitted login form */
      if(isset($_POST['sublogin'])){
         $this->procLogin();
      }
      /* User submitted registration form */
      else if(isset($_POST['subjoin'])){
         $this->procRegister();
      }
      /* User submitted forgot password form */
      else if(isset($_POST['subforgot'])){
         $this->procForgotPass();
      }
      /* User submitted edit account form */
      else if(isset($_POST['subedit'])){
         $this->procEditAccount();
      }
      /**
       * The only other reason user should be directed here
       * is if he wants to logout, which means user is
       * logged in currently.
       */
      else if($session->logged_in){
         $this->procLogout();
      }
      /**
       * Should not get here, which means user is viewing this page
       * by mistake and therefore is redirected.
       */
       else{
          header("Location: ../index.blc");
       }
   }

   /**
    * procLogin - Processes the user submitted login form, if errors
    * are found, the user is redirected to correct the information,
    * if not, the user is effectively logged in to the system.
    */
   function procLogin(){
      global $session, $form;
      /* Login attempt */
      $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember']));
      
      /* Login successful */
      if($retval){
         header("Location: ".$session->referrer);
      }
      /* Login failed */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }
   
   /**
    * procLogout - Simply attempts to log the user out of the system
    * given that there is no logout form to process.
    */
   function procLogout(){
      global $session;
      $retval = $session->logout();
      header("Location: ../index.blc");
   }
   
   /**
    * procRegister - Processes the user submitted registration form,
    * if errors are found, the user is redirected to correct the
    * information, if not, the user is effectively registered with
    * the system and an email is (optionally) sent to the newly
    * created user.
    */
   function procRegister(){
      global $session, $form;
      /* Convert username to all lowercase (by option) */
      if(ALL_LOWERCASE){
         $_POST['user'] = strtolower($_POST['user']);
      }
      /* Registration attempt */
      $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['pass2'], $_POST['email'], $_POST['location'], $_POST['rnme'], $_POST['skype'], $_POST['myspace'], $_POST['www'] );
      
      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else if($retval == 1){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
      /* Registration attempt failed */
      else if($retval == 2){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = false;
         header("Location: ".$session->referrer);
      }
   }
   
   /**
    * procForgotPass - Validates the given username then if
    * everything is fine, a new password is generated and
    * emailed to the address the user gave on sign up.
    */
   function procForgotPass(){
      global $database, $session, $mailer, $form;
      /* Username error checking */
      $subuser = $_POST['user'];
      $field = "user";  //Use field name for username
      if(!$subuser || strlen($subuser = trim($subuser)) == 0){
         $form->setError($field, "* Username not entered<br>");
      }
      else{
         /* Make sure username is in database */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < 5 || strlen($subuser) > 30 ||
            !eregi("^([0-9a-z])+$", $subuser) ||
            (!$database->usernameTaken($subuser))){
            $form->setError($field, "* Username does not exist<br>");
         }
      }
      
      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
      }
      /* Generate new password and email it to user */
      else{
         /* Generate new password */
         $newpass = $session->generateRandStr(;
         
         /* Get email of user */
         $usrinf = $database->getUserInfo($subuser);
         $email  = $usrinf['email'];
         
         /* Attempt to send the email with new password */
         if($mailer->sendNewPass($subuser,$email,$newpass)){
            /* Email sent, update database */
            $database->updateUserField($subuser, "password", md5($newpass));
            $_SESSION['forgotpass'] = true;
         }
         /* Email failure, do not change password */
         else{
            $_SESSION['forgotpass'] = false;
         }
      }
      
      header("Location: ".$session->referrer);
   }
   
   /**
    * procEditAccount - Attempts to edit the user's account
    * information, including the password, which must be verified
    * before a change is made.
    */
   function procEditAccount(){
      global $session, $form;
      /* Account edit attempt */
      $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email'], $_POST['location'], $_POST['rnme'], $_POST['skype'], $_POST['myspace'], $_POST['www']);

      /* Account edit successful */
      if($retval){
         $_SESSION['useredit'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else{
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
   }
};

/* Initialize process */
$process = new Process;

?>

 

after editing it is supposed to tell you if it is succesfull or not and it always says succesfull even though it isnt if you delete the whole entry

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

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