How can i link two mysql tables. One that (already) displays customers records And another table specifically for a commenting system about each record entered.?
I.e Account #1 // displays that particular customer info.. My need is.... I need to have a system to post comments only about that record (Account # 1).. and so on and so on with all future ones.
I have two tables Customers table and Comments table...
Conclusion. .
How can I link them so that I can comment on each customers tables record individually?
Thank you very much
this is my code below....
<?php
include('../includes/mysql_connect.php');
$query = "CREATE TABLE `customers` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`cust_name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)engine=innodb";
if(@mysql_query($query,$dbc)){
echo '<p>Table for customers has been successfully created!</p><br/>';
} else {
echo '' . mysql_error($dbc) .'<br/>';
}
mysql_close($dbc);
?>
<?php
include('../includes/mysql_connect.php');
$query = "CREATE TABLE `comments` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`customer` INT(10) UNSIGNED NOT NULL,
`comment` VARCHAR(255)NOT NULL,
PRIMARY KEY (`id`),
INDEX `customer` (`customer`)
)engine=innodb";
if(@mysql_query($query,$dbc)){
echo '<p>Table for comments has been successfully created!</p><br/>';
} else {
echo '' . mysql_error($dbc) .'<br/>';
}
mysql_close($dbc);
?>
/* ABOVE CODE CREATES THE TABLES */
/* CODE BELOW INSERTS*/
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// NEEDS DATABAE CONNECTION
include('includes/mysql_connect.php');
//CREATE VARIBLE IF PROBLEM DOES NOT OCCUR
$problem = FALSE;
// VALIDATION
if(!empty($_POST['cust_name'])){
// REAL ESCAPE STRING AGAINST SQL INJECTION
$cust_name = mysql_real_escape_string(trim(strip_tags($_POST['cust_name'])), $dbc);
} else { // IF PROBLEM
echo '<p style="color:red;">Please enter name!</p>';
$problem = TRUE;
}
// IF NO PROBLEM
if(!$problem){
//RUN QUERY
$query = "INSERT INTO `customers` (`cust_name`) values ('$cust_name')";
// EXECUTE QUERY
if(@mysql_query($query, $dbc)){ // NO PROBLEM
echo '<p style="color:blue;">This name has been added!<p>';
} else {// IF PROBLEM
echo '<p style="color:red;">This name was not successfully added!</p>';
}
} // END OF VARIABLE IN NO PROBLEM
mysql_close($dbc); // CLOSING CONNECTION
} // END OF MAIN IF
?>
/* CODE BELOW RECEIVES */
<?php
include('includes/mysql_connect.php');
//$query = 'SELECT * FROM customers';
$query = 'SELECT * FROM customers';
if($y = mysql_query($query,$dbc)){
while($row = mysql_fetch_array($y)){
//echo " {$row['id']} <br/>";
echo "<div id='container'>
<div id='first_wrapper'>
<a style='text-decoration:none'; href=\"fetch_join.php?id={$row['id']}\">
Stolve # " . $row['user_id'] ."
</a></div>";
}
}
mysql_close($dbc);
?>
/* CODE BELOW SELECTS */
<?PHP
include('includes/mysql_connect.php');
/* Select records for specific user based upon their ID */
$query = "SELECT c.`id` as 'customer_id', ct.`id` as 'comment_id', c.`cust_name` as 'customer', ct.`comment` from `customers` c
inner join `comments` ct on ct.`customer`=c.`id`
where c.`id`=1";
if($y = mysql_query($query,$dbc)){
while($row = mysql_fetch_array($y)){
echo "{$row['customer']} {$row['comment']}<br/>";
//echo " {$row['id']} <br/>";
}
}
mysql_close($dbc);
?>
But no matter how many records I enter, I keep on getting all of them instead of just the one for the particuar ID.
Below each record I have entered but the user ID still shows as one.
1 This is a test for joining tables 1 this is a second test 1 third test 1 another comment here 1 fourth test 1 last test 4
I should be able to see only one record per click.