0

I have mysql data base in which i am adding data in mysql data base but problem is that it only stores only one record not more than that.

my table structure is

    <?php
    $con =  mysql_connect("example.com","name","password");

    if (!$con)
     {
     die('Could not connect: ' . mysql_error());
     }

     mysql_select_db("surveyipad", $con);

     $response_id=$_POST['response_id'];

     $participant_id=$_POST['participant_id'];


     $question_id=$_POST['question_id'];



     $answer_text=$_POST['answer_text'];


     $answer_option=$_POST['answer_option'];


     $query=("INSERT INTO survey_question_responses (response_id,participant_id,question_id,answer_text,answer_option)

      VALUES ('', '$participant_id','$question_id','$answer_text','$answer_option')");

       mysql_query($query,$con);
       printf("Records inserted: %d\n", mysql_affected_rows());

     echo($response_id)
   ?>

response id is primary key in table and also set to auto increment

Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27
user1619187
  • 529
  • 2
  • 6
  • 8
  • Could you change "mysql_query($query,$con)" to "mysql_query($query,$con) or die(mysql_error())" and tell us what error you see? – Wayne Whitty Aug 28 '12 at 08:49
  • 1
    **Your code is vulnerable to SQL injection.** You *really* should be using [prepared statements](http://stackoverflow.com/a/60496/623041), into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of [Bobby Tables](http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain). – eggyal Aug 28 '12 at 08:49
  • Also, as stated in [the introduction](http://www.php.net/manual/en/intro.mysql.php) to the PHP manual chapter on the `mysql_*` functions: *This extension is not recommended for writing new code. Instead, either the [mysqli](http://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](http://www.php.net/manual/en/ref.pdo-mysql.php) extension should be used. See also the [MySQL API Overview](http://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API.* – eggyal Aug 28 '12 at 08:50
  • there is no any error in code i thins if it error then how it is inserting one record – user1619187 Aug 28 '12 at 08:50
  • What value does response_id have in your first row? – Wayne Whitty Aug 28 '12 at 08:51
  • it inserts only one record starting from 0 primary key – user1619187 Aug 28 '12 at 08:52
  • @user1619187 do u added the auto increment to the primary key if any??? – swapnesh Aug 28 '12 at 08:53
  • yes i have added autoincremetn to primary key – user1619187 Aug 28 '12 at 08:56
  • response id is primary key and has value 0 – user1619187 Aug 28 '12 at 08:56

2 Answers2

0

Try like this

$query=("INSERT INTO survey_question_responses (participant_id,question_id,answer_text,answer_option)

      VALUES ('$participant_id','$question_id','$answer_text','$answer_option')");
Justin John
  • 9,223
  • 14
  • 70
  • 129
0

As you made the id field auto incremented don't insert it vie your INSERT query .

Write the query as

INSERT INTO survey_question_responses (participant_id, question_id, answer_text, answer_option)
VALUES ('$participant_id', '$question_id', '$answer_text', '$answer_option')

You should also explain or send the structure of your table.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mansoor Jafar
  • 1,458
  • 3
  • 15
  • 31