How do I automatically update a foreign key [closed] - php

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 2 years ago.
Improve this question
I am a novice at php and mysql. Let's say my simplified fan fic site's mysql database has two tables one named authors and another named articles. Table articles has a column called authorID which is a foreign key which relates to column authorID which is a primary key in table authors. I have two forms on a html page each with php scripts that process them. one adds data to table authors (essentially a user registration form for authors that add data such as name, email and password to table authors to columns of the same names). The other form accepts an author email and author password and logs them in by comparing them to the values in table authors and if they match it creates session variables, one with name and another with authorID which if not set the user is not logged in. Once the user id logged in there's a third form which updates data to table articles (title, subtiltle, article to columns of the same names). When the user clicks submit in this form, the php script returns a success message as if my mysql query executed but nothing is added to table articles probably because my mysql query only adds values to columns title, subtitle and article leaving column authorID unupdated. So my question is how should I go about ensuring the system recognizes the logged in user by their authorID (primary key column from table authors) and update it to authorID(foreign key in table articles) whenever a user posts an article?

You have to send the author id with the form data when you are adding an article. You can include it as a hidden field in your HTML page with the value as your author id.
For example,
<input type="hidden" name="author_id" value="5">
When creating the SQL query, include the author_id field in the insert statement similar to title, subtitle, article etc.
If you have set author_id in the articles table as a foreign key, and an author with the sent id does not exist SQL will generate an Integrity Constraint Violation.
Since you haven't provided any code, I cannot answer precisely.

Related

Is it possible to insert a column in a database table automatically using a variable in PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to insert a column in a database table automatically using a variable.
I have a table named tbl_category, I want to let the user choose the sub category field by their own choice. So, from the dashboard, the user will insert 3/4 (or how many fields they would like to create) sub category fields.
I will take this value as $insert_sub_cat_count. So, when this info will hit the function named function save_category_info($data), the function will receive the value $insert_sub_cat_count as $data.
After that, this info have to implement in the table tbl_category and add 3/4 fields automatically according to the value $data.
If the user inputs 2, this will insert two columns automatically:
If the user inputs 3, this will insert 3 columns:
Is this possible? I don't know how to extend columns automatically or if there's any other way to do so.
This is a bad idea from the very start. As Raymond Nijland said above ("the same column names with increment are a SQL anti pattern.. you should check table normalization instead"), you shouldn't allow users to create columns with their desired name in your database. You should have the following tables:
user - id, name
categories - id, id_user, name
subcategories - id, id_category, id_user, name
So you will be able to link the category and subcategory to the user that created it. You don't need to create a separate column for each user.
If you're worried about speed you should add index for the subcategories table, for the column: id_user. In this way the search will work fast enough.
Insert is used to insert variables into an already existing column, you need to first create your column before filling it:
ALTER TABLE `tbl_category` ADD `sub_category_one` VARCHAR(100) NOT NULL;

Foreign keys in database for php blog

Right now I'm working on a small blog project and I have 2 tables in my database: users and posts. I want to display name of the author of the post so I thought I should make foreign key for user in post table. But what if I would create normal column called for example user_id and just save there id of the author. Then while I would like to display post i could join both tables and display content of post and name of the author.
Does creating foreign key have some adventages?
Either way, you are going to need a "normal" column, such as user_id.
The advantage of making the user_id a foreign key is that then, the database will enforce referential integrity. This means it won't allow you to set on a post a user that does not exist, nor will it allow you to delete a user who has one or more posts (without also deleting the relevant posts).

