如何链接两个mysql表。 一个(已经)显示客户记录 还有一个专门针对每个记录的评论系统的表格。?
即 帐户#1 //显示特定的客户信息.. 我的需要是...... 我需要一个系统来发布关于该记录的评论(帐户#1)..等等以及所有未来的记录。
我有两张桌子 客户表和 评论表......
结论。
如何链接它们以便我可以单独评论每个客户表记录?
非常感谢
这是我的代码......
<?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);
?>
但无论我输入多少记录,我都会继续获取所有记录 而不只是特定ID的那个。
在我输入的每条记录下方,但用户ID仍显示为一个。
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
我应该只能看到每次点击一条记录。
答案 0 :(得分:1)
这是我对你的MySQL表的解释。从我所看到的,您正在尝试建立一个系统,您可以轻松地点击客户并撰写/查看有关它们的书面评论。此查询将分别从“数据库= 1”的客户的数据库中获取评论以及客户用户名。
SELECT comments.comment, users.username FROM comments, users WHERE comments.customerid = users.customerid AND users.customerid = 1
由于提供的信息量很大,这是一个非常通用的答案。如果您有任何问题或疑虑,请发表评论!
答案 1 :(得分:0)
回答您的问题"do both tables have to be created at the same time so their Id's start both from 1?"
〜否。
下面的sql创建了两个表 - comments表有一个键控字段(customer
),它链接到id
表中的customers
字段。
create table `customers` (
`id` int(10) unsigned not null auto_increment,
`cust_name` varchar(50) not null,
primary key (`id`)
)engine=innodb;
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;
/* Create a dummy / test user in the customers table */
insert into `customers` (`cust_name`) values ('Bob');
/* Insert a dummy / test record in the comments table */
insert into `comments` (`customer`,`comment`) values (1,'Bob is the best customer in the World, ever! However, he rarely pays on time and has bad body odour');
/* Select ALL records from both tables using the customer id as a key */
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`;
/*
Note the JOIN type is `inner` - this shows only records where there is
a value in both. If the JOIN type was `left outer` there would be records
for all regardless of whether or not a comment exists for that customer
*/
/* Select records for specific user based upon their ID */
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;
@AndyChavarria:sql旨在说明如何将评论链接到客户,而不打算运行最终版本!也就是说,我稍微整理了一下php并进行了一些小修改......如果有关于sql注入的评论或关于使用已弃用的mysql_*
函数系列,请不要感到惊讶。如果这还处于生产/构建的早期阶段,那么你真的应该考虑实施mysqli
或PDO
〜mysqli
需要更少的更改但是有一些微妙的变化语法差异。从长远来看,PDO
可能更强大,而且肯定更灵活 - 但是,如果正确实现,这两种方法都会阻止SQL注入,而你的代码即使在转义时仍然容易受到攻击。
例如,mysql和amp;之间的许多细微差别之一mysqli的:
mysqli_query( $conn, $sql )
而不是mysql( $sql, $conn )
<?php
include('../includes/mysql_connect.php');
/* To build the tables: this should only need to be run once! */
$query = "CREATE TABLE `customers` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`cust_name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)engine=innodb";
$res=mysql_query( $query, $dbc );
echo $res ? '<p>Table for customers has been successfully created!</p><br/>' : mysql_error( $dbc ) .'<br/>';
$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";
$res=mysql_query( $query, $dbc );
echo $res ? '<p>Table for comments has been successfully created!</p><br/>' : mysql_error($dbc) .'<br/>';
mysql_close( $dbc );
?>
<?php
/* Insert comment */
if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
include('includes/mysql_connect.php');
$problem = FALSE;
if( isset( $_POST['cust_name'] ) && !empty( $_POST['cust_name'] ) ){
$cust_name = mysql_real_escape_string( trim( strip_tags( $_POST['cust_name'] ) ), $dbc );
} else {
echo '<p style="color:red;">Please enter name!</p>';
$problem = TRUE;
}
if( !$problem ){
$query = "INSERT INTO `customers` (`cust_name`) values ('$cust_name')";
$res=mysql_query( $query, $dbc );
echo $res ? '<p style="color:blue;">This name has been added!<p>' : '<p style="color:red;">This name was not successfully added!</p>';
}
mysql_close($dbc);
}
?>
<?php
/* Fetch customers and create hyperlink */
include('includes/mysql_connect.php');
$query = 'SELECT * FROM `customers`';
$res=mysql_query( $query, $dbc );
if( $res ){
/* Assumed that the container does not get repeated */
echo "<div id='container'>";
while( $row = mysql_fetch_array( $res ) ){
/* Here you have the same id used for each iteration of the loop.... this is not valid html */
echo "
<div id='first_wrapper'>
<a style='text-decoration:none' href='fetch_join.php?id={$row['id']}'>Stolve # {$row['user_id']}</a>
</div>";
}
echo "</div>";
}
mysql_close($dbc);
?>
<?php
/* Show specific customer ( based upon id ) and associated comments */
include('includes/mysql_connect.php');
$id=isset( $_GET['id'] ) ? intval( mysql_real_escape_string( trim( strip_tags( $_GET['id'] ) ), $dbc ) ) : 1;
$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`='{$id}';";
$res=mysql_query( $query, $dbc );
if( $res ){
while( $row = mysql_fetch_array( $res ) ){
echo "{$row['customer']} {$row['comment']}<br/>";
}
} else {
echo 'No comments for selected user';
}
mysql_close( $dbc );
?>