b3253223b33v
b3253223b33v

Reputation: 27

Fail to insert data into mysql using php

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) {

Upvotes: 0

Views: 302

Answers (1)

Nick
Nick

Reputation: 147216

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')";

Upvotes: 2

Related Questions