PHP mysql table rows [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I want to make like following:
I have a page, where users can register..
And another page, with their profile..
mysql table look's like following:
USER
-----------------------------------------------
| id | name | age | about | registered | email|
-----------------------------------------------
INFO
--------------------------------------
| id | title | content | date | hits |
--------------------------------------
Informations about user are stored in USER database..
Now user want to add some "INFO" in their accound everyday..
when user add their info, there will be another page wich will show:
NAME REGISTERED with EMAIL (from USER database) added following
TITLE (from INFO database)
CONTENT (from INFO database)
DATE (from INFO database)
HITS (from INFO database)
I really dont know how to do that ..
Pls understand me.. Im newbie on PHP
Cheers!

Try this
SELECT u.email,i.title,i.content,i.date,i.hits FROM user u,info i where u.id = i.id;

Try the mySQL JOIN command that will bring data from two or more data tables together via a specific collaboration.

I can't see where you link the USER table to the INFO table.
My suggestion is that you must create another table to be able to connect the two tables. Something like:
---------------------
| USER_ID | INFO_ID |
---------------------
| 1 | 1 |
---------------------
| 1 | 2 |
---------------------
That way, you could make a JOIN between the two tables.

Related

Database for Poll + Explanation which can be updated

I've got the following poll form for my users but I am not sure how I should structure the DB around it.
The user will get 20-30 poll questions like the following:
What is your favorite color?
Blue
Green
Yellow
Red
Other
Will be able to choose one of the above answers and must also provide around 100 words explaining why he chose that answer.
I've currently got two tables. One that holds the poll questions and one that holds the poll options. What I am not sure about is how should I hold the user answers.
The thing is because the poll is so big, the user can do it partially, come back at a later time, alter his answers and keep going until he is 100% done which is then that I'll be able to view the whole result in my panel. So he can basically save his progress and alter it at anytime. On top of that I would like to "remove" the whole poll for a specific user and be able to redo it all over again but at the same time keep a history of his previous answers.
So I am not sure if a table like this would be the best option for my needs:
id
user_id
poll_questiond
poll_answer
poll_text
last_update
status
Seems like something like this will create a huge mess. Is there a better way to do this?
I would create 3 tables:
table_polls
+----+---------------------+--------+
| id | description | status |
+----+---------------------+--------+
| 1 | Example description | 1 |
+----+---------------------+--------+
table_poll_options
+----+---------+-------------+
| id | poll_id | description |
+----+---------+-------------+
| 1 | 1 | Question 1 |
| 2 | 1 | Question 2 |
+----+---------+-------------+
table_poll_answers
+----+-----------+---------+----------------+---------------------+
| id | option_id | user_id | description | created_at |
+----+-----------+---------+----------------+---------------------+
| 1 | 1 | 1 | A valid reason | 1970-01-01 00:00:00 |
| 1 | 2 | 2 | Another reason | 1970-01-01 00:00:00 |
+----+-----------+---------+----------------+---------------------+
To recapitulate the above:
A poll has many questions.
A poll question has many answers
A poll answer has one user.
This way you have everything split up with pivot tables and you no longer need to create messy rows in your poll table.
You can expand on the tables of course, if you need extra information for dates etc.
I think you will find it easier if you define your problem domain in semi-structured language. You may decide to delegate some of the logic to the application layer, rather than embedding in the database schema - for instance, saving partially-completed polls might be easier within the application layer.
You might start with something like this, capturing the major entities in your domain, and their relationships, but not their attributes.
The system consists of many polls.
One poll has many questions. A question belongs to 1
poll.
A question can be of type multiple choice (select one) or
multiple choice (select n) or free text
A question may be mandatory or optional
A multiple choice question has 1..n answer options
A question has a sequential relationship to other questions
(e.g. the free text question must follow the favourite colour
question)
The system consists of many users
A user answers many polls
When the user answers a poll, they answer 0..n questions
When the user answers a poll, the answer is only valid if the
user completes all mandatory questions
This suggests that you have two polymorphic entities - question (which can be free text or multiple choice), and answer (as the answer is related to the question, it is also free text or multiple choice).
You have to decide how to model this in your schema - Stack Overflow has many questions on this topic - but I'll pick the simplest.
Poll
-----
Poll_id
Question
------
Question_id
Poll_id
Sequence
Is_mandatory
Description
Question_option
-------------
Question_option_id
Question_id
Option_id
Sequence
Description
User
-----
User_id
Poll_session
------
Poll_session_id
User_id
Poll_id
Date
Status
Poll_session_answer
-----
Poll_session_id
Quesion_id
Free_text_answer
Question_option_id_answer

MySQL query to get one of each Item [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working with php and MySQL, I am making a messaging app, the app has communicates with a remote database, in my database is my messaging table, the table has 9 tables of which includes the "Subject table", I need help, I want to make a query that retrieves only one subject of each type, lets say I have 10 messages with the subject "Man", 12 of the "Dog", I want it to get only one man and one dog below is a graphic representation of my Messaging Table.
| message_id | subject | username |
|:-----------|------------:|:------------:|
| 1 | cyber | Chrome |
| 2 | Hyper | Ciare |
| 4 | Cyber | Gorger |
You can use SQL's DISTINCT:
SELECT DISTINCT subject FROM table
This will fetch each subject only one time, even if it appers more than once.

how to copy table between two database using php+mysql? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
how to copy table between two database using php+mysql?
sometimes I want to use php copy table derectly between two tables, it seems too hard for me. is there anyone know how to do it ?pls see my code bellow:
<?php
// database: a , table: user
name | age | sex
jimmy | 30 | male
ricky | 20 | male
tina | 20 | female
// database: b , table: user
name | age
kelvin | 30
// I want to copy a.user to replace b.user completely, how to do it in php+mysql?
$db_a = $db->connect($a_config);
$db_b = $db->connect($b_config);
// $db_a->table('user')->copy_to($db_b); // only explaining what I want to do
// final result: b.user is
name | age | sex
jimmy | 30 | male
ricky | 20 | male
tina | 20 | female
create table db_a.table_name like db_b.table_name;
After creating table you have to insert data like this
insert into db_b.table_name select * from db_a.table_name;

How do I make a php page that reads different selections for different users? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a MySQL that lists about 500 items. Users can register at the site and mark which item they have already. This is a simple php that reads the table and each row.
How should I structure the database so that each user can have a particular setting for each item?
So that when Bob opens his account, he sees he already has ITEM 1, but when Mary opens it, she'll have ITEM 1 as missing and she can see who has that ITEM 1 twice or more times so that she may contact whoever has more than 1 of an item?
You could do something like this:
Have a table that shows which users have what items...
Table Name: user_items
primary_key | user_id | item_number
-----------------------------------
1 | 1 | 1
2 | 1 | 1
3 | 1 | 2
4 | 2 | 2
or you could have something like this
primary_key | username | item_number
-----------------------------------
1 | bob | 1
2 | bob | 1
3 | bob | 2
4 | mary | 2
This table above shows that:
User 1 / Bob has:
item 1 x2
item 2
User 2 / Mary has:
item 2
You can create other table with three columns, foreign key for item, foreign key for user and amount of that item.
You will keep there information that Bob has ITEM 1.

Tagging text using tags stored in database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
in my new PHP project, I'm going to get a large amount of text as input.
And I have hundreds of tags stored in my mySQL database:
| id | tag |
+-------+------------------+
| 1 | Halo |
| 2 | LIMBO |
| 3 | Super Mario |
| 3 | Metal Gear Solid | <-- multiple words as single tag
The inputs are also stored in mySQL, before being processed.
How can I check each input for these tags? using explode(' ' ? strpos() ? LIKE '%TAG1%' OR LIKE '%TAG2%' ? ... how can I gain maximum performance with PHP and mySQL?
Thanks
P.S.
How about an input in HTML format?
For maximum coincidence set FULLTEXT-index to field tag. Then in query use next design :
SELECT * FROM `tags`
WHERE MATCH(tag) AGAINST (YOUR_INPUT_VALUE)
Read Full-Text Search Functions

Categories