PHP MySQLi没有更新

时间:2016-02-27 02:50:08

标签: php mysqli

我一直在尝试创建一个表单,我可以更新两个字段,一个字段将是admin_welcomeboxadmin_author,我尝试按ID更新它,所以这里转到我的代码

<div class="col-lg-6">                      
    <div class="panel panel-color panel-inverse">                               
        <div class="panel-heading">
            <h3 class="panel-title">Welcome Box Update</h3>
        </div>

        <?php
            if(isset($_POST["submit"])){
                $servername = "localhost";
                $username = "trres";
                $password = "sss";
                $dbname = "txxxs";

                // Create connection
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                }

                $sql = "UPDATE admin_news SET welcomebox = '{$admin_news}' SET author = {$admin_author} id='{$id}'";

                if ($conn->query($sql) === TRUE) {
                    echo "<h4 class='bg-success'>You have updated admin welcome box.</h4>";
                } else {
                    echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
                }
                $conn->close();
            }
        ?>
        <div class="panel-body">
            <form method="post" action="">
                <div class="form-group">
                    <label for="welcomebox">Welcome Box</label>
                    <textarea type="text" name="welcomebox" id="welcomebox" placeholder="Enter Your Message" class="form-control"></textarea>
                </div>      
                <div class="form-group">
                    <label for="author">Author Name</label>
                    <input type="text" name="author" id="author" placeholder="Author Name" class="form-control" / >
                </div>                                          
                <div class="form-group text-right m-b-0">
                    <button class="btn btn-primary waves-effect waves-light" type="submit" name="submit" id="submit">
                        Update Info
                    </button>
                </div>
            </form>                                 
        </div>                                              
    </div>                              
</div>

当我尝试更新时,只需刷新页面。

2 个答案:

答案 0 :(得分:1)

您的查询语法无效。这是错的:

UPDATE admin_news SET welcomebox = '{$admin_news}' SET author = {$admin_author} id='{$id}'

UPDATE的正确MySQL语法是

UPDATE admin_news SET welcomebox = 'value', author = 'value' WHERE id='id'

MySQL manual

中的更多内容

此外,您在哪里定义$admin_news$admin_author$id?我在代码中没有看到任何变量定义。

答案 1 :(得分:1)

您也没有定义$admin_news$admin_author$id。首先定义。

试试这个code: -

<div class="col-lg-6">                      
<div class="panel panel-color panel-inverse">                               
    <div class="panel-heading">
        <h3 class="panel-title">Welcome Box Update</h3>
    </div>

    <?php
        if(isset($_POST["submit"])){
            $servername = "localhost";
            $username = "trres";
            $password = "sss";
            $dbname = "txxxs";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
            $id=$_POST['id'];
            $admin_news=$_POST['welcomebox']; 
            $admin_author=$_POST['author']; 

            $sql = "UPDATE admin_news SET welcomebox = '$admin_news', author = $admin_author where id=$id";

            if ($conn->query($sql) === TRUE) {
                echo "<h4 class='bg-success'>You have updated admin welcome box.</h4>";
            } else {
                echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
            }
            $conn->close();
        }
    ?>


    <div class="panel-body">
            <form method="post" action="">
  <input type="hidden" name="id" value="<?php echo $id; ?>" /> <!-- put here your id  --> 
               <div class="form-group">
                    <label for="welcomebox">Welcome Box</label>
                    <textarea type="text" name="welcomebox" id="welcomebox"                       placeholder="Enter Your Message" class="form-control"></textarea>
                </div>      
                <div class="form-group">
                    <label for="author">Author Name</label>
                    <input type="text" name="author" id="author" placeholder="Author Name" class="form-control" / >
                </div>                                          
                <div class="form-group text-right m-b-0">
                    <button class="btn btn-primary waves-effect waves-light" type="submit" name="submit" id="submit">
                        Update Info
                    </button>
                </div>
            </form>                                 
        </div>                                              
    </div>                              
</div>