Jump to content

Weird SQL error


Go to solution Solved by mac_gyver,

Recommended Posts

Hey there! Here's what I got...

 

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 'WHERE username = 'Micard', password = '21232f297a57a5a743894a0e4a801fc3'' at line 1

File.php:

<?php
session_start();
include_once './connect.php';

if($_POST['cc_request'] != null && $_POST['cc_request'] == "create_cc") 
{
if(isset($_POST['player_gender']) && isset($_POST['player_path']))
{
    $PlayerGender = $_POST['player_gender'];
    $PlayerPath = $_POST['player_path'];
    $PlayerName = $_SESSION['username'];
    $PlayerPassword = $_SESSION['password'];
    
    $cc_query_sql = "INSERT INTO `players` (`gender`, `path`) VALUES ('".mysqli_real_escape_string($link, $PlayerGender)."', '".mysqli_real_escape_string($link, $PlayerPath)."') WHERE `username` = '".$PlayerName."', `password` = '".$PlayerPassword."'";
        
    $this_query = mysqli_query($link, $cc_query_sql) or die(mysqli_error($link));
    
    $this_query2 = mysqli_fetch_array($this_query);
    
    if(!$this_query2)
    {
        $cc_result = "couldnt_create";
        echo "cc_request=$cc_result";
        die();
    } else {
    
        $cc_result = "char_created";
        echo "cc_request=$cc_result";
    }
    
} else
{
    $cc_result = "pGpP_notset";
    echo "cc_request=$cc_result";
    die();
}
    
} else
{
    $cc_result = "not_exist";
    echo "cc_request=$cc_result";
    die();
}

?>
Link to comment
https://forums.phpfreaks.com/topic/297456-weird-sql-error/
Share on other sites

  • Solution

INSERT queries don't have WHERE clauses.

 

please read the documentation for whatever you are trying to do - http://dev.mysql.com/doc/refman/5.6/en/insert.html

 

perhaps you you are trying to UPDATE existing information, rather than inserting/creating it?

Link to comment
https://forums.phpfreaks.com/topic/297456-weird-sql-error/#findComment-1517291
Share on other sites

Uhm.. Yeah? Am I supposed to see something here? Sorry if I'm being dumb. It's been a while writing a whole bunch of scripts in 2 languages so I might have lost the "sight" if you know what I mean :)

 

I just read your error message, and didn't realize you were doing an INSERT statement.  As MacGyver indicated, INSERT doesn't support WHERE (however, you could do an SELECT with a WHERE which gets inserted).

 

I was pointing out that AFAIK your various WHERE clauses should be concatenated with AND or OR, and not commas.

Link to comment
https://forums.phpfreaks.com/topic/297456-weird-sql-error/#findComment-1517294
Share on other sites

Sorry guys, I don't want to create a separate topic for this, but mysqli_fetch_array fails in my code and returns false. I have no idea why. I tried to check it so many time already... The SQL seems to be fine this time.

<?php
session_start();
include_once './connect.php';

if($_POST['cc_request'] != null && $_POST['cc_request'] == "create_cc") 
{
if(isset($_POST['player_gender']) && isset($_POST['player_path']))
{
    $PlayerGender = $_POST['player_gender'];
    $PlayerGender = mysqli_real_escape_string($link, $PlayerGender);
    $PlayerPath = $_POST['player_path'];
    $PlayerPath = mysqli_real_escape_string($link, $PlayerPath);
    $PlayerName = $_SESSION['username'];
    $PlayerPassword = $_SESSION['password'];
    
    $cc_query_sql = "UPDATE players SET gender = '$PlayerGender', path = '$PlayerPath' WHERE username = '$PlayerName' AND password = '$PlayerPassword'";
        
    $this_query = mysqli_query($link, $cc_query_sql);
    $this_query2 = mysqli_fetch_array($this_query);
    
    if(!$this_query2)
    {
        $cc_result = "couldnt_create";
        echo "cc_request=$cc_result";
        die();
    } else {
    
        $cc_result = "char_created";
        echo "cc_request=$cc_result";
    }
    
} else
{
    $cc_result = "pGpP_notset";
    echo "cc_request=$cc_result";
    die();
}
    
} else
{
    $cc_result = "not_exist";
    echo "cc_request=$cc_result";
    die();
}

?>
Link to comment
https://forums.phpfreaks.com/topic/297456-weird-sql-error/#findComment-1517295
Share on other sites

Sorry, my brain has finally caught up.  :-[

 

Update queries don't return a result set. So fetch_array isn't going to work. If you're looking to see how many rows were affected, you could use the function defined here:

http://php.net/manual/en/mysqli.affected-rows.php

 

Or you could just test that the query successfully executed.

Edited by cyberRobot
Link to comment
https://forums.phpfreaks.com/topic/297456-weird-sql-error/#findComment-1517297
Share on other sites

Yeah, that's what I'm trying to do! :) How do I do that?

 

With the SELECT query I checked the number of rows returned, what about this case?

 

The mysqli_query() function returns false on failure. Otherwise it returns true (for update queries). More information can be found here:

http://php.net/manual/en/mysqli.query.php

Edited by cyberRobot
Link to comment
https://forums.phpfreaks.com/topic/297456-weird-sql-error/#findComment-1517303
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.