0

how can i insert loop value into database stored all values into array please take a look and suggest me

$data['name'] = mysql_real_escape_string($_POST['name']);
    $data['email'] = $_POST['email'];
    $data['password'] = $_POST['password'];
    $data['gender'] = $_POST['gender'];
    $data['dob'] = $_POST['dob'];
    $data['address'] = $_POST['address'];
    $data['country'] = $_POST['country'];
    $data['hobbie'] = $_POST['hobbie'];

$checkElement = "";
    foreach($data['hobbie'] as $check=>$chkValue){
        $checkElement = $chkValue.","."<br>";

        echo $data['hobbie']= $checkElement;
// here i am getting all value

    }
//outside of loop i am getting only last value
3
  • Your loop is only referencing the 'hobbie' index of your array: foreach($data['hobbie'] as $check=>$chkValue){ - is this expected? Commented Sep 7, 2015 at 17:17
  • yes i jst want to get the values into $data['hobbie'] what ever in the checkboxes array so that i can directly inserted into table Commented Sep 7, 2015 at 17:47
  • how you can directly insert into table with $data['hobbie'];. Since, you don't know how many hobbies are selected. You need to have for loop for multiple selected checkbox. Insertion will be done inside for loop only. Commented Sep 7, 2015 at 17:49

1 Answer 1

1
$data['name'] = mysql_real_escape_string($_POST['name']);
$data['email'] = $_POST['email'];
$data['password'] = $_POST['password'];
$data['gender'] = $_POST['gender'];
$data['dob'] = $_POST['dob'];
$data['address'] = $_POST['address'];
$data['country'] = $_POST['country'];
$Hobbie = $_POST['hobbie'];

$TotalCheckedHobbie=sizeof($Hobbie);
$Hobbies = "";
for($i=0;$i<$TotalCheckedHobbie;$i++)
{
    $Hobbie=$Hobbie[$i];
    $Hobbies=$Hobbie.",";
}
echo $Hobbies;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi danish I have already created a function for insertion now i am getting Array ( [name] => Chrish [email] => [email protected] [password] => 222 [gender] => male [dob] => 08/05/1988 [address] => Mumbai [country] => India [hobbie] => Array ( [0] => Cricket [1] => Hockey [2] => Badminton [3] => Volleyball ) ) these value bt in India[hobbie] i dont want array in want only value that can insert into direct.
@user4464505: Edited. Check it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.