[英]MySQL/PHP UPDATE query not working
在问这个问题之前,我在很多地方(包括这里)都在寻找答案,但是我非常想问。
我有以下几点:
edit.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查询根本不起作用(其他脚本/查询工作得很好)。
我尝试查找任何错误或警告,但是它只是命中了标头语句并重定向到index.php,但没有执行UPDATE查询。
我回顾了PHP版本日志从5.2.3更改为5.3.10的情况,以防万一,但是没有运气。
然后,我尝试以下配置,并获得与上面相同的结果:
和这个:
从所有这些方面,我认为Apache,PMA和PHP与我遇到的这个问题无关,所以到目前为止,我的赢家是MySQL。 但...
任何帮助,灯光,指南,视线,无论在这里将有什么帮助。
一如既往的感谢!
解决此问题的第一步是输出要发送到mysql对象的确切UPDATE查询。
我还建议从PHP MySQL切换到PHP MySQLi(较新): http : //php.net/manual/en/book.mysqli.php
我将如何解决此问题,是回显“ query:$ query”,然后将该确切查询粘贴到MySQL数据库浏览器(MySQL Workbench,Navicat,DataGrip等)中。
Database Explorer的Query Execution选项卡将告诉您查询的错误是什么(语法,不赞成使用的东西等)。
希望这可以帮助。
在尝试了mysql_error()
和其他东西之后,我设法使其工作了 (以某种方式)。
对于PHP> = 5.3(基于我测试过的所有设置),我只是更改了代码的顺序和样式,它的工作原理很吸引人。
这是更改前的edit.php (不适用于PHP> = 5.3):
<?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>
这是更改后的edit.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","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>";
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
mysql_connect("localhost","root","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");
}
?>
<br/>
</body>
</html>
但是后来我回到PHP <= 5.3(5.2.3),由于header()
函数,它崩溃了。 我使用原始文件进行了修复(快速修复)。
我知道我可以修改脚本,这样我就可以摆脱header()
问题,但这是后面的故事。
我根本不认为这是一个“解决方案”,但它可以很好地实现此目的。
不用担心,对于更严重的内容,我将至少更改为mysqli API或PDO
谢谢大家的旅程!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.