MySQL/PHP output is not as expected - php

I have two tables in my database by name language and language_variables.
In language table, I have stored languages and in language_variables have value of specific word in all languages.
DB Structure & data of language
id, title, short_code, type enum('ltr', 'rtl'), default enum('yes', 'no'), status enum('on', 'off')
1, English, en, ltr, yes, on
2, Urdu, ur, rtl, no, on
DB Structure & data of language_variables
id, language_id, keyword, value
1, 1, str_word, this is simple string
2, 2, str_word, اس سادہ سلک ہے
3, 1, str_word2, this is 2nd string
Output I want
I want to show keyword str_word for once and show drop down list of languages list for same keyword.
Issue in my output
My SQL query is
SELECT
lv.id, lv.language_id, lv.keyword, lv.`value`, l.title
FROM language_variable AS lv
INNER JOIN `language` AS l ON l.id = lv.language_id
GROUP BY lv.keyword
ORDER BY lv.id DESC
My view
<?php
foreach($records as $record)
{
?>
<div class="banner-row1">
<div class="banner-row-text">
<h2><?=$record->keyword?></h2>
<p class="text-muted">
<small>
Value: <?=$record->value?><br />
Languages:
<select id="language_id" required>
<option></option>
<?php
foreach($languages as $language)
{
echo '<option value="'.$record->id.'">'.$language->title.'</option>';
}
?>
</select>
</small>
</p>
</div>
<div class="clr"></div>
</div>
<?php
}
?>

You should join with language_id, not with id in language_variables table.
INNER JOIN `language` AS l ON lv.id = l.id
Use this
INNER JOIN `language` AS l ON l.id = lv.language_id
^ ^

You need to do at least 2 queries (or change your whole code).
1) get all the "str_word" : query1 = select distinct keyword from language_variable
2) get all the values : query2 = select * from language_variable
3) do a foreach loop with query1, and inside it, display all the corresponding values str_word.
That solution requires to have the query2 as an associative array indexed on str_word.
The alternative, easier to code but requiring more resources is to do the query1 then, in the loop, do the corresponding query select * from language_variable where str_word=:that_keyword (that means as many sql query as there is keywords)

Related

Auto Hide Choices that are Full and Insert all the data to other Table

