0

Hi I'm trying to save player information and update it if information already exists but I can't seem to figure out why my query won't work. it looks sound to me.

<?php
include("DBTools.php");
$link=dbConnect();

$name = safe($_POST['name']);
$level = safe($_POST['level']);
$experiance = safe($_POST['experiance']);
$health = safe($_POST['health']);
$maxHealth = safe($_POST['maxHealth']);

$posx = safe($_POST['posx']);
$posy = safe($_POST['posy']);
$posz = safe($_POST['posz']);


    $query = "IF EXISTS(select * from 'PlayerStats` where name = '$name') UPDATE 'PlayerStats` SET level = '$level', experiance = '$experiance', health = '$health', maxHealth = '$maxHealth',posx = '$posx', posy = '$posy', posz = '$posz' WHERE name = '$name' ELSE INSERT INTO `PlayerStats`(`name`, `level`, `experiance`, `health`,`maxHealth`, `posx`, `posy`, `posz`) VALUES ('$name','$level','$experiance','$health','$maxHealth','$posx','$posy','$posz')";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

?>

i don't really have access to any logs so i cant see any errors

I have also tried this which doesn't work either

$sql = "SELECT * FROM PlayerStats` WHERE name = '$name'";
$result = mysql_query($sql);
if(mysqli_num_rows($result) !== 1){
    $query = "INSERT INTO `PlayerStats`(`name`, `level`, `experiance`, `health`,`maxHealth`, `posx`, `posy`, `posz`) VALUES ('$name','$level','$experiance','$health','$maxHealth','$posx','$posy','$posz')";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
}else{
    $query = "UPDATE `PlayerStats` SET level = '$level', experiance = '$experiance', health = '$health', maxHealth = '$maxHealth', posx = '$posx', posy = '$posy', posz = '$posz' WHERE name = '$name';
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
}

if i just insert it works fine but its just when i try and update nothing works

Dip Hasan
  • 225
  • 3
  • 9
pvtctrlalt
  • 61
  • 2
  • 9
  • 3
    Please don't combine an old deprecated api `mysql_` with `mysqli`.. If your hosting provider upgrades the php version to `7.0+` , you'll have issues – Rotimi Jan 05 '18 at 20:02
  • Your first code block's query mixes up backticks and single quotes on the table names, the second code block mixes `mysql` and `mysqli` functions and these are not compatible. – cteski Jan 05 '18 at 20:04
  • thank you. ive been copying php scripts from a website i made ages ago must have just missed it :) – pvtctrlalt Jan 05 '18 at 20:16
  • 1
    You should learn to use `INSERT ... ON DUPLICATE KEY UPDATE` so you don't have to do multiple queries. – Barmar Jan 05 '18 at 20:40
  • i cant use that in this case as my game never has access or knows about the unique identifier and has no way of linking it – pvtctrlalt Jan 05 '18 at 22:15

0 Answers0