Php / Sql如果存在则更新其他插入不工作

时间:2018-01-05 20:00:02

标签: php sql unity3d

嗨我试图保存播放器信息并更新它,如果信息已经存在但我似乎无法弄清楚为什么我的查询不会工作。 它看起来很健康。

<?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());

&GT;

我真的无法访问任何日志,所以我看不到任何错误

我也试过这个剂量工作

$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());
}

如果我只是插入它工作正常但它只是当我尝试更新没有任何工作

0 个答案:

没有答案