2016-08-24 95 views
-1

我目前正在为我的课程项目做一个项目。我目前正在尝试更新到数据库中,但我沿途收到一些错误,基本上它是一个单选按钮,用于设置链接到更新页面。任何帮助和见解将不胜感激!MySQL和PHP更新

<html> 
<head> 
<title>asdf</title> 
<link rel="stylesheet" type="text/css" href="Background.css"> 
</head> 
<?php 
session_start(); 
if(!isset($_SESSION["login"])) 
header("location:admin.php"); 
?> 

<body> 
<h1 style="color:white"><u><center></center></u></h1> 
<div id="BG"></div> 

<form action = "update1.php" method = "GET"> 
    <table border = 0> 
    <tr> 
     <td>Image: <input type = "text" name = "image" id = "image"></td> 
      <br/> 
     <td>Hero Name: <input type = "text" name = "heroes" id = "heroes"></td> 
      <br/> 
     <td>Role: <input type = "text" name = "roles" id = "roles"></td> 
      <br/> 
     <td>Attribute: <input type = "text" name = "attribute" id = "attribute"></td> 
      <br/> 
     <td>Description: <input type = "text" name = "description" id = "description"></td> 
      <br/> 
     <td>General: <input type = "text" name = "general" id = "general"></td> 
      <br/> 
    </tr> 
    </table> 
    </br> 
    <input type = "submit" name="update" value = "Update"> 
</form> 
</center> 
</html> 



<?php 
ini_set('display_errors', 1); ini_set('display_startup_errors', 1);  error_reporting(E_ALL); 
define("DB_USER","*****"); 
define("DB_PASSWORD","****"); 
define("DB_HOST","*****"); 
define("DB_NAME","*****"); 

$dbc=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); 
if(isset($_GET['update'])) 
{ 

    $image = $_GET['image']; 
    $heroes = $_GET['heroes']; 
    $roles = $_GET['roles']; 
    $attribute = $_GET['attribute']; 
    $description = $_GET['description']; 
    $general = $_GET['general']; 
    $sql = "update `Dota 2 select` set (`image` = '$image',`heroes` = '$heroes') WHERE (heroes= '$heroes', image = '$image')"; 
    // $sql = "Update `Dota 2 select` SET (`image`= [$image]) = WHERE `image`)"; 

// $sql = "Update `Dota 2 select` SET (`image`= [$image],`heroes` =[$heroes],`roles` =[$roles],`attribute`=[$attribute],`description`=[$description],`general`=[$general]) = WHERE `heroes`='$heroes')"; 
// $sql = "Update `Dota 2 select` SET (`image`= [$image],`heroes`,`roles`,`attribute`,`description`,`general`) = WHERE (`image`,`heroes`,`roles`,`attribute`,`description`,`general`) =  ('$image','$heroes','$roles','$attribute','$description','$general')"; 

     if(!mysqli_query($dbc, $sql)) 
     { 
echo(mysqli_error($dbc)); 
     } 
     else 
     { 
      echo 'Data successfully updated!'; 
     } 
     mysqli_close($dbc); 
    } 


?> 

这是 此页“ 您的SQL语法错误的错误;检查对应于你的MySQL服务器版本使用附近“(image =‘正确的语法手册一’ ,heroes = 'A'),其中(英雄= 'A',图像= 'A')”位于第1行

+0

'A' ='这里了'。你的更新语法无效。 –

+0

表格名称的名称中不能有空格。 – Rahi

+0

@Rahi *“表名不能有名字空格” - 噢,为什么不呢?他们已经逃过了表名。 –

回答

0

似乎你得到的MySQL插入和更新语法混合..

UPDATE `table` set `col1`='val1', `col2`='val2',.... 

其中规定当作为PHP变种可能看起来像

$sql = 'UPDATE `table` set `col1`=\''. $val1.'\', `col2`=\''.$val2.'\',.... 

,而不是做的一个方法更多,但是这是我的首选方式。反列周围的列名称,并围绕值逃逸撇号,因为我在这里使用单引号字符串

+0

试过了,它不起作用$ sql =“更新Dota 2选择SET image ='$ image'WHERE image =”。$ image; – RBram

0

表名称具有无效的空间!

你可以设置你的指令为:

“更新Dota_2_select设置图像= $形象,英雄= $英雄英雄之= $英雄,图像= $形象”;

删除表名和之间的空间使用“,而不是”因为你可以$直接打电话去PHP变量

0

你的第一个where是错误的:

WHERE (heroes= '$heroes', image = '$image')"; 

应该

WHERE (heroes= '$heroes' AND image = '$image')"; 
         ^^^^ 

您也容易受到sql injection attacks

因为您测试您 $_GET价值的存在

你的第二个失败你使用它已经尝试过:

if (isset($_GET['Heroes'])) { 
    $Heroes = $_GET['heroes']; 
    ... 
}