简体   繁体   中英

Not able to copy data from one table to another

I have two tables: trade and user_pokemon_db

I want to copy a specific rows from user_pokemon_db, when an event occurs.

Html code:

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.

$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!??

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.

Keep in mind that in most of case, once the web page is sent to the user browser, the PHP execution is finished. 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.

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 :

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

And the trade.php would do something like that :

$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

( And if you are using GET or POST variables in your SQL query, be aware of SQL Injections )

If you want to run your PHP function without reloading the page, you should use AJAX . Check this page to understand how it works. A very easy way to use it is to use jQuery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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