I have the following database consisted from the following fields.
**table_choice**
field_choiceid(auto increment) (1, 2, 3, 4, 5)
field_choicename (C1, C2, C3, C4, C5)
field_choicemaximumslot (10, 12, 15, 18, 20)
**table_select**
field_selectedid(auto increment)
field_selectedchoice
field_selectedby
**table_full_choice**
field_fullid(auto increment)
field_choiceid_full
field_choicename_full
field_choicemaximumslot_full
**table_full_select**
field_selectedid_full(auto increment)
field_selectedchoice_full
field_selectedby_full
And I'm using this code to auto-populate my dropdown list.
<label>CHOICE</label>
<select value="" name="choice" id="choice" required>
<option>SELECT</option>
<?php
$result=mysqli_query($connection,"SELECT * FROM table_choice")or die(mysqli_error,());
while($row=mysqli_fetch_array($result)){
$choiceid=$row['field_choiceid'];
$choicename=$row['field_choicename'];
$choiceslot=$row['field_choiceslot'];
?>
<option value="<?php echo $choicename;?>">
<?php echo $choicename;?>
</option>
<?php } ?>
</select>
If C1, C3, C5 reached their maximum slot.
So I would like to hide C1, C3, C5 in the dropdown list that is auto-populated and insert the data of C1, C3, C5 from table_choice to table_full_choice and insert the data of C1, C3, C5 from table_select to table_full_select then delete the data of C1, C2, C3 in the table_choice and table_select. How to do that? Please help. I'm a noob. I'm a newbie. The this will be the data in the table
table_choice
field_choiceid(auto increment) (2, 4)
field_choicename (C2, C4)
field_choicemaximumslot (12, 18)
table_select
field_selectedid(auto increment)
field_selectedchoice
field_selectedby
table_full_choice
field_fullid(auto increment) (1, 2, 3)
field_choiceid_full (1, 3, 5)
field_choicename_full (C1, C3, C5)
field_choicemaximumslot_full (10, 15, 20)
table_full_select
field_selectedid_full (1 to 45)
field_selectedchoice_full (C1(10 times), C3(15 times), C5(20 times))
field_selectedby_full (Person1 to Person45)
You can use SQL JOIN to select.
If you haven't done it you should add a foreign Key for the fields table_choice.field_choiceid and table_select.field_selectedchoice
I hope I don't have a mistake but this Code should work. But try the select at first.
<form>
<select name = "CourseList">
<?php
$result=mysqli_query($connection,"SELECT tc.*, count(ts.field_selectedchoice) AS c_selected FROM table_choice tc LEFT JOIN table_select ts ON ts.field_selectedchoice = tc.field.choiceid GROUP BY tc.field_choiceid")or die(mysqli_error,());
while($row=mysqli_fetch_array($result)){
$choiceid=$row['field_choiceid'];
$choicename=$row['field_choicename'];
$choiceslot=$row['field_choiceslot'];
$maxchoice=$row['field_choicemaximumslot'];
$selectcount=$row['c_selected'];
if ($maxchoice > $selectcount){
echo '<option value='.$choiceid.'>'.$choicename.'</option>';
}
?>
</select>
If there is a mistake in the SQL-Statment, I'll try and correct it.
If you can add how many people have signed up and the maximum of people
who can sign up to the database, you can do it this way.
<form>
<select name = "CourseList">
<?php
$result = mysqli_query($connection,"SELECT * FROM table_choice")or die(mysqli_error,());
while($row = mysqli_fetch_array($result)){
$choiceid = $row['field_choiceid'];
$choicename = $row['field_choicename'];
$choiceslot = $row['field_choiceslot'];
$HowMany = $row['How_Many'];
$Maximum = $row['Maximum'];
if(HowMany != Maximum){
echo "<option value = "$choiceid" selected>$choicename</option>\n";
}
?>
</select>
</form>
BadSly, I am humbly welcoming you to stackoverflow!
According to your problem as #Spirit Says you can LEFT JOIN them, after that you can filter it out using the HAVING reserved word. With that I filter out the lessons that are already full (as far as I understood in your question) after having joined it all together.
For more info regarding having reference at this piece of documentation. Also more information focusing on the differences between HAVING and WHERE is located in this piece of information: https://stackoverflow.com/a/2905312/4706711
I build a sample of your database in sqlfiddle http://sqlfiddle.com/#!9/df211f/4/0 for completeness reasons the sample database is build like that:
CREATE TABLE table_choice(
field_choiceid INT UNSIGNED NOT NULL PRIMARY KEY,
field_choicename VARCHAR(2) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
field_choicemaximumslot INT UNSIGNED NULL
);
CREATE TABLE table_select(
field_selectedid INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
field_selectedchoice INT UNSIGNED NOT NULL,
FOREIGN KEY fk_select(field_selectedchoice) REFERENCES table_choice(field_choiceid) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO table_choice VALUES (1,'C1',10),(2,'C2',12),(3,'C3',15),(4,'C4',18),(5,'C5',20);
insert into table_select(field_selectedchoice) VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(4),(5),(3),(2),(2),(2),(5),(4);
As you can see the query:
SELECT table_choice.*, count(table_select.field_selectedchoice)
AS c_selected FROM table_choice LEFT JOIN table_select
ON table_select.field_selectedchoice = table_choice.field_choiceid
GROUP BY table_choice.field_choiceid HAVING c_selected < table_choice.field_choicemaximumslot;
Will filter out the unwanted options. Thus you can use your code with a twist:
<label>CHOICE</label>
<select value="" name="choice" id="choice" required>
<option>SELECT</option>
<?php
// Is good idea to fetch the required fields and no more.
$query="SELECT table_choice.field_choicename as field_choicename, count(table_select.field_selectedchoice) AS c_selected FROM table_choice LEFT JOIN table_select ON table_select.field_selectedchoice = table_choice.field_choiceid GROUP BY table_choice.field_choiceid HAVING c_selected < table_choice.field_choicemaximumslot";
$result=mysqli_query($connection, $query)or die(mysqli_error,());
while($row=mysqli_fetch_array($result)){
$choicename=$row['field_choicename'];
?>
<option value="<?php echo $choicename;?>">
<?php echo $choicename;?>
</option>
<?php } ?>
Furthermore to keep things neat in your code you can create a view based on the query you want. As an example in your case you can create the vie like that:
CREATE VIEW view_table_choice AS SELECT table_choice.*, count(table_select.field_selectedchoice)
AS c_selected FROM table_choice LEFT JOIN table_select
ON table_select.field_selectedchoice = table_choice.field_choiceid
GROUP BY table_choice.field_choiceid;
You can reference about views in this piece of mysql documentation.
So using the view the code will be:
<label>CHOICE</label>
<select value="" name="choice" id="choice" required>
<option>SELECT</option>
<?php
$result=mysqli_query($connection,"SELECT * FROM view_table_choice WHERE view_table_choice.c_selected < view_table_choice.field_choicemaximumslot")or die(mysqli_error,());
while($row=mysqli_fetch_array($result)){
$choiceid=$row['field_choiceid'];
$choicename=$row['field_choicename'];
$choiceslot=$row['field_choiceslot'];
?>
<option value="<?php echo $choicename;?>">
<?php echo $choicename;?>
</option>
<?php } ?>
</select>
And you can enjoy on how neater looks like. In order to update your current database you can create a php migration script to do that where will run the CREATE VIEW query.

PHP/SQL: How to concatenate/combine columns value into one row

I have this php script called title, where it is supposed to list movie details of those movies with the title matching the inputed substring. The expected output is supposed to be like in the link/picture below. I have trouble with concatenating the genres of each movies since one movie can have many genres. I have tried using the concat(), array_to_string() but still fails.
mkSQL() constructs "safe" SQL query strings by taking a query template
string and filling in printf-like slots in the template with values
supplied in subsequent arguments. The function takes a variable number
of arguments; the first is always a query template string, with the
following arguments corresponding exactly to the slots in the
template. E.g.
$id = 3012345;
$q1 = mkSQL("select * from R where id = %d",$id);
would create the query strings:
$q1: "select * from R where id = 12345"
Below are the codes, any helps and tips will be greatly appreciated, thanks!
This is the Genre Table Schema
CREATE TABLE Genre (
movie_id integer REFERENCES Movie(id),
genre GenreType,
primary key (movie_id,genre));
#!/usr/bin/php
<?php
// include the common PHP code file
require("a2.php");
$db = pg_connect("dbname=mydb");
// Check arguments
if (count($argv) < 2) exit("$usage\n");
// Get the return results
$val = $argv[1];
$q = "select m.title, m.year, m.content_rating, r.imdb_score, array_to_string(array(select g.genre FROM Genre g where g.movie_id = m.id),',')
-- concat(select g.genre FROM Genre g where g.movie_id = m.id
from Movie m JOIN Rating r ON r.movie_id = m.id
where m.title ilike %p
order by m.year, r.imdb_score desc, m.title asc";
$r = pg_query($db, mkSQL($q, $val));
// Iterate through the results and print
$i = 1;
while ($t = pg_fetch_array($r)) {
echo "$i. $t[0] ($t[1], $t[2], $t[3]) [$t[4]]\n";
$i++;
}
?>
The expected output is supposed to be in this format
Change your query like,
SELECT CONCAT(m.title, ' (', m.year, ', ', m.content_rating, ',', r.imdb_score, ') [', (SELECT array_to_string(array_agg(g.genre), ',') FROM Genre g WHERE g.movie_id = m.id), ']') movie_title
FROM Movie m JOIN Rating r ON r.movie_id = m.id
WHERE m.title ilike %p
ORDER BY m.year, r.imdb_score desc, m.title ASC
Here, I have concat all columns into one and given it an alias movie_title. You will get the movie name as per your specified format.
For achieving this, you can use the group_concat function in your mysql script.
This will concatenate your respective column via comma(,).

group a query's values together based on its id

I have data coming from the database , i am using JOIN(s) to select data from different tables,I need to be able to group the data together based on the specific id of the user in relation to the specific course, as an email needs to be sent based on the selection and within this selection(using a radio button) it will allow admin to be able to email all the users assigned to that specific category selected
i am currently getting duplicated data, like the users details will show on each category instead of being in one single category to pass into the array to email. I only need to select one radio button per category/course but now i am getting multiple radio buttons.
here is my query:
$query="SELECT * FROM course_student
JOIN course ON course.course_id=course_student.course_id
JOIN student ON student.student_id=course_student.student_id
WHERE course.course_id=course_student.course_id
ORDER BY course.course_id";
this is my loop to select data -- it's creating duplicate names for the entries where i just want one name with all of the data that is supposed to be in it
$result=mysqli_query($connection,$query);
confirmation($connection);
while($course_email_students = mysqli_fetch_assoc($result)){
$course_email = $course_email_students['student_email'];
$course_name = $course_email_students['course_name'] ."<br/>";
here is my html:
<input type="radio" name="course_mail[]" value="<?php echo $course_email ;?>">
<?php echo $course_name ?>
<?php } ?>
here is more code
html
<form action ="#" method="POST">
<P> <label for="">Send to specific student courses</label>
</p>
<?php // email specific students
$query = "SELECT student.student_email, course.course_name, course.course_id
FROM course_student
JOIN student ON student.student_id=course_student.student_id
JOIN course ON course.course_id=course_student.course_id
GROUP BY student.student_email, course.course_id
ORDER BY course.course_id";
$result=mysqli_query($connection,$query);
confirm_query($connection);
while($course_email_students=mysqli_fetch_assoc($result)){
$course_student_email=$course_email_students['student_email'];
$course_student_email_name=$course_email_students['course_name'] ."<br/>";
var_dump($course_email_students['student_email']);
?>
<input type="radio" name="course_email[]" value="<?php echo $course_student_email ;?>">
<?php echo $course_student_email_name ?>
<P> <label for="">Message</label>
<p><textarea rows="10" cols="20" name="message"></textarea></p>
</p>
<input type="submit" name="submit" value="send">
here is the php for testing to see what comes through
if(isset($_POST['submit'])){
// do validation
if(isset($_POST['course_email'])){
var_dump($_POST['course_email']);
}
}
From the PHP you've posted, it appears that you are just using data from two, possibly three columns in your query. If so, it is much more efficient to select the specific columns you're interested in in the SQL. There's also a redundancy in the query where you're using the same condition for a WHERE as you have in a JOIN:
$query="SELECT student.student_email, course.course_name, course.course_id FROM course_student
JOIN student ON student.student_id=course_student.student_id
JOIN course ON course.course_id=course_student.course_id <-- !!!
WHERE course.course_id=course_student.course_id <-- !!!
ORDER BY course.course_id";
There are two ways of selecting unique combinations of results with this select query; you can use DISTINCT or GROUP BY.
DISTINCT:
$query = "SELECT DISTINCT student.student_email, course.course_name, course.course_id
FROM course_student
JOIN student ON student.student_id=course_student.student_id
JOIN course ON course.course_id=course_student.course_id
ORDER BY course.course_name";
GROUP BY:
$query = "SELECT student.student_email, course.course_name, course.course_id, student.student_id
FROM course_student
JOIN student ON student.student_id=course_student.student_id
JOIN course ON course.course_id=course_student.course_id
GROUP BY student.student_email, course.course_name
ORDER BY course.course_name";
Now for the PHP code:
$c_name = ''; # course name
$c_id = ''; # course ID
$students = array();
while($aa = mysqli_fetch_assoc($result){
if ($aa['course_name'] !== $c_name) {
# the course name has changed. print out the course and student data
make_radios($c_name, $c_id, $students);
# set c_name to the new course name
$c_name = $aa['course.course_name'];
$c_id = $aa['course.course_id'];
# set the students to the new student
$students = array( $aa['student.student_email'] );
}
else {
# add this student to the list of students
$students[] = $aa['student.student_email'];
}
}
# print out the last set of data
make_radios( $c_name, $c_id, $students );
function make_radios( $course_name, $course_id, $email_arr ) {
$html = '<input type="radio" id="email'
. $course_id . '" name="course_email[]" value="'
. implode(',', $email_arr) . '"> <label for="email'
. $course_id . '">$course_name</label>';
# I don't know if you want to list all the email addresses or not... in case you do:
$html .= "<ul>";
foreach ($email_arr as $e) {
$html .= "<li>$e</li>\n";
}
$html .= "</ul>";
# append the message box
$html .= '<label for="message' . $course_id . '">Message</label>'
. '<textarea id="message' . $course_id . '" rows="10" cols="20" name="message">'
. "</textarea>\n";
echo $html; # or you could return $html
}
I would not recommend that you have the email addresses on the form--I'd use the student IDs and the students' names instead, and then have your script pull the appropriate email addresses from the database--but it's up to you, obviously.
Please read through the code and check you understand what it's doing. I'm happy to answer any questions.
Group and Order are words that have meaning both in English and in SQL jargon. You've said you want your values grouped. I think you mean, in SQL terms, you want them ordered, in such a way that each course's student emails are together.
There are some problems with your query.
First, Pro tip: Avoid using SELECT * in software. (It's OK when you're troubleshooting databases, but it's wasteful and confusing in software, especially when you're JOINing more than one table.)
Second, you have this condition repeated in both your JOIN...ON and your WHERE clause. That's redundant.
course.course_id = course_student.course_id
Leave it in your ON clause and out of your WHERE clause.
Third, your result set's order is underdetermined. This may not matter, but you are ordering only by course id. If you care whether your result set is ordered by student email, then mention it in the ORDER BY clause.
Fourth, you do have three tables. But one of them, course_student, appears to be a classic join table allowing there to be a many-to-many relationship between courses and students. So it seems likely you only have two tables with actual application data items in them.
Finally, from the code in your question it looks like you want
course_id
course_name
student_email
in your result set generated by your SELECT query. Your question's narrative mentions category, but because you've used SELECT * we can't tell what you mean by that.
To get the unique values of those three fields in your result set, use this query:
SELECT DISTINCT
course.course_id,
course.course_name,
student.student_email
FROM course_student
JOIN course ON course.course_id=course_student.course_id
JOIN student ON student.student_id=course_student.student_id
ORDER BY course.course_id, student.student_email
The DISTINCT qualifier eliminates duplicates.
From your comment, it's still hard to tell what you want. But it seems that you want to try this:
SELECT GROUP_CONCAT(DISTINCT course.course_name),
student.student_email
FROM course_student
JOIN course ON course.course_id=course_student.course_id
JOIN student ON student.student_id=course_student.student_id
GROUP BY student.student_email
ORDER BY student.student_email
Or maybe you want this.
SELECT GROUP_CONCAT(DISTINCT student.student_email),
course.course_name
FROM course_student
JOIN course ON course.course_id=course_student.course_id
JOIN student ON student.student_id=course_student.student_id
GROUP BY course.course_name
ORDER BY course.course_name
This second one is going to fail in your large courses because of a limitation of GROUP_CONCAT(). You may need to write application code to organize your web page course by course, or student by student.

PHP MySQL get data from 2 tables

I am trying to combine 2 tables from my database:
files table:
id
file_name
file_description
file_url
access_files table:
id
student_id
file_id
Here is my sql code, currently getting all files from the files table, it doesn`t show the selected files for the user.
<?php
$SQL = "SELECT * FROM files, access_files WHERE student_id ='$studentid'";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
?>
<div class="accordion-group">
<div class="accordion-heading">
<a href="#<?php print $db_field['file_id']; ?>" data-parent="#accordion" data-toggle="collapse" class="accordion-toggle collapsed">
<?php print $db_field['file_name']; ?>
</a>
</div>
<div class="accordion-body collapse in" id="<?php print $db_field['file_id']; ?>">
<div class="accordion-inner">
<?php print $db_field['file_description']; ?><br/><br/>
Download File Now!
<br/><br/>
</div>
</div>
</div>
<?php } ?>
The code is suppose to show only the files associated to the user.
What you need to do is JOIN the tables.
Most common types of JOINs:
INNER JOIN - Used to match data where it can match the ON () statement. If the ON() doesn't match, the result will be excluded.
LEFT JOIN - Used if the data you match in the ON() doesn't have to be there. It just appends the data to the original FROM, and fills columns with NULL if no data is matched.
Example
SELECT
ft.id,
ft.file_name,
ft.file_description,
ft.file_url,
af.id as access_id,
af.student_id,
af.file_id
FROM
files ft
INNER JOIN access_files af ON ( ft.id = af.file_id )
WHERE
fa.student_id = '$studentid'
You are making a classic cartesian join with your query:
SELECT * FROM files, access_files WHERE student_id ='$studentid'
You need to specify how the two tables are connected:
SELECT * FROM files a, access_files b WHERE a.student_id ='$studentid' and b.studentID=a.student_id
If you don't specify the link - or don't have one, the database will try to link every single row in the first table with every single row in the second.
Join your tables.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table1.pk = table2.fk
WHERE table1.pk = 1;

How to display all comments per article (PHP & SQL)?

So I have two tables, article and comments (which has one-to-many relationship (1 article - many comments)). This is how the table is structured:
Articles - id (prikey), title, publicationDate, content
Comments - com_id (prikey), author, comment, id (foreign key)
I used this to query the two tables:
SELECT * FROM articles as a INNER JOIN comments as c ON a.id = c.id
Previously, I was only displaying the articles table using this:
<?php
while($row = mysqli_fetch_array($query)) {
echo "
<div id='article'>
<header>
<hgroup>
<h2>".$row['title']."</h2>
<h4>Posted on ".$row['publicationDate']."</h4>
</hgroup>
</header><p>".$row['content']."</p></div>";
}
?>
This displays all articles (with date, title, content, etc.). Now there are comments. How do I edit the php code (or if my query is incorrect, how to write the query), so that it shows all articles and all comments per article as in:
Article One
-- Comment 1
-- Comment 2, etc.
Article Two
-- Comment 1
-- Comment 2, etc.
An alternative would be to split the query into two.
The first would bring back the articles you want...
SELECT * FROM article;
Once you have those, you can get all the IDs and use something like the following
SELECT * FROM comments WHERE article_id IN (".$list.");
This restricts the MySQL queries to 2 whilst getting all the data you need. After this loop around the article data, and in that loop, loop around the comments data.
This also means that, unlike using GROUP_CONCAT, you will also have author data to use.
It's not a very eloquent solution, but should work.
Query:
SELECT c.author, c.comment,
a.id article_id, a.title, a.publicationDate, a.content
FROM comments c
RIGHT JOIN articles a
ON c.id = a.id
PHP:
<?php
$lastArticleId = 0;
$isNewArticle = false;
while($row = mysqli_fetch_array($query)) {
$isNewArticle = ($lastArticleId != $row['article_id']) ? true : false;
if($isNewArticle) {
$lastArticleId = $row['article_id']; ?>
<div class="article">
<header>
<hgroup>
<h2><?php echo $row['title']; ?></h2>
<h4>Posted on <?php echo $row['publicationDate']; ?></h4>
</hgroup>
</header>
<p><?php echo $row['content']; ?></p>
</div>
<?php
}
if($row['comment'] != '') { ?>
<p><strong><?php echo $row['author']; ?></strong> - <?php echo $row['comment']; ?></p>
<?php
} ?>
<?php
} ?>
Use something like
SELECT a.article
,GROUP_CONCAT(CONCAT('<p>', c.comment, '</p>') SEPARATOR "\n") as comments
FROM
article a
INNER JOIN comment c ON (c.article_id = a.id)
WHERE a.id = '12454';
You may have to fiddle a bit with the separator.
See: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
Do note however:
The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:
SET [GLOBAL | SESSION] group_concat_max_len = val;
See here how to change max_allowed_packet
http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_max_allowed_packet
Look into MySQL GROUP_CONCAT which will return a comma delimited list of items. You can then explode that for your comments section.
once a person will comment on a article insert article id with that comment and later get them accordingly something like this
once a person will select an article to read send article id in the $_GET to your article page so you can excess the article id.Once a person will comment on that article insert it as follows
$sql = mysql_query("INSERT INTO comments_table (subject,article_id,comments) VALUES ('$subject','$_GET['id']','$comments')");
and later when you pulling them do it the same way as you have the article id in the $_GET
you can access it run a query like this
$fetch = mysql_query("SELECT * FROM comments WHERE article_id = $_GET['id'] ORDER BY id DESC") or die(mysql_error());
Hope this will work

Categories