douqingzhi0980 2013-11-21 09:38
浏览 19
已采纳

PHP:将动态数量的值添加到MySQL数据库中

I've got a page with text fields where the user can input data (in our situation they are questions).

It would look look something like this:

 ___________________________________
|   Enter your question...          |  +
|___________________________________|  

If the user clicks '+' then another field comes up:

 ___________________________________
|   User's first Question..         | 
|___________________________________|  
 ___________________________________
|   Enter your next question...     |  +
|___________________________________|  

...And so on. Each of these questions need to be stored in the database, but with the number of questions being dynamic (let's say for arguments sake we limit the max number to 100 questions), what's the best way to achieve this? Should I be creating a new column for each question? Wouldn't that really pollute the database? Ideally I'd have just one column in my database called 'Questions' and just store all the questions in that one column. Is that even possible?

Something like this:

++++++++++++++++++++++++++++++++++++++++++++++
+ id |  Questions                            +
++++++++++++++++++++++++++++++++++++++++++++++
+  1 |  My Question 1,MyQuestion,            +
+  2 |  Another Question, And Another one,   +
+  . |  ......                               +
++++++++++++++++++++++++++++++++++++++++++++++

*EDIT* To make it clear - I need to be able to retrieve each of those questions later on and be able to isolate each one individually (e.g. as a PHP array).

*EDIT2* Additionally because the set of questions are per each user, the bundle of questions need to remain in ONE row. I.e. I can't add the questions in their own rows. In the example above, ID1 is an individual user and My Question 1 and MyQuestion are that user's questions.

  • 写回答

3条回答 默认 最新

  • dosin84644 2013-11-21 09:48
    关注

    An option you can try as well would be to split it up by creating another table. So to be clear one table for user and another one for question.

    In table user you can have a user_id and maybe username and in table question you can have a unique_id, the question and then a foreign key constraint referencing the user of the question.

    This way you can access them individually or all together.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度