Hello all,

I have about 2 months experience working with php so please if you answer to my post try not to use too technical term or lingo only a season program would understand. I would appreciate that very much. Also English is not my native language, so please be warned lol. Just ask me to re-phrase my question if it make no sense to you.

my question:

I have a profile form which include a set of 3 fields used to enter a Canadian social insurance number. there are 9 number total. 3 numbers per field. this is necessary because I picked up a neath little javascript validation utility that verify if the SIN is genuine. So after the user enter the SIN I then concatenate the 3 fields into one and save it into my database with "-" separators.

Now I am scratching my head trying to figure out how to bring that concatenated sin number from the database back into the 3 original fields on the form when a user wants to update their profile. I think using the explode function will do that but I have problem finding a way to name the variable array that will be used to insert into the form.

i been trying to echo into the first input as a test and what I have doesnt' work.

Social Insurance Number <input type = "text" id = "S1" name="S1" size =" 3" maxlength = "3" onkeyup = "validate(this,2)" value=<?php if (isset($_POST['sin']) ){echo explode("-",$sin)['0']}?>>
<input type = "text" id = "S2" name="S2" size =" 3" maxlength = "3" onkeyup = "validate(this,3)">
<input type = "text" id = "S3" name="S3" size =" 4" maxlength = "3" onkeyup = "validate(this,3)">

this is how I concatenated my values:

$sin1 	= $_POST['S1'];
$sin2	= $_POST['S2'];
$sin3	= $_POST['S3']; 

$sin	= $sin1 ."-". $sin2 ."-". $sin3;

this is my calling function:

$users->update_user($first_name, $middle_name, $last_name, $gender, $dob, $sin, $bio, $image_location, $user_id);
					header('Location: settings.php?success');
					exit();

This is my user update class

public function update_user($first_name, $middle_name, $last_name, $gender, $dob, $sin, $bio, $image_location, $id){

	$query = $this->db->prepare("UPDATE `users` SET
							`first_name`	= ?,
							`middle_name`	= ?,
							`last_name`		= ?,
							`gender`		= ?,
							`dob`			= ?,
							`sin`			= ?,
							`bio`			= ?,
							`image_location`= ?

							WHERE `id` 		= ? 
							");

	$query->bindValue(1, $first_name);
	$query->bindValue(2, $middle_name);
	$query->bindValue(3, $last_name);
	$query->bindValue(4, $gender);
	$query->bindValue(5, $dob);
	$query->bindValue(6, $sin);
	$query->bindValue(7, $bio);
	$query->bindValue(8, $image_location);
	$query->bindValue(9, $id);

	try{
		$query->execute();
	}catch(PDOException $e){
		die($e->getMessage());
	}
	

}

    list($s1, $s2, $s3) = explode('-', $sin);

    And use [font=monospace]$s1[/font] etc., to populate the form fields.

      Weedpacket;11037079 wrote:
      list($s1, $s2, $s3) = explode('-', $sin);

      And use [font=monospace]$s1[/font] etc., to populate the form fields.

      thank you for replying back. Where do I insert this line of code? in my page below the script that I have already or in the user_update function in my user class?

        After you get the value of [font=monospace]$sin[/font] from the database but before you use the values of [font=monospace]$s1[/font], etc. in the form fields.

          I tried at several places but can't seem to get those three fields to update. it's a complicated one alright.

            <input type = "text" id = "S1" name="S1" size =" 3" maxlength = "3" onkeyup = "validate(this,2)" value=<?php if (isset($_POST['sin']) ){echo explode("-",$sin)['0']}?>> 
            

            Looking at this part of your attempt, you are trying to reuse post data, i.e. the $_POST array, which is the data sent from the browser to your server when the user posts/submits the form. This will only work if you redisplay the page right after the form was submitted.

            But nowhere in the code you showed us is there a call to the database to retrieve the data after it has been stored. So if this is your call, you'd have to first issue a query along the lines of

            SELECT some, stuff FROM users WHERE ...
            

            and then use the retrieved data together with explode.

            It often helps to break down the problem into smaller bits to figure out which parts are working and which are not. For example, you could create a new file which has no connection to anything else containing

            $sin = '123-456-789';
            list($s1, $s2, $s3) = explode($sin);
            printf('
                <input type="text" value="%d"> - 
                <input type="text" value="%d"> - 
                <input type="text" value="%d">
                $s1,
                $s2,
                $s3
            );
            

            to make sure you get the exploding and output right. Then you can insert that part into whatever page you are trying to build. And when that is done, you can go about retrieving the actual values from the database.

              johanafm;11037133 wrote:
              It often helps to break down the problem into smaller bits to figure out which parts are working and which are not. For example, you could create a new file which has no connection to anything else containing
              [code=php]
              $sin = '123-456-789';
              list($s1, $s2, $s3) = explode($sin);
              printf('
                  <input type="text" value="%d"> - 
                  <input type="text" value="%d"> - 
                  <input type="text" value="%d">
                  $s1,
                  $s2,
                  $s3
              );
              

              to make sure you get the exploding and output right. Then you can insert that part into whatever page you are trying to build. And when that is done, you can go about retrieving the actual values from the database.

              yes, I actually created a prototype exactly like the one you described and it worked like a charm. When I try to implement the code into my real form and script I get nowhere. I have been working on this for the last 5 days or roughly 30 intensive hours of reading, searching and in various forums and I still can't get the damn values to show in the field S1 S2 S3. I just can't deal with this anymore so I decide to enter the Raw Sin number into my database without validation and be done with it. Maybe one day in the future When I am better at PHP I will return back to it and do it right but right now, I am fed up with it and I have to move on to something else otherwise I am going to punch the light out of my computer screen hahahahaa.

                In that case, provide us with the code you are trying to get to work. Include error messages and notices if present.

                  Gosh jahanafm, I really appreciate you trying to help me out on this. the message I was getting was that an array was expected but none was available. I tried your code and a good dozen more from really cool people who took the time to look over my codes but at the end I never was able to get it to work and decided that I had spent enough time on this. none of what I been trying to do with the explode function worked and still not working. I have the same exact problem getting multi-checkbox to show in my form and stuck with the same explode function that doesn't work. I think my explode function in my computer is broken hahahaha. I am not going to spend another 30 hours trying to figure out how to display the checkboxes in my form this time around though. if I can't make it work by the end of the day I will delete the darn explode function from my computer and find a substitute lol. is there such a thing as programer hating a function? if not well I am making a precedent, I hate explode function hahahaha

                    Gosh jahanafm, I really appreciate you trying to help me out on this. the message I was getting was that an array was expected but none was available. I tried your code and a good dozen more from really cool people who took the time to look over my codes but at the end I never was able to get it to work and decided that I had spent enough time on this. none of what I been trying to do with the explode function worked and still not working. I have the same exact problem getting multi-checkbox to show in my form and stuck with the same explode function that doesn't work. I think my explode function in my computer is broken hahahaha. I am not going to spend another 30 hours trying to figure out how to display the checkboxes in my form this time around though. if I can't make it work by the end of the day I will delete the darn explode function from my computer and find a substitute lol. is there such a thing as programer hating a function? if not well I am making a precedent, I hate explode function hahahaha

                      Write a Reply...