动态表INNER JOINS

时间:2015-11-25 02:03:00

标签: php mysql database

好的..这是最新的信息。     我需要为每个ID增加时生成注释。即     我有客户1.他/她注册然后我有能力在他们的帐户上工作,无论我在做什么......我需要在该特定页面上发表评论。 "我能做什么"     第二个客户注册,然后在用户2的链接上,我可以访问他们的帐户并留下评论仅用于管理目的..

"我不能因为我只能在ID#1指示插入而进行此操作。我不知道如何指出新ID"

第三位客户注册......依此类推......   代码如下。我在这里也包含了所有PHP代码..您可以使用自己的数据连接在最后测试它。

/* CREATING THE TABLE Customers*/
      <?php
include('../includes/mysql_connect.php');
$query = "CREATE TABLE `Customers` (
`customer_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`cust_name` VARCHAR(50) NOT NULL,
INDEX (`customer_id`),
PRIMARY KEY (`customer_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);
?>


/* CREATING THE TABLE Comments*/
<?php 
include('../includes/mysql_connect.php');
$query = "CREATE TABLE `Comments` (
`comment_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`customer_id` INT(10) UNSIGNED NOT NULL,
`comment` VARCHAR(255) NOT NULL,
PRIMARY KEY  (`comment_id`),
INDEX (`customer_id`),
FOREIGN KEY (`customer_id`) REFERENCES `Customers`
(`customer_id`)
)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);
?>

/* So now that I have already created the tables, I work on getting the link for each customer. */

<?php
include('includes/mysql_connect.php');

$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=\"http://www.mywebsite.com/master/account.php?id={$row['customer_id']}\"> 
Customer # " . $row['customer_id'] ." 
</a></div>";
}
}
mysql_close($dbc);
?>


/* The next step is to retrieve the info From the Customers table */
/* Select records for specific user based upon their ID */
<?php

include('includes/mysql_connect.php');

if(isset($_GET['id']) && is_numeric($_GET['id'])) { 

$query = "SELECT * FROM Customers WHERE `customer_id`={$_GET['id']}";
if($x = mysql_query($query,$dbc)){

$row = mysql_fetch_array($x); 

echo "{$row['cust_name']}<br/>";

echo '<input type="hidden" name="id" value="' . $_GET['id'] . '"/>';    
}
}
mysql_close($dbc);  

?>

/* My problem comes here on the comments table */


if($_SERVER['REQUEST_METHOD'] == 'POST'){
// NEEDS DATA CONNECTION
include('includes/mysql_connect.php');
// VARIABLE IF PROBLEM DOES NOT OCCUR
$problem = FALSE;
// VALIDATION HERE
if(!empty($_POST['comment'])){

$comment = mysql_real_escape_string(trim(strip_tags($_POST['comment'])), $dbc);
} else { // IF PROBLEM
echo '<p style="color:red;">Please enter comment!</p>';
$problem = TRUE;
}
//IF NO PROBLEM
if(!$problem){

// RUN QUERY

$query = "INSERT INTO `Comments`(`customer_id`,`comment`) VALUES (1,'$comment')";
// EXECUTE QUERY
if(@mysql_query($query,$dbc)){
echo '<p style="color:blue;">This comment has been added!<p>';
} else {// IF PROBLEM
echo '<p style="color:red;">Could not retrieve the information because <br/> 
'. mysql_error($dbc) .'.</p><p>The query run was '.$query.' </p>';
}
} // END OF VARIABLE IN NO PROBLEM
mysql_close($dbc); // CLOSING CONNECTION
} // END OF MAIN IF
?>
<html>
<head><title>DUMMY INSERTS</title></head>
<body>
<form action="self.php" method="post">
<textarea name="comment" cols="70" rows="10"/></textarea>
<br/>
<input type="submit" name="submit" value="Submit!"/>
</form>
</body>
</html>

请帮我解决这个问题......我已经有一个星期了,而且我已经累了...... THX

1 个答案:

答案 0 :(得分:0)

Per davejal的评论:FK约束说评论中的每个值customers_id必须是客户customers_id 中的值。

Sean说:首先INSERT INTO customers (cust_name) values ('$cust_name')使用客户&#39; customers_id的AUTO_INCREMENT。然后INSERT INTO comments (customers_id, comment) VALUES (LAST_INSERT_ID(), '$comment')使用LAST_INSERT_ID为客户customer_id值选择该AUTO_INCREMENT并发表评论&#39}。 comments_id的AUTO_INCREMENT。

Per Sean&amp; philipxy:客户插入AUTO_INCREMENT使您成为新的客户ID。如果你想立即使用LAST_INSERT_ID();否则,当他们登录或管理员给出时,你会得到他们的身份证明。根据Ryan Vincent的说法:A version modified to use LAST_INSERT_ID()

Per Sean&amp; Ryan Vincent:当您使用PHP进行i / o时,请在您的问题中显示它。如果您获取了要在表单中处理其注释(echo '<input type="hidden" name="customer_id" value="' . $_GET['customer_id'] . '"/>';)的客户ID,那么 是您在注释中使用的值INSERT或SELECT $_GET['customer_id']

所有这些最小的fiddled代码,输入,输出(可能包括执行期间的调试输出,虽然这可能会影响bug的表现)和错误应该在每个关于修复代码的问题中。阅读并采取行动How to create a Minimal, Complete, and Verifiable example