Hack Forums
[PHP] PHP & Mysql fatal error - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] PHP & Mysql fatal error (/showthread.php?tid=6048358)



PHP & Mysql fatal error - Alex - Feb 10, 2020

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;
}
?>



RE: PHP & Mysql fatal _error_ - Zexture - Feb 10, 2020

(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']
 )); 



RE: PHP & Mysql fatal _error_ - Alex - Feb 10, 2020

(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!