简体   繁体   English

数据库表不会使用Mysqli更新

[英]Database Table Won't Update with Mysqli

So I have a problem with updating a mysql table via mysqli in php. 因此,我在php中通过mysqli更新mysql表时遇到问题。

The database connection and class: 数据库连接和类:

<?php
class testDB extends mysqli {
    private static $instance = null;

    private $user = "tester";
    private $pass = "tester01";
    private $dbName = "testdb";
    private $dbHost = "localhost";

 public static function getInstance() {
   if (!self::$instance instanceof self) {
     self::$instance = new self;
   }
   return self::$instance;
 }

 public function __clone() {
   trigger_error('Clone is not allowed.', E_USER_ERROR);
 }
 public function __wakeup() {
   trigger_error('Deserializing is not allowed.', E_USER_ERROR);
 }

private function __construct() {
    parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
    if (mysqli_connect_error()) {
        exit('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    }
    parent::set_charset('utf-8');
}

public function verify_credentials ($username, $password){
   $username = $this->real_escape_string($username);
   $password = $this->real_escape_string($password);
   $result = $this->query("SELECT 1 FROM users WHERE user_username = '" . $username . "' AND user_password = '" . $password . "'");
   return $result->data_seek(0);
}

public function get_vitae() {
    return $this->query("SELECT * FROM vitae");

public function update_vitae($text, $id) {
    $text = $this->real_escape_string($text);
    $this->query("UPDATE vitae SET vitae_text=".$text." WHERE vitae_id = ".$id);
}
}
?>

Here's the page code: 这是页面代码:

Above the header we check the login by making sure there is a session started; 在标题上方,我们通过确保已启动会话来检查登录名; then import the database class and the rest is called upon resubmitting the form to this same page: 然后导入数据库类,然后在将表单重新提交到同一页面时调用其余的类:

<?php
session_start();
if (!array_key_exists("username", $_SESSION)) {
    header('Location: index.php');
    exit;
}

require_once("includes/db.php");

$vitae_empty = false;

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if ($_POST['text'] == "") {
        $vitae_empty = true;
    } else if ($_POST["text"]!="") {
        testDB::getInstance()->update_vitae($_POST["text"], $_POST["id"]);
        header('Location: manage.php' );
        exit;
 } 
}

?>

In the body (the header and the rest of the html is imported via a 'require_once'): 在正文中(通过“ require_once”导入html的标头和其余部分):

    <section>
        <div class="grid_3 header_line"><h2>Update for CV</h2></div>
        <div class="grid_3">
            <?php
            $result = testDB::getInstance()->get_vitae();
            $vitae = mysqli_fetch_array($result);                
            ?>

            <form name="editvitae" action="editvitae.php" method="POST">

                <textarea name="text" rows="50" cols="100"><?php echo $vitae['vitae_text'];?></textarea><br/>
                <?php if ($vitae_empty) echo "Please enter some text.<br/>";?>

                <input type="hidden" name="id" value="<?php echo $vitae["vitae_id"];?>" /> <br/>
                <input type="submit" name="savevitae" value="Save Changes"/>
            </form>
        </div>
        <div class="grid_3">
            <p><a href="manage.php">&lsaquo; back to management consol</a></p>
        </div>
    </section>

After the 'body' tag: 在“ body”标签之后:

<?php mysql_free_result($result);?>

As you can see this pulls the 'vitae' text from the database then loops it to the same page with changes to update the table. 如您所见,这将从数据库中提取“简历”文本,然后将其循环到同一页面并进行更改以更新表。 It also check's to see that the 'text' box is not empty. 它还会检查“文本”框是否为空。

This code works in another application; 该代码可在另一个应用程序中使用。 I'm not understanding why it won't work here. 我不明白为什么它在这里不起作用。 AND before you start warning me about injection and security I have stripped most of it out trying to find the problem with the update. 并且在您开始警告我有关注入和安全的问题之前,我已经删除了其中的大部分内容,试图查找更新问题。 It WILL go back in once I can figure that out. 一旦我知道这一点,它就会返回。

I have tried stripping the text check; 我试过剥离文本检查; different variable names; 不同的变量名; dumping the post values into an array before updating the database; 在更新数据库之前将帖子值转储到数组中; putting the post values into static variables; 将发布值放入静态变量中; checking all my spellings etc... 检查我所有的拼写等...

I'm missing something and I feel like it's going to be simple. 我缺少了一些东西,我觉得这很简单。

Anytime an UPDATE is run through mysqli you need to run the $mysqli->commit(); $mysqli->commit();通过mysqli运行UPDATE您都需要运行$mysqli->commit(); method. 方法。

Your new update_vitae would be: 您的新update_vitae将是:

public function update_vitae($text, $id) {
    $text = $this->real_escape_string($text);
    $this->query("UPDATE vitae SET vitae_text=".$text." WHERE vitae_id = ".$id);
    $this->commit;
}

mysqli also has an autocommit feature that can be toggled on or off: mysqli还具有可启用或禁用的自动提交功能:

$this->autocommit(true); //on
$this->autocommit(false); //off

So the answer was indeed simple. 因此,答案确实很简单。 It was my escaping on the update string as Andrewsi suggested. 正如Andrewsi建议的那样,这是我对更新字符串的转义。 Here's the update that fixed it: 这是修复此问题的更新:

public function update_vitae($text, $id) {
    $text = $this->real_escape_string($text);
    $this->query("UPDATE vitae SET vitae_text = '$text' WHERE vitae_id = ".$id);
}

Thanks for the help! 谢谢您的帮助!

I've been designing websites for almost 10 years, but I'm just now getting into 'real' php coding instead of using prepared classes and dreamweaver's built in functions. 我设计网站已有近十年的时间,但是我现在正在进入“真正的” php编码,而不是使用准备好的类和Dreamweaver的内置函数。 Far to much to learn but it's fun in my limited spare time. 要学习的东西很多,但是在我有限的业余时间里这很有趣。

$result = $this->query("SELECT 1 FROM users WHERE user_username = '" . $username . "' AND user_password = '" . $password . "'");

Be careful with using logical ANDs for user authentication. 使用逻辑AND进行用户身份验证时要小心。 It may be more wise to completely validate the username first before going after any kind of password. 在使用任何类型的密码之前,先完全验证用户名可能更明智。 I say this in regard to the many examples of people inserting --; 我说的是关于人们插入-的许多示例。 WHERE 1=1 -- and stuff like that (not specifically this statement, however). WHERE 1 = 1-以及类似的内容(不过,不是专门针对此语句)。 Sure, this may require two queries, but at least you only have to process one piece of information to determine if a visitor is valid. 当然,这可能需要两个查询,但是至少您只需要处理一条信息即可确定访问者是否有效。 Another advantage might be saving processing because you won't have to deal with hashing/encrypting the user's password in your app or at the database (until the username has been verified). 另一个好处是可以节省处理时间,因为您不必在应用程序或数据库中处理哈希/加密用户密码(直到用户名已验证)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM