简体   繁体   English

无法将数据从一个表复制到另一个表

[英]Not able to copy data from one table to another

I have two tables: trade and user_pokemon_db 我有两个表: tradeuser_pokemon_db

I want to copy a specific rows from user_pokemon_db, when an event occurs. 发生事件时,我想从user_pokemon_db复制特定行。

Html code: HTML代码:

echo "<a href='tradecenter.php' onClick='document.write(".trade($db_id).")' >Put in Trade</a>";

When the user clicks on the link, the php function is called which consists on sql query to copy the row. 当用户单击链接时,将调用php函数,该函数由sql查询组成以复制行。

$sql = " INSERT INTO trade (trade_id, user_id, pkmn_id, level, exp, health, attack1, attack2, attack3) 
SELECT (id, user_id, pkmn_id, level, exp, health, attack1, attack2, attack3) 
FROM user_pokemon_db WHERE user_id = '".$id."' AND id = '".$db_id."' ";

Problem maybe due to improper writting of the query.. or maybe due to improper formatting of the href!?? 问题可能是由于查询的写法不正确。或者是由于href格式不正确!

What should I do? 我该怎么办?

I don't know the content of your php function trade() but it seems that you are confusing javascript and PHP. 我不知道您的php函数trade()的内容,但似乎您在混淆javascript和PHP。

Keep in mind that in most of case, once the web page is sent to the user browser, the PHP execution is finished. 请记住,在大多数情况下,将网页发送到用户浏览器后,PHP执行就完成了。 If you want to do a SQL request after a link click, you need to load a new page or to use something like Ajax to run some PHP code again. 如果要在单击链接后发出SQL请求,则需要加载新页面或使用Ajax之类的东西来再次运行一些PHP代码。

The simplest way to do what you want is to pass the pokemon id as a GET variable (= in the URL) and check this variable on another page and generate the good SQL query : 执行所需操作的最简单方法是将神奇宝贝ID作为GET变量(URL中的=)传递,并在另一页上检查此变量并生成良好的SQL查询:

echo '<a href="trade.php?pokemon_id='.$id.'" >Trade </a>' ; 

And the trade.php would do something like that : 而且trade.php会做类似的事情:

$id = $_GET['pokemon_id'] ; // Be Aware of SQL Injections !!
trade($id); 

Have a look at this page for more information about forms : http://www.w3schools.com/php/php_forms.asp 请查看此页面以获取有关表单的更多信息: http : //www.w3schools.com/php/php_forms.asp

( And if you are using GET or POST variables in your SQL query, be aware of SQL Injections ) (并且,如果您在SQL查询中使用GET或POST变量,请注意SQL注入

If you want to run your PHP function without reloading the page, you should use AJAX . 如果要在不重新加载页面的情况下运行PHP函数,则应使用AJAX Check this page to understand how it works. 检查此以了解其工作原理。 A very easy way to use it is to use jQuery 一种简单的使用方法是使用jQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM