Two FK from the same table with the same meaning - mysql

I'm trying to make a chat with dialogue fuction as my learning project. Any user should be able to write messages to another user and recieve answers from that user, and no one else should have access to that dialogue.
In MySQL, I've created table user with id, login, password and privileges, table message with id, author (FK to user id), message text, time of post, and dialogue id (FK), and also table dialogue with id, FK to one user, FK to another user and additional columns with some parameters. I want to make my chat the way it let me have all messages from two given users to each other with just one query, but in the realization above I have to check two columns to find one user first, and then check all entries with that user to find his interlocutor. Only this query will return me a dialogue PK which could be used to get all messages. I think this realization is totally wrong, but I can't find the way to fix it. I've tried to attach dialogue id to table user, but this made my base even more complicated.
What should I do?

Here is my first schema: http://puu.sh/qdLMw/f9acde605c.png But I've make another table which will containt both interlocutor and dialogue id. When some user write a message to another, a query request line from this table where current user set as sender and his interlocutor set as target, and get a dialogue id from this line. If there is no such line, system will run another query which switch sender and target in their places. And if result is empty again, new dialogue will be created with two lines in interlocutors table linking two users to each other. http://puu.sh/qdORJ/2122558f64.png
It looks just slightly better for me, and if someone have an even better answer, please tell me.

Related

How can I populate the third table in a many-to-many relationship in an Access form? (MySQL backend)

Firstly, here is the relationship between the relevant tables.
I'm developing an Access frontend to serve as a CRUD for an underlying MySQL database, and I'm most of the way there for properly representing and interacting with the tables in a many-to-many relationship. I followed the instructions in this tutorial and my form is mostly functional. But the tutorial example is limited in that the third table in the relationship (in my case, Addresses) is static, and cannot be changed from the form.
Here's how my form looks currently. When adding a new entry in the people_has_addresses subform, if I wish to add an address which already exists in the addresses table by entering its ID into the addresses_addressID column, everything works. The rest of the columns automatically load in the address, and people_has_addresses automatically populates a new row with this unique combination of PersonID and AddressID. The problem comes when I want to create a new address on the fly, add it to the Addresses table, and populate people_has_addresses with this new combination. If I free-form type a new address into the subform and hit enter, I get the following error: "The Microsoft Access database engine cannot find a record in the table 'addresses' with key matching fields 'addresses_AddressID'."
My question is, what do I need to do in order to allow the creation of new rows in Addresses? Ideally working the way I describe above. I feel that I'm close to getting this right, but don't know what to do from here.
Ok, so the combo box in that row lets you select a given address. You not really described that if you have 10 address,which one you want to appear say in a mailing list or what not. What you could do is add an extra column to the table where you shove in (save) the address id, and add a "default" check box. Then you can have a query with the default Address column = true to only pull the one address for mailing etc. So in this example, I do have a check box to select which out of possible many address are to be used by default. And we could add another column for home, business, holiday location or whatever. That way you can categorize the address to what it is.
However, the issue and your question is of course how to add a new address?
There is as many ways to do this as their are flavors of ice cream. In other words, it up to your creative mind. One way would be to use the combo box not in list event. So if you typed in a address id that don't exist, you could be prompted to enter it. However, users should never have to see, know, or use some silly "id" during data entry. Users in fact should never even see PK or FK id's during use of the application.
I would suggest you next to the combo box that lets you select a address is place a button to add a new address. You could use a graphic image like a + sign. So, they can use the combo box to select an existing address, or click on on the + button to add a new address. What the code behind the button would do is launch a form to edit the address, but launch the form in add mode. And when you close/save the form, then the code could/would stuff in the PK id of the new address you just added into the FK Address_ID column for the address.
And in the interest of cruelty to animals and your users? They should not need to know or see or type in some "number". The combo box can hide the first PK column. So when you select a given address (in this example hotel, but it could be just a address) you display the name. So, users in place of expanding the combo box can just start typing in the hotel name. To be fair, with address, you really don't have a "name" to type in, so your case is somewhat different. In most cases the thing you are selecting has some name, or part name/number, or in our case a Hotel Name.
Note that the sub form is based on a query that joins in the other columns. So, when they crack open the combo box, they are in fact selecting a PK. So like magic (and without code), then the city,province, description columns automatic display from that other table.
So the form would look like this:
In above, we selected several hotels. However, note the check box as to which address to use, since we might have several, but for printing a letter or envelope, then the check box will determine which address will appear.
And in the above, if we don't find or have a Hotel (or address) we want to use, then we can hit the + sign beside the combo box. That would launch up a nice single form layout to enter a new Hotel (or address). As noted, how you setup your UI to work is quite much a open canvas. One great way to get ideas is to look at other software you use. So, how do you add multiple address to outlook users? So, a gazilion ways and ideas exist as to how you want to do this. It not really how to do this, but HOW YOU want this to work, since you can design this to your hearts content to work quite much anyway you want.
So, the PK id should not be exposed here. Users can't care or know about such things. I mean, as I am typing this response, no doubt the SQL server that drives stack overflow generated a new number for this post - but it not a user thing, only a developer thing to care about.
Ok, so we build that sub form. The table driving that sub form is only
ID (PK)
Customer_ID (FK to parent form driving this)
Hotel_ID (FK to hotel - or in your case address)
DefaultAddress (true/false column)
And as noted, we should add ONE MORE column that sets the address as the default address to use.
eg: default address.
. All of the other columns display as a result of a left join. (a left join for this sub form is VERY important).
Now, if a hotel is not in our list of choices, then the user can simply hit the + sign beside the combo box, and we pop up a nice form to edit/add a new hotel. And take well note that when you use outlook, an accounting system, or just about any software? You not seeing and typing in PK id values - that only really for you the developer to see, and end users should never see or have to deal with actual PK numbers.

