MySQL不会使用URL变量进行更新

时间:2013-03-25 16:07:31

标签: php mysql

我遇到麻烦让我的代码正常工作。如果我输入phpMyAdmin它可以工作,但是当我在代码中尝试它时,它不会更新数据库。

<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DATABASE", $con);

$sp=mysql_real_escape_string($_GET['file']);

$query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'"; 

mysql_close($con);

?>

3 个答案:

答案 0 :(得分:1)

试试这段代码片段,看看你是如何继续下去的。

<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con) {
  die('Could not connect: ' . mysql_error());
} else {
    mysql_select_db("DATABASE", $con);
    $sp=mysql_real_escape_string($_GET['file']);
    $query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'"; 
    $result = mysql_query($query);
    mysql_close($con);
}
?>

我建议这样做,因为PHP不再支持mysql。

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");

if (!$mysqli) {
  die('Could not connect: ' . $mysqli->connect_error);
} else {
    $sp = $mysqli->real_escape_string($_GET['file']);
    $query = "UPDATE TDB SET WEIGHT=100000 WHERE PATH='$sp'"; 
    $mysqli->query(query);
    $mysqli->close();
}
?>

答案 1 :(得分:0)

您没有执行查询。您只是定义了恰好包含某些SQL的字符串,例如

$sql = "blah blah blah";
$result = mysql_query($sql) or die(mysql_error()); <--forgot this

答案 2 :(得分:0)

<?php
$con = mysql_connect("SERVER","USER","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DATABASE", $con);

$sp=mysql_real_escape_string($_GET['file']);

$sql = "UPDATE TRACKDB SET WEIGHT=100000 WHERE PATH='$sp'"; 

$result = mysql_query($sql) or die(mysql_error());

mysql_close($con);

?>