dongpan9760 2015-11-22 09:29
浏览 37

另一个动态mysql表

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.

  • 写回答

2条回答 默认 最新

  • doubi1910 2015-11-22 10:00
    关注

    enter image description here

    This is my interpretation of your MySQL tables. From what I can see, you are trying to have a system in which you can easily click on a customer and write/view written comments about them. This query will individually get comments along with the customers username from the database for the customer with the 'id' = 1.

    SELECT comments.comment, users.username FROM comments, users WHERE comments.customerid = users.customerid AND users.customerid = 1
    

    This is a very generic answer due to the amount of information given. If you have any questions or concerns, please comment!

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。