How to Manage Guest Users with Php and SQL

I am using PHP , MySQL for Handling My site. I have a user table called user_table containing user_id PRIMARY and Now user can chat with other user. For that I am using a table called message_table. this table contains essential fields like msg_id , sender_id , reciver_id , replay_id etc. this works very fine.But I need to handle also guest users[Not registered] with this chat. how can I handle them. I inserted the guset_id as the mixture of entering time and his name to a table called guest_table. Now I can use this guest_table like user_table. Is that a good idea ?
But I need to handle also guest users[Not registered] with this chat.
These guests look to me like a special sort of user. I suggest you handle them as if they were users. You may wish to put a column called something like is_guest in your user table, and set it to 1 when you know the user is a guest.
Keeping a separate table, parallel to the user table, for your guests is NOT a good idea. It will greatly complicate your queries, especially when a person in your user table communicates with a person in your guest table.
Instead, put everybody in the user table, guests and registered users alike.
A nice little benefit: if you convert a guest to a registered user, all you have to do is flip the is_guest flag and let them specify their own username and other data. They get to keep their history.

Issue about phpmyadmin, mysql and dreamweaver

I have an issue that I could not figure out. I need to clarify that I am very new to phpmyadmin, mysql, databases and dreamweaver.
Currently, I have one table (book) which contains these attributes (ISBN(primary key), title, author, publisher, price etc) in phpmyadmin
I have the other table (users) which contains these attributes (username (primary key), password, name, DOB etc) in phpmyadmin too.
Let me explain the series of steps for my webpages first. An user logs in on the log in page successfully, and he will be directed to the main page, then he will type or select his search terms, then the results page will display the relevant results in the master list, then he will select the specific result to go to the detailed page containing more information.
What I want now is to create a reservation page. If the user likes the specific book, he can click reserve it, and will be directed to the reservation page.
I intend to have these search textfields: Username, Title, Price, and Number of books.
How can I let the username textfield to be automatically filled with the username as the user has logged in at the login page initially.? Also, how can I let the title and price textfield to be automatically filled with the relevant title and price, as the user has clicked reserve it at the specific detailed page? I intend that the user just key in number of orders and press submit button. My 3rd database (reservation) will capture the info of username, title, price, and number of orders. Do i use insert record or update record?
Do i create a relational table in phpmyadmin by linking it with the keys from tables (book and users)? Will this be a many to many relationship?
You can use sessionId to display username in the textbox...
use javascript to automatic fill up the title and price....
for the 3rd databse u should use insert record and create 3rd table too...

Mysql database table design

Users on my site are shown a list of items. The list they see is based on criteria they have completed in a form - they're receiving suggestions if you like.
Now, if a user doesn't want to receive a certain suggestion again, i want them to be able to stop it from showing up.
So, do i create an exceptions table which simply has a user id number and suggestion id number per row and search that before giving suggestions..meaning if the user has previously said no then skip to next suggestion.
Or, do i have a table with a row per user which has their user id and a field of all their exceptions, maybe comma separated or enclosed in[] and do a "where in" search to see if an exception is there - if the latter though and the user decides they do want a particular suggestion at a later date, its easier to get it out of a row per suggestion table as opposed to a field where they are, in fact i wouldn't know if thats even possible.
All advice welcome.
It sounds like a many-to-many relationship between users and preferences/exceptions. I'd rather do that than the IN idea.
I'd have a table of "available suggestions" with a suggestion reference and a user reference. so for each user there would be one record for each suggestion available to them. When a user user clicks no, then that record can be deleted from the available suggestions.
Hope this makes some kind of sense.

Database design question with message boxes

I have a question and I'm not sure about the best way to design it.
Basically, there are users. Each user can have a message box, and each box in turn can have several messages.
The problem is, is that there are some special message boxes, including "New," and "Trash." A user is required to have these two boxes.
I had some ideas about what could be done. One of my ideas was for each message box to have a field recording its type (enumeration of "new," "trash," or "other). But that doesn't force them to have those boxes, which I would prefer.
I could also have each user have fields specifically relating to their special boxes (so in the user table there would be fields like newBox and trashBox). Of course, if these are required, then the user and user_messageBox tables would be mutually related, which would cause obvious problems.
Then, I could make it many-to-many where the message box table doesn't relate to users, so there would be another table for relating these two as a many-to-many. But I don't want a many-to-many relationship, so it doesn't solve anything!
If you want just one message box per user, then call "new" and "trash" something else, like a state. Have this be a field of the message. (You'd also need a field for the user or equivalently the message box in each message as well.)
This gets you thinking outside of the box (!) and may give you other ideas as to how to use that field if you so choose: "Flagged," "important," "spam," etc.
Why dont u use a message_box_type field,.
I think you should force the users to have both the message box types using application logic.
Ideally there would be two tables, a messages table with a foreign key id message_box_id that links it to another table message_box. The message_box table in turn has a message_box_type field in addition to other fields. The application logic would make sure that two message_box records of the two types 'new' and 'trash' are created on user signup/creation