0

I have this website I'm working on that matches up two objects and I found a pairing function to generate a unique id number for each matchup. That's all well and good, however I do not want to fill in the 7000 something unique numbers into a database. So, I thought of an idea. I was gonna use PHP and while loops to insert these values automatically. Unfortunately, I have run into quite the issue. This is the code I have so far:

<?php
$db = new PDO('mysql:host=hostname;dbname=db_name;charset=utf8', 'username', 'pasword');
$x = 2;
$y = 1;
while ($x <= 117) {
            $num = ((pow($x,2) + (3*$x) + (2 * $x * $y) + $y + pow($y,2))/2);
            echo $num ."<br>";
    $sql = "INSERT INTO votes (match) VALUES (:match)";
$q = $db->prepare($sql);
$q->execute(array(':match'=>$num));

            $x = $x + 1;
}

?>

and when I try to run this I get

Fatal error:

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'match) VALUES (?)' at line 1' in
/home/content/70/11524070/html/matchup/math.php:11 Stack trace: #0
/home/content/70/11524070/html/matchup/math.php(11):
PDO->prepare('INSERT INTO vot...') #1 {main} thrown in
/home/content/70/11524070/html/matchup/math.php on line 11

any ideas as to how to get this to work?

A. Donda
  • 8,381
  • 2
  • 20
  • 49
user1895377
  • 201
  • 1
  • 3
  • 17

2 Answers2

1

You are getting this error because MATCH is a reserved word in mysql. You have two options to fix this:

1.) change the name of your column to something else besides match. (RECOMMENDED)

2.) Use backticks in your sql string:

$sql = "INSERT INTO `votes` (`match`) VALUES (:match)";
A.O.
  • 3,733
  • 6
  • 30
  • 49
-1

Escape table and column names using backticks:

$sql = "INSERT INTO `votes` (`match`) VALUES (:match)";
Community
  • 1
  • 1
Sjoerd
  • 74,049
  • 16
  • 131
  • 175