php - 动态表内连接

标签 php mysql database

好的..这是这里的最新信息。 我需要在每个 ID 递增时为其生成注释。 IE 我有客户 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>

请帮我解决这个问题..我已经在这个问题上一周了,我累了..谢谢

最佳答案

根据 davejal 的评论:FK 约束表示评论customers_id 中的每个值都必须是客户customers_id 中的值

所以肖恩说:首先INSERT INTO customers (cust_name) values ('$cust_name')它使用客户的 AUTO_INCRMENT 作为客户 ID。然后INSERT INTO comments (customers_id, comment) VALUES (LAST_INSERT_ID(), '$comment')它使用 LAST_INSERT_ID 作为该 AUTO_INCRMENT 选择的客户customers_id 值,并使用评论的 AUTO_INCRMENT 作为 comments_id。

Per Sean 和 philipxy:客户插入 AUTO_INCREMENT 使您成为一个新的客户 ID。如果您立即想要它,请使用 LAST_INSERT_ID();否则,当他们登录或管理员提供时,您会获得他们的 ID。 Per Ryan Vincent 的 sqlfiddle:A version modified to use LAST_INSERT_ID() .

Per Sean 和 Ryan Vincent:当您使用 PHP 进行 I/O 时,请在问题中显示它。如果您在表单中获取了要处理其评论的客户 ID(echo '<input type="hidden" name="customer_id" value="' . $_GET['customer_id'] . '"/>';),那么就是您的评论中使用的值,通过 $_GET['customer_id'] 插入或选择.

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

关于php - 动态表内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33907315/

相关文章:

MySQL 选择具有特定属性的计数行

mysql - 外键和基本关系表

php - 如何将部分字符串与mysql中的大型数据库进行比较

PHP/MYSQL - 在 for 循环内仅生成一个随机单词

mysql - 给出 mysql 数据库列排名

java - 如何编写 sql 查询以使用准备好的语句更新表的多个列?

ruby-on-rails - 如何获取 Rails 表中每个不同名称的第 n 条记录?

javascript - 在 AngularJS 中用公式调用 Controller

php - 如何更改从 Controller 发送的电子邮件中的语言环境 - laravel 5.4

php - 添加到购物车每个产品的最大数量并在 WooCommerce 中进行验证