Getting a SQL error from my insert query on my PHP page

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
alexwaston
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Aug 17, 2016 8:59 pm
Contact:

Wed Sep 28, 2016 7:52 pm

can anyone please tell me what I have managed to do wrong here in this code block please,
if I take out the query section and in comment the echo's under there I get the page working correct so I guess I messed up the sql insert,
when I run this code in my page I get this error,

"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 '5"
and a part post displayed back

Code: Select all

<?PHP
 //explode the post into an array
 $points = $_POST['points'];
 $player_name = explode(" ", $points);
 //get the INT points value
 $player_points = str_split($player_name, -3);
 // count the array length
 $player_count = count($player_name);
 for($i = 0; $i<$player_count; $i++) {
 $query = "INSERT INTO bg_points (
 player_name, points
 ) VALUES (
 '{$player_name}', {$points}
 )";
 $result = mysql_query($query, $connection);
 if ($result) {
 // Success!
 echo $player_name[$i];
 echo $player_points;
 } else {
 // Display error message.
 echo "<p>Failed To Post Your Points Please Correct The Errors And Try Again.</p>";
 echo "<p>" . mysql_error() . "</p>";
 }

 //echo $player_name[$i] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $player_points;
 //echo "<br>";

 }
 ?>
Relax: watch adventure movie | watch adult movie | watch jason movie . Good fun!
Last edited by alexwaston on Mon Oct 03, 2016 12:14 am, edited 1 time in total.

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Fri Sep 30, 2016 2:55 am

Try changing your query line from this:

Code: Select all

 $query = "INSERT INTO bg_points (
 player_name, points
 ) VALUES (
 '{$player_name}', {$points}
 )";
to this:

Code: Select all

 $query = "INSERT INTO bg_points (
 player_name, points
 ) VALUES (
 '". $player_name ."', ". $points .")";

thinsoldier
New php-forum User
New php-forum User
Posts: 24
Joined: Sat Dec 02, 2017 3:12 pm

Sun Dec 03, 2017 10:08 am

I suggest just using a query builder library like https://github.com/auraphp/Aura.SqlQuer ... /insert.md instead of doing sql string building yourself.

Post Reply