I'm new to PDO and php. I want to move a row from one table to another with a link i send to the script below.
LINK The "id" is used as a primary key in the invtable, (see FROM invtable in the script below)
submit-ads-florida.php?id=01820007985
SCRIPT submit-ads-florida.php
<?php
$host = "localhost";
$user = "user";
$password = "pass";
$database_name = "db";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$barCode = $GET['id'];
$query = "INSERT INTO adstable (Brand, Description, Price, Size, Price, Barcode)
SELECT Brand, Description, Price, Size, Price, Barcode FROM invtable
WHERE Barcode='".$barCode."'";
$pdo->query($query);
?>
PROBLEM
removed extra bracket by GET[id]). I'm getting the following error.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1110 Column 'Price' specified twice' in /home/myaccount/public_html/florida-ave/submit-ads-florida.php:16 Stack trace: #0 /home/myaccount/public_html/florida-ave/submit-ads-florida.php(16): PDO->query('INSERT INTO flo...') #1 {main} thrown in /home/myaccount/public_html/florida-ave/submit-ads-florida.php on line 16
UPDATE
I corrected multiple entries of Price. No data is being add to adstable and I'm not getting any errors.
Please check and correct this line:
$barCode = $GET['id'];
Let me know if this is not your solution.
Ok, so the issues you had/have were:
$barCode = $GET['id']);
should have been$barCode = $GET['id'];
, and possibly even$_GET['id'];
SELECT
query selects the same field twice(SELECT Brand, Description, >Price<, Size, >Price<)
INSERT INTO adstable (Brand, Description, >Price<, Size, >Price<
So let's address the issues:
Next, to use the same field twice in the
SELECT
query, you can define an alias, but you just don't need the same field twice...Then, to protect against first degree injection attacks, let's use a prepared statement instead of calling
PDO::query
with a GET parameter:The code, then should look something like this:
That should do the trick. But seriously: error messages tell you what's wrong Read them