Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I'm having difficulties inserting php array into mysql database. Could you please see what am I doing wrong? how i can insert this data

My HTML form :

    <form method="post" action="edu.php">
    <label for="firstname">Firstname : </label>
    <input type="text" id="firstname" name="firstname" />
    <br><br>
    <label for="surname">Surname : </label>
    <input type="text" id="surname" name="surname" />
    <br><br>
    <label for="gender">Gender : </label>
    <select id="gender" name="gender">
        <option>Male</option>
        <option>Female</option>
    </select>
    <br><br>
    <input type="submit" value="Submit" />
    <input type="hidden" value="submit" />
</form>

PHP CODE :

if(isset($_POST['submit'])){
   $array = array(
     array(
      'id' => 'xx', /* i want this id [Auto]  */
      'firstName' => 'xxxx', /* i want this Dynamically form $_POST['firstname']  */
      'surname'   => 'xxxx', /* i want this Dynamically form $_POST['surname']   */
      'gender'    => 'xxxx', /* i want this Dynamically form $_POST['gender']    */
         )
    );
    /* `education` tbl_structure   */
    /*  edu_id  int(11)   AUTO_INCREMENT */
    /*  Education   (longtext)   */
    $array_string=mysql_escape_string(serialize($array));  
    $insert = mysql_query("insert into education (Education) values($array_string)"); 
      if($insert){
           echo "Done";
         }else{
           echo "Error";
          }
       }
share|improve this question

marked as duplicate by Elliott Frisch, Shankar Damodaran, Fred -ii-, samitha, Mike W May 11 at 4:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
You should use mysqli and prepared statements. There are some good examples in the documentation: php.net/manual/en/mysqli.quickstart.prepared-statements.php –  Cully Larson May 10 at 22:36
    
    
thanks for answer but i want more explain if you can :) –  user3623578 May 10 at 22:46
    
Is there something specific about the information in those examples that is confusing? If so, edit your answer to include any confusion. I'll see if I can help :) –  Cully Larson May 10 at 22:52
    
The main issue here is that your entire execution is based on and is dependant of the conditional statement. What you haven't done is name your form element in question that's related to it. –  Fred -ii- May 11 at 3:46

1 Answer 1

up vote -1 down vote accepted
if(isset($_POST['submit'])){
   $array = array(
      'firstName' => $_POST['firstname'], /* i want this Dynamically form $_POST['firstname']  */
      'surname'   => $_POST['surname'], /* i want this Dynamically form $_POST['surname']   */
      'gender'    => $_POST['gender'], /* i want this Dynamically form $_POST['gender']    */

    );
    /* `education` tbl_structure   */
    /*  edu_id  int(11)   AUTO_INCREMENT */
    /*  Education   (longtext)   */
    $array_string=mysql_escape_string(serialize($array));  
    $insert = mysql_query("insert into education (Education) values($array_string)"); 
      if($insert){
           echo "Done";
         }else{
           echo "Error";
          }
       }

I assume you have connected to your database in the earlier part of script.

It's also rather not the best way to put serialized data into MySQL table. You should rather create separate columns and put there data and not serialize array. Databases are used for searching and choosing some data and in your case searching won't be very easy and you won't be able to choose only part of data.

You should also use PDO or mysqli and not mysql (it's deprecated).

share|improve this answer
    
This really isn't helpful, for the very reasons you pointed out in your answer. It uses deprecated methods, serialized data, and there are already tons of answers to this question on SO and the rest of the internet. –  Cully Larson May 10 at 22:39
    
I cannot agree. I answer the question and point out disadvantages of using it. Should I maybe hack on his PC, change database schema and insert data into his database? Come on... –  Marcin Nabiałek May 10 at 22:41
    
This question has already been asked a multitude of times. Giving yet another answer, and one that shouldn't actually be followed, doesn't help improve Stack Overflow as a repository of useful information. –  Cully Larson May 10 at 22:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.