how to fetch data from database in php if i have two table in database and i want to fetch from one table user id to others table data [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 7 years ago.
Improve this question
I have created two table name is 'orders' and 'registration' in one database. when I am registering that is working fine and when i am trying to logged in with current registration that is also working fine. but
problem is only that suppose 5 users registered there and they submitted different orders. after loggin while going to see history of orders all users are seeing the same history and all are able to see each others orders in his account after logged in . I am only trying to stop this. so that only logged users can see their own submission only. they should be able to see others user order
As database schema is not available, I'm assuming the structure like this.
Change it where you need.
registration table
Id | firstName | lastName | email | ....
Where, Id is Primary Key and auto-incremented
orders table
orderId | userID | orderName | ...
Where, orderId is Primary Key and auto-incremented ;
userID is Id of registration table.
So, when you are logged in through registration table. Create one session for Id.
$_Session['user_id']; (Id of registration table will be stored here.)
For viewing his/her orders
<?
$userID=$_SESSION['user_id'];
$orderQuery="SELECT * FROM orders WHERE userID=$userID";
.
.//Execute Your query
.
?>
Like David said, you could just use something like:
Select * from order
where userid = logedinuserid
but you should also consider to rename the table order.
I just asume you have the UserID in the Orders Table...
So in orders table you need to add column
user id related with foreign key to table users and select orders only for logged/choosed user by id.
For exaple when user is logged you need to get his unique id.
And when he wants to see orders query onle results for this particular user.
For ex.
set #loggeduser = session_id() from php.
Select * from oreders where userid=#loggeduser;
I can't comment, yet.

using comment ID to associate images with comment

I'm designing a site where comments can be added in reply to an initial post and each comment can have attached images (and likely will given the type of content). I'm wondering how I will grab the proper comment ID to insert it into a MySQL DB column in the image table.
So far the db has a table for the initial post with serial as the primary key, the db also has an image table with id as the primary and columns for various attributes of the image as well as a column for the serial of the post images belong to (serial comes from the serial of the item the post is about), I plan to also add a column to that table for the comment ID which will be filled with the ID of the comment they belong to if they don't just belong to the initial post. I'd like to add comments in their own table with ID, info, date and title.
What I'm unsure of is when inserting a comment with attached image, how do I grab the comment ID to insert into the image table comment ID column? Or is there a better way to approach the issue?
Thanks in advance for any advice.

PHP structure of a comment database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
At the moment I create a blog system with php. The admin can create x posts, and under every post should be a comment function. How can I realize something like that? I dont know the structure of the databse, because it must be many tables (for each post(?)). Maybe someone of you have a good link. I searched already but have found nothing for my problem.
You don't need many tables. You could have a table for the posts like this:
id INT(10)
title TEXT
content TEXT
And a table for the comments, like this:
id INT(10)
postId INT(10) // this links to the column post.id
content TEXT
Of course, you can add more columns to both tables if necessary, for example to add timestamps, etc.
Now, when you have a post with id 7, you make sure that all the comments to that post get postId 7. That way, when you display post 7 on your website, you can search for all the rows in the comment table with postId=7, and display exactly those comments that are linked to that post.
I've simplified this example. To optimise the tables you could add indices, primary keys, etc.
#Skeptar, there quite a number of ways you can achieve this. Let me suggest one to you.
Users table: (very obvious)
Columns: user_id (INTEGER(n) PRIMARY KEY), username (VARCHAR(n)), password (VARCHAR(32) for md5), level (INTEGER Example: 1 for admin, 3 for regular user)
Posts:
Columns: post_id (INTEGER(n) PRIMARY KEY), post_title (VARCHAR(n)), post (TEXT), user_id (INTEGER(n))
Comments:
Columns: comment_id (INTEGER(n)), comment(TEXT), post_id,(INTEGER FOREIGN KEY REFERENCES post(post_ID)) user_id(INTEGER(n) FOREIGN KEY REFERENCES users(user_id))
Now, I believe you have a means of allowing both levels (admin and regular) of users to register into your system.
When inserting, you take the post title and the actual post and with the help of some user identification mechanism like SESSIONS, you get the user_id and insert them into to post table.
Then for each comment, when inserting, you fetch the post_id of the post on which the user is commenting and insert that into the comment table along with the comment and the user_id.
When you want to fetch all posts, that's fairly easy.
When you want to fetch all comments for a particular post, you take the post_id of that post to the comments table as such:
SELECT * FROM comments C, posts P WHERE C.post_id = C.post_id;
Since you inserted the user_id against each post and comment, similarly, you can fetch the corresponding user information for each comment as such:
SELECT U.username FROM comments C, users U Where C.user_id = U.user_id
Remember, this is only to give you and idea and not to be taken as a complete working solution. I wrote this off head and could have some errors.
Also, where I indicated in bracket n (INTEGER(n)), it means you need to replace n with the desired columns size. Hope it helps.

Categories