[英]Inserting into mysql table using foreign key with PHP
Hi I have two tables,嗨,我有两张桌子,
users_table and orders table. users_table 和 orders 表。
users_id is in both tables users_id 在两个表中
as a primary key in users_table and as foreign key in orders_table(referencing users_id in users_table)作为 users_table 中的主键和 orders_table 中的外键(在 users_table 中引用 users_id)
When I try to place an order if it's the first time the user is able to place an order but if a user already placed an order the data is not saved to the database in the second attempt.. any Idea why?当我尝试下订单时,如果这是用户第一次下订单,但如果用户已经下订单,则第二次尝试时数据不会保存到数据库中.. 知道为什么吗? or any solutions?或任何解决方案?
I apologise for the bad english我为糟糕的英语道歉
MY PHP CODE:我的 PHP 代码:
$query = "INSERT INTO orders_table(users_id, orders_postDate, orders_category, orders_categoryId, orders_name, orders_description, orders_deliveryDate) VALUES('$users_id', '$orders_postDate', '$orders_category', '$orders_categoryId', '$orders_name', '$orders_description', '$orders_deliveryDate')";
$result = mysqli_query($connection, $query);
if($result){
echo "SUCCESS";
}else{
echo "fail";
}
so Let's say that I created an account, signed in and placed an order, the data is successfully on the database.所以假设我创建了一个帐户,登录并下订单,数据成功上数据库。 if I try to place a new order with the same user I get the fail message.. so for some reason如果我尝试向同一用户下新订单,我会收到失败消息..所以出于某种原因
mysqli_query($connection, $query);
fails the second time, I am assuming that it is because my foreign key is a primary key?第二次失败,我假设这是因为我的外键是主键? how can I fix this?我怎样才能解决这个问题?
Yes, you are right, if orders_table.user_id
with primary key, also there is a ' unique
' key. 是的,您是对的,如果orders_table.user_id
具有主键,那么也有一个“ unique
”键。 If so, MySql did not allow to paste the same user_id
. 如果是这样,则MySql不允许粘贴相同的user_id
。
Use ALTER TABLE
sql command to remove the primary key on orders_table.user_id
add add a new column with primary key. 使用ALTER TABLE
sql命令删除orders_table.user_id
上的主键,添加带有主键的新列。
There is code you need: 您需要以下代码:
ALTER TABLE orders_table DROP PRIMARY KEY;
ALTER TABLE orders_table ADD COLUMN `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE orders_table ADD PRIMARY KEY(`id`);
Without code/error message it is difficult to say what the issue is. 没有代码/错误消息,很难说出问题所在。 However, you mentioned that user_id
is the foreign key for the orders_table
, but if it is also used for the primary key for your orders_table
it would cause duplication when creating any subsequent orders, hence, insertion of a new order would fail. 但是,您提到过user_id
是orders_table
的外键,但是如果它也用作orders_table
的主键,则在创建任何后续订单时会导致重复,因此,新订单的插入将失败。
These might help you to help us to get a better understanding of whats going on. 这些可能会帮助您帮助我们更好地了解正在发生的事情。
orders_table
created? 您的orders_table
如何创建? Orders_table
? Orders_table
的主键是Orders_table
? Also, since you are dealing with MySQL and PHP you might want to take a look into using PDO objects instead, if you haven't already. 另外,由于您正在使用MySQL和PHP,因此,如果您尚未使用PDO对象,则可能需要考虑一下。 Being able to use prepared statements is a huge plus. 能够使用准备好的语句是一个巨大的优势。
Your insert query is not getting data from the users_table and so will not be able to insert the right user data into the Orders_table;您的插入查询未从 users_table 获取数据,因此将无法将正确的用户数据插入 Orders_table; and second, the order_table is created having user_id as a primary key and foreign key which will cause data duplication.其次,创建的 order_table 以 user_id 作为主键和外键,这将导致数据重复。 Try altering the table to drop the primary and create another column for it(Primary key).尝试更改表以删除主并为其创建另一列(主键)。
An example一个例子
ALTER TABLE orders_table DROP PRIMARY KEY;
ALTER TABLE orders_table ADD COLUMN 'id' int NOT NULL AUTO_INCREMENT PRIMARY KEY;
That should make your code work now.这应该使您的代码现在可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.