php - MySQL/PHP UPDATE 查询不起作用

标签 php mysql database apache phpmyadmin

在问这个问题之前,我在很多地方(包括这里)寻找答案,但我拼命地问。

我有以下内容:

编辑.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;
  }
?>

这个东西(连同其他用于添加、删除和插入数据的文件)在这个配置下工作得很好:
  • Apache Web 服务器版本 2.2.4
  • PHP 脚本语言版本 5.2.3
  • MySQL 数据库版本 5.0.45
  • phpMyAdmin 数据库管理器版本 2.10.2

  • 现在,我无缘无故地决定升级到以下内容:
  • Apache Web 服务器版本 2.4.25
  • PHP 脚本语言版本 5.6.30 和 7.1.1
  • MySQL 数据库版本 5.7.17
  • phpMyAdmin 数据库管理器版本 4.6.6

  • 而且 BOOM!,只有 UPDATE 查询根本不起作用(其他脚本/查询工作得很好)。

    我尝试查看任何错误或警告,但它只是命中了 header 语句并重定向到 index.php,但没有执行 UPDATE 查询。

    我查看了从 5.2.3 到 5.3.10 的 PHP 版本日志更改,以防万一有什么问题,但没有运气。

    然后我尝试以下配置,结果与上面相同:
  • Apache Web 服务器版本 2.2.22
  • PHP 脚本语言版本 5.3.10
  • MySQL 数据库版本 5.5.54
  • phpMyAdmin 数据库管理器版本 3.4.10

  • 和这个:
  • Apache Web 服务器版本 2.4.25
  • PHP 脚本语言版本 5.6.30
  • MariaDB 10.1.21
  • phpMyAdmin 数据库管理器版本 4.6.5.2

  • 从所有这些来看,我认为 Apache、PMA 和 PHP 与我遇到的这个问题无关,所以到目前为止我的赢家是 MySQL。但...
  • 这里真正的问题应该是什么?
  • 我在查询中遗漏了什么吗?
  • 我可以在我的代码中尝试不同的东西吗?
  • 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/

    相关文章:

    mysql - 如何设计一个有很多用户并且每个用户都有多个联系人列表的数据库?

    php - 使用 Symfony Process 运行后台任务,无需等待进程完成

    PHP+MySQL :- Get title from parent ID row

    php - 检查 php 脚本是否仍在运行

    PHP MYSQL - 插入一条新记录,但需要另一个表中的 select 语句中的列 - 在 php 中失败

    php - 不确定循环时逻辑是否正确

    php - 如何连接多个表以根据给定条件获取结果?

    php - mysql 查询连接和速度/替代数据库解决方案是否必要?

    mysql - 优化MySQL查询

    sql - Oracle DB 中复杂的 SELECT 语句