views:

42

answers:

1

I'm trying to execute I have an html form in a page of this sort :

Name: <input type="text" id="namebox" value="" name="fields[]" /> <br />
Position: <input type="text" id="positionbox" value="" name="fields[]" /> <br />

<input type="hidden" name="createcard">     
<input type="submit" value="Create">

.. and 3 other fields. I'm passing these 5 form fields by POST to a file process.php which has the following function to insert the array elements into a mysql DB.

if(isset($_POST['createcard'])){
    $this->procCreateCardtheme1();
..
..

    function procCreateCardtheme1(){
    global $session;

            $cardData = $_POST['fields'];
            $username=$session->username;
            $sno=1;
            echo count($cardData);
            while($sno < count($cardData))
            {
             $v=$cardData[$sno];
             echo $v;
             mysql_query("INSERT INTO carddata VALUES ('$username', $sno, '$v', 1)");
             $sno++;
            }

Now, the echo statement above returns the expected output, that is the five or so fields. But the mysql_query only executes once. It just stores the first entry in the DB, and nothing else. Even re-submitting the form does nothing at all. It's just the one entry that is stored in the DB. Any ideas?

+2  A: 

Do you have a unique constraint on username in the carddata table? This will cause the second insert to fail.

To debug this you should add some error checking to your program:

mysql_query("INSERT INTO carddata VALUES ('$username', $sno, '$v', 1)")
    or trigger_error(mysql_error());

You might also need to use mysql_real_escape_string to avoid syntax errors or possible SQL injection vulnerabilities if the string data can contain quotes.

Mark Byers
I want it to be the same username. I just need the array values to be changing. i.e. $cardData[0], $cardData[1] and so on..
Hardik Ruparel
@Hardik Ruparel: Can you post the output of `SHOW CREATE TABLE carddata` in your question?
Mark Byers
@Hardik Ruparel: You initialize `$sno` with `1`. I think you should initialize it with `0`.
Felix Kling
Yes, it was set to unique! Thanks so much!
Hardik Ruparel
@Felix. Yep. I noticed that too. Thanks :)
Hardik Ruparel