Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

PHP & Mysql fatal error

Hey.

Currently doing a example to have something to show, but I just can't get this to work.
Error I get when trying this code:
Code
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' g, b = '-3', '213', '8' WHERE id = '4'' at line 1 in C:\xampp\htdocs\mysqlConfig.php:13 Stack trace: #0 C:\xampp\htdocs\mysqlConfig.php(13): PDOStatement->execute(Array) #1 C:\xampp\htdocs\example.php(333): query('UPDATE char SET...', Array) #2 {main} thrown in C:\xampp\htdocs\mysqlConfig.php on line 13

I'm not really great at this and just wanted to set up a example. Anyone have any idea what I did wrong? The code is to change RGB colors in a mysql table.
It also shows the current RGB in the form

PHP code:
PHP Code

<?php
if(isset($_SESSION['userID'])){
 
$data = query("SELECT r, g, b FROM char WHERE id = :uid LIMIT 1", array(
 
"uid" => $_SESSION['userID']
 ));
 if(
$data->rowCount() > 0)
 {
 
$data = $data->fetch(PDO::FETCH_ASSOC);
 
$r = $data['r'];
 
$g = $data['g'];
 
$b = $data['b'];
 }else{
 
$_SESSION['userID'] = false;
 
session_destroy();
 }
}
if(isset(
$_GET['changecolor_char1']))
{
 
 if(
count($_POST) > 0)
 {
 {
 
$ins = query("UPDATE char SET r, g, b = :r, :g, :b WHERE id = :uid", array(
"r"    => $_POST['r'],
"g"    => $_POST['g'],
"b"    => $_POST['b'],
 
"uid"       => $_SESSION['userID']
 ));
 
header("Refresh:0");
 }
 }
 
?>

Form:
PHP Code

<?php 
<form action="" method="post">
 
R:
 <
input required="" name="r" autocomplete="off" value="<?php echo $r; ?>"/><br/><br/>
 
G:
 <
input required="" name="g" autocomplete="off" value="<?php echo $g; ?>"/><br/><br/>
 
B:
 <
input required="" name="b" autocomplete="off" value="<?php echo $b; ?>"/><br/><br/>
 </
form> 

mysqlConfig.php:
PHP Code

<?php
ini_set
('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
$conn = new PDO('mysql:host=localhost;dbname=removed;charset=utf8', 'removed', 'removed');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function 
query($query = "", $params = array())
{
 
   global $conn;
 
   $stmt = $conn->prepare($query);
 
   $stmt->execute($params);

 
   return $stmt;
}
?>
[Image: cpJG5qD.png]
(Feb 10, 2020 03:21 PM)Alex Wrote:  Hey.

Currently doing a example to have something to show, but I just can't get this to work. error I get when trying this code:
Code
Fatal _error_: Uncaught PDOException: SQLSTATE[42000]: Syntax _error_ or access violation: 1064 You have an _error_ in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' g, b = '-3', '213', '8' WHERE id = '4'' at line 1 in C:\xampp\htdocs\mysqlConfig.php:13 Stack trace: #0 C:\xampp\htdocs\mysqlConfig.php(13): PDOStatement->execute(Array) #1 C:\xampp\htdocs\example.php(333): query('UPDATE char SET...', Array) #2 {main} thrown in C:\xampp\htdocs\mysqlConfig.php on line 13

I'm not really great at this and just wanted to set up a example. Anyone have any idea what I did wrong? The code is to change RGB colors in a mysql table.
It also shows the current RGB in the form

PHP code:
PHP Code

<?php
if(isset($_SESSION['userID'])){
 
$data = query("SELECT r, g, b FROM char WHERE id = :uid LIMIT 1", array(
 
"uid" => $_SESSION['userID']
 ));
 if(
$data->rowCount() > 0)
 {
 
$data = $data->fetch(PDO::FETCH_ASSOC);
 
$r = $data['r'];
 
$g = $data['g'];
 
$b = $data['b'];
 }else{
 
$_SESSION['userID'] = false;
 
session_destroy();
 }
}
if(isset(
$_GET['changecolor_char1']))
{
 
 if(
count($_POST) > 0)
 {
 {
 
$ins = query("UPDATE char SET r, g, b = :r, :g, :b WHERE id = :uid", array(
"r" => $_POST['r'],
"g" => $_POST['g'],
"b" => $_POST['b'],
 
"uid"       => $_SESSION['userID']
 ));
 
header("Refresh:0");
 }
 }
 
?>

Form:
PHP Code

<?php 
<form action="" method="post">
 
R:
 <
input required="" name="r" autocomplete="off" value="<?php echo $r; ?>"/><br/><br/>
 
G:
 <
input required="" name="g" autocomplete="off" value="<?php echo $g; ?>"/><br/><br/>
 
B:
 <
input required="" name="b" autocomplete="off" value="<?php echo $b; ?>"/><br/><br/>
 </
form> 

mysqlConfig.php:
PHP Code

<?php
ini_set
('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
$conn = new PDO('mysql:host=localhost;dbname=removed;charset=utf8', 'removed', 'removed');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function 
query($query = "", $params = array())
{
 
   global $conn;
 
   $stmt = $conn->prepare($query);
 
   $stmt->execute($params);

 
   return $stmt;
}
?>

There is an error in your update statement
PHP Code

<?php 
$ins 
= query("UPDATE char SET r = :r, g = :g, b = :b WHERE id = :uid", array(
"r"    => $_POST['r'],
"g"    => $_POST['g'],
"b"    => $_POST['b'],
 
"uid"       => $_SESSION['userID']
 )); 
(Feb 10, 2020 03:43 PM)Zexture Wrote:  There is anerror in your update statement
PHP Code

<?php 
$ins 
= query("UPDATE char SET r = :r, g = :g, b = :b WHERE id = :uid", array(
"r"    => $_POST['r'],
"g"    => $_POST['g'],
"b"    => $_POST['b'],
 
"uid"       => $_SESSION['userID']
 )); 

That was it. Thank you!
[Image: cpJG5qD.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by Jack DrakeAug 30, 2019 08:12 PM 20 493 Aug 30, 2019 08:12 PM
Last Post: sdk
[PHP] by Jack DrakeApr 5, 2019 03:32 AM 12 288 Apr 5, 2019 03:32 AM
Last Post: xHeaven
[PHP] by JASFeb 24, 2018 05:53 PM 11 262 Feb 24, 2018 05:53 PM
Last Post: Nachfolger