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.

hai i'm trying to populate a table by inserting multiple rows by one for each query as i'm sending an array elements following is my code:

The mark up:

<form action="post.php" method="POST">
    <h1>user sign up</h1>
    name:<input type="text" name="name" id="name"/>
    password:<input type="text" name="pass" id="pass"/>

    <div id="container">
        <a href="#"><span id="add_field">» Add your list...</span></a><br/><br/>
    </div>
    <input id="go" class="btn" name="btnSubmit" type="submit" value="Signup" />
</form>

the script:

<script>
var count = 0;
    $(function(){
        $('#add_field').click(function(){
            count +=1;
            //this is for appending input type="text" with the name of fields[]
            // example like this: <input id="field_"+the incremented value (1,2,3..)+"name="fields[]" type="text"/>
            $('#container').append('<strong>Link #' + count + '</strong>'+ '<input id="field_' + count + '" name="fields[]' + '" type="text" /><br/>' );
        });
    });
</script>

the php:

<?php  
$db = mysqli_connect("localhost","root","","test");
error_reporting(E_ALL);
//on click of the submit button
if(isset($_POST['btnsubmit'])){
    //if the fields are not empty
        if(!empty($_POST['fields']) && !empty($_POST['name']) && !empty($_POST['pass'])){
            //the array / dynamic field
            $fields = $_POST['fields'];
            //the other two normal fields
            $user = $_POST['name'];
            $pass = $_POST['pass'];

            //looping through and inserting the rows in table  
            foreach($fields as $key=> $value){
                $query = mysqli_query($db,"INSERT INTO sample VALUES('','$user','$pass','$value')");
                if($query){
                    echo "added <br/>";
                } else {
                    echo "try";
                }
            }//end of foreach loop
            echo "user added";
        } else {
            echo "fill the web fields";
        }//end of the !empty if statement
}//end of first if
?>

here i would like to populate the table like the following

if there is only one $_POST['fields'] typed

id | username   | password | field
----------------------------------------
1 | name        | password | field value

if there are more than one $_POST['fields'] typed

id | username   | password      | field
------------------------------------------------
 1 | name       | password      | field value 1
 2 | name 2     | password 2    | field value 2
 3 | name 3     | password 3    | field value 3

so on (depending upon the array size/... )

share|improve this question
3  
AAAANNNNDDDD what is the problem??? –  Let me see Dec 16 '13 at 13:28
    
it's not inserting the datas –  user3104743 Dec 16 '13 at 14:11
add comment

1 Answer

I guess your question is how to do it since the code doesn't make sense. You're almost there just change this

mysqli_query($db,"INSERT INTO sample VALUES('','$user','$pass','$value')");

into this:

mysqli_query($db,"INSERT INTO sample (user, pass, value) VALUES(" . 
    mysql_real_escape_string($user) . "," .
    mysql_real_escape_string($pass) . "," .
    mysql_real_escape_string($value) . 
")");

Change (user, pass, value) to the column names you use.

share|improve this answer
    
thank you it worked :D –  user3104743 Dec 16 '13 at 14:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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