0

Checking with phpcodechecker not showing the error but can't insert data into mysql.

PHP version: 5.6 Server type: MariaDB

here the code

header('Access-Control-Allow-Origin: *');

include "config.php";

$dblink = mysqli_connect($host,$dbu,$dbp,$db);

if (!$dblink) {
error_log( "Error: Unable to connect to MySQL." . PHP_EOL);
error_log("Debugging errno: " . mysqli_connect_errno() . PHP_EOL);
error_log("Debugging error: " . mysqli_connect_error() . PHP_EOL);
exit;
}


if (isset($_GET['name']) &&isset($_GET['score'])){
$name = strip_tags(mysqli_real_escape_string($dblink, $_GET['name']));//get data from column USER
$score = strip_tags(mysqli_real_escape_string($dblink, $_GET['score']));//get data from column HIGHscore
$sql=("SELECT * FROM scores WHERE name='$name';");//choose userdata table from database where column USER
$result = $dblink->query($sql);//execute database


if ($result->num_rows > 0){ //if result > 0, if any data on tables 

$row = $result->fetch_assoc(); 
if ((int)$row['score'] < (int)$score){ //score on database less than score input will execute database
$sql=("INSERT scores SET name='$name', score='$score' ;"); //Update score 
if ($dblink->query($sql) === TRUE) {
echo "Success";//this is if success
} 
else 
{
echo "Error updating record: " . $dblink->error;//this is if failed or error
}
}
}
}
// echo not effect
    mysqli_close($dblink);

whether the use of .htaccess affects data insert ?

SOLVED

Delete this code if ($result->num_rows > 0) {

2
  • try to indent your code properly and will it easier for people to read and expose any errors. Commented Aug 12, 2018 at 4:30
  • I don't believe .htaccess affects database connections. Its more related to what directories the server has access to as I understand it. Commented Aug 12, 2018 at 4:35

1 Answer 1

2

The format of your INSERT query is wrong (you have used the UPDATE form). You should use:

INSERT INTO scores (name, score) VALUES('$name', '$score')

See the manual...

In PHP:

$sql = "INSERT INTO scores (name, score) VALUES('$name', '$score')";
Sign up to request clarification or add additional context in comments.

3 Comments

doesn't work, Does PHP version also need to be considered?
Can you be a bit more specific? I can't tell what is wrong from "doesn't work"
thanks it work, i just delete this if ($result->num_rows > 0) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.