Php 没有更新我的数据库

Php isn't updating my database

我正在建立一个网站,其中包含要发布和更新的报告, 我有以下代码,在网上冲浪并检查解决方案后完全没有帮助。

选择参考号后,此页面从数据库中抓取内容并将其回显在文本区域中,供用户更新。 这是一个示例:

第二页:

<form method="post"  action="./../php/updated_preview_report.php">
ending:
<textarea id="endings" name="endings"  placeholder="ending" > <?php echo $endings; ?></textarea> 
<input type="submit" name="preview" value="ending" />
</form>

updated_preview_report.php页面:

<?php
include 'connectionfile.php' ;

$ref= mysql_real_escape_string($_POST['ref']);
$titl= mysql_real_escape_string($_POST['titles']);
$kind= $_POST['kindy'];
$subjec= mysql_real_escape_string($_POST['subjects']);
$caus= mysql_real_escape_string($_POST['causes']);
$solutio= mysql_real_escape_string($_POST['solutions']);
$penalt= mysql_real_escape_string($_POST['penaltys']);
$not= mysql_real_escape_string($_POST['notes']);
$endin= mysql_real_escape_string($_POST['endings']);
session_start();

$sql = "UPDATE report SET title = '$titl', type = '$kind', subject = '$subjec', cause = '$caus', solution = '$solutio', penalty = '$penalt' , note = '$not', ending = '$endin'  WHERE reference = $ref";

$result = mysqli_query($con, $sql);

?>

例如,当我回显任何更新值(例如 $title)时,它会显示更新值

请注意没有错误 reported/shown。

为什么这个查询没有更新我的数据库?

本人网络开发知识匮乏,请多多包涵,先谢谢了!

您需要对正在使用的变量进行转义,并使用 concat . 运算符将查询串在一起。

例如像

$str = "SELECT " . $var1 . " FROM " . $var2;

所以这个

$sql = "UPDATE report SET title = '$titl', type = '$kind', subject = '$subjec', cause = '$caus', solution = '$solutio', penalty = '$penalt' , note = '$not', ending = '$endin'  WHERE reference = $ref";

变成这样

$sql = "UPDATE report SET title = '" . $titl . "', type = '" . $kind . "', subject = '" . $subjec. "', cause = '" . $caus . "', solution = '" . $solutio . "', penalty = '" . $penalt . "' , note = '" . $not . "', ending = '" . $endin . "'  WHERE reference = '" . $ref . "'";

您没有收到错误,因为 UPDATE report SET title = '$titl', type = '$kind', subject = '$subjec', cause = '$caus', solution = '$solutio', penalty = '$penalt' , note = '$not', ending = '$endin' WHERE reference = $ref 是有效语法。

应该有错误,因为您同时使用了“mysql”和“mysqli”连接类型。无法说明您的连接文件中实现了哪种方法,因为未提供。只使用一种类型。如果问题没有解决,尝试添加

error_reporting(E_ALL);
ini_set('display_errors', '1'); 

到页面顶部。 (以上include 'connectionfile.php' ;

这将显示您的代码中发生的任何错误(如果它是隐藏的)。

您应该在列上添加反引号,因为其中一些是保留 MySQL 字。

 $sql = "UPDATE report SET 
                            `title` = $titl, 
                            `type` = $kind, 
                            `subject` = $subjec, 
                            `cause` = $caus, 
                            `solution` = $solutio, 
                            `penalty` = $penalt , 
                            `note` = $not, 
                            `ending` = $endin  
        WHERE `reference` = $ref";

在本页找到保留字:https://dev.mysql.com/doc/refman/5.0/en/keywords.html

首先你需要设置 isset 如果你要发帖,以确保你在发帖。

if (isset($_POST['endings'])

您不需要设置会话来更新数据库

session_start(); //You don't need to start session, don't see any reason

您应该添加以下代码来检查错误

error_reporting(E_ALL);
ini_set('display_errors', '1'); 

最后你的代码看起来像

<?php
    include 'connectionfile.php' ;
    error_reporting(E_ALL);
    ini_set('display_errors', '1'); 
    if (isset($_POST['endings']) {
            $ref= mysql_real_escape_string($_POST['ref']);
            $titl= mysql_real_escape_string($_POST['titles']);
            $kind= mysql_real_escape_string($_POST['kindy']);
            $subjec= mysql_real_escape_string($_POST['subjects']);
            $caus= mysql_real_escape_string($_POST['causes']);
            $solutio= mysql_real_escape_string($_POST['solutions']);
            $penalt= mysql_real_escape_string($_POST['penaltys']);
            $not= mysql_real_escape_string($_POST['notes']);
            $endin= mysql_real_escape_string($_POST['endings']);
            session_start(); //You don't need to start session, don't see any reason 
            $sql = "UPDATE report SET title = '$titl', type = '$kind', subject = '$subjec', cause = '$caus', solution = '$solutio', penalty = '$penalt' , note = '$not', ending = '$endin'  WHERE reference = '$ref'";
            $result = mysqli_query($con, $sql) or die(mysql_error());
    }
?>