在问这个问题之前,我在很多地方(包括这里)寻找答案,但我拼命地问。
我有以下内容:
编辑.php
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
include("lib/functions.php");
mysql_connect("localhost","root","") or die(mysql_error()); //Connect to server
mysql_select_db("proyectoinmob") or die("Cannot connect to the selected database"); //Connect to database
$nombreCalle = mysql_escape_string($_POST["nombreCalle"]);
$numeroCasa = mysql_escape_string($_POST["numeroCasa"]);
$precio = mysql_escape_string($_POST["precio"]);
$sql = "UPDATE propiedades SET nombreCalle='$nombreCalle', numeroCasa='$numeroCasa', precio='$precio' WHERE id='$id'";
executesql($sql);
header("location: index.php");
}
?>
<html>
<head>
<title>Proyecto Inmobiliaria - Editar</title>
</head>
<body>
<h2>Editar Propiedad</h2>
<?php
if(!empty($_GET["id"]))
{
$id = $_GET["id"];
$_SESSION["id"] = $id;
include("lib/functions.php");
mysql_connect("localhost","root","") or die(mysql_error()); //Connect to server
mysql_select_db("proyectoinmob") or die("Cannot connect to the selected database"); //connect to database
$sql = "SELECT * FROM propiedades WHERE id='$id'";
$output = executesql($sql);
$row = mysql_fetch_array($output);
Print "<form class='' action='' method='POST'>";
Print "<div class=''>";
Print "<label>Nombre Calle</label>";
Print "<input type='text' name='nombreCalle' value='".$row["nombreCalle"]."' placeholder='ej: Antonio del Viso'>";
Print "</div>";
Print "<div class=''>";
Print "<label for=''>Número de Casa</label>";
Print "<input type='text' name='numeroCasa' value='".$row["numeroCasa"]."' placeholder='ej: 440'>";
Print "</div>";
Print "<div class=''>";
Print "<label for=''>Precio</label>";
Print "<input type='text' name='precio' value='".$row["precio"]."' placeholder='ej: 150000'>";
Print "</div>";
Print "<button type='submit' name='add'>Guardar</button>";
Print "<a href='index.php' style='display:inline-block;border:2px solid #000;border-radius:25%;padding:10px;margin:10px;'>Regresar</a>";
Print "</form>";
}
?>
<br/>
</body>
</html>
lib/functions.php
<?php
function executesql($sql) {
$test = substr($sql,0,6);
if($test == "INSERT" || $test == "insert" || $test == "Insert") {
return mysql_query($sql);
} elseif ($test == "UPDATE" || $test == "update" || $test == "Update") {
return mysql_query($sql);
} elseif ($test == "SELECT" || $test == "select" || $test == "Select") {
return mysql_query($sql);
} elseif ($test == "DELETE" || $test == "delete" || $test == "Delete") {
return mysql_query($sql);
} else {
$var = "echo '<script>alert('This is not a valid query')</script>'";
}
return $var;
}
?>
这个东西(连同其他用于添加、删除和插入数据的文件)在这个配置下工作得很好:
现在,我无缘无故地决定升级到以下内容:
而且 BOOM!,只有 UPDATE 查询根本不起作用(其他脚本/查询工作得很好)。
我尝试查看任何错误或警告,但它只是命中了 header 语句并重定向到 index.php,但没有执行 UPDATE 查询。
我查看了从 5.2.3 到 5.3.10 的 PHP 版本日志更改,以防万一有什么问题,但没有运气。
然后我尝试以下配置,结果与上面相同:
和这个:
从所有这些来看,我认为 Apache、PMA 和 PHP 与我遇到的这个问题无关,所以到目前为止我的赢家是 MySQL。但...
任何帮助,光,向导,视线,任何在这里都会有帮助的东西。
非常感谢一如既往!
最佳答案
解决此问题的第一步是将您发送到 mysql 对象的确切 UPDATE 查询输出。
我还建议从 PHP MySQL 切换到 PHP MySQLi(更新):http://php.net/manual/en/book.mysqli.php
我将如何解决这个问题是 echo "query: $query",然后将确切的查询粘贴到 MySQL 数据库资源管理器(MySQL Workbench、Navicat、DataGrip 等)中。
数据库资源管理器的查询执行选项卡将告诉您查询的错误是什么(是语法、不推荐使用的东西等)
希望这可以帮助。
关于php - MySQL/PHP UPDATE 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42881495/