简体   繁体   中英

Updating database

I have a form where i am fetching data from database and now i want to update the database. The problem is when i update something in the form and press update button it says record updated successfully but database isn't getting updated. Here is my code

<?php

session_start();

    $host="localhost";
    $user="root";
    $pass="";
    $db="documentation";


    $conn=mysqli_connect($host,$user,$pass,$db);
    $off=$_GET['id'];
    $result = mysqli_query($conn,"SELECT * FROM article WHERE id='". $off ."'");

if (isset($_POST['submitt']))
{

    $f1 = $_POST['f1'];
    $f2 = $_POST['f2'];
    $f3 = $_POST['f3'];
    $f4 = $_POST['f4'];


    $sql= mysqli_query($conn, "UPDATE article SET sub_name='" . mysqli_real_escape_string($conn,$f2) . "', article_name='" . mysqli_real_escape_string($conn,$f3) . "', art_description='" . mysqli_real_escape_string($conn,$f4) . "', useremailll='". $_SESSION["user_mail"] ."', dateposted= NOW() WHERE id='" . mysqli_real_escape_string($conn,$f1) . "'");

    if ($sql) {
        echo "Record updated successfully";
        header('index.php');

    } else {
        echo "Error updating record: " . mysqli_error($conn);
    }


}



?>

<html>
<head>
    <title>Edit article</title>
    <link href="Content/cleditor/jquery.cleditor.css" rel="stylesheet" type="text/css" />
    <link href="Content/Site.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="css/mystyle.css">
    <script src="js/bootstrap.min.js"></script>
    <script src="Scripts/jquery-1.6.3.js" type="text/javascript"></script>
    <script src="Scripts/jquery.cleditor.js" type="text/javascript"></script>
    <script type="text/javascript">

    $(document).ready(function () {
        var options = {
            width: 1150,
            height: 200,
            controls: "bold italic underline strikethrough subscript superscript | font size " +
                    "style | color highlight removeformat | bullets numbering | outdent " +
                    "indent | alignleft center alignright justify | undo redo | " +
                    "rule link image unlink | cut copy paste pastetext | print source"
        };

        var editor = $("#editor").cleditor(options)[0];

        $("#btnClear").click(function (e) {
            e.preventDefault();
            editor.focus();
            editor.clear();
        });

        $("#btnAddImage").click(function () {
            editor.execCommand("insertimage", "http://images.free-extras.com/pics/s/smile-1620.JPG", null, null)
            editor.focus();
        });



    });
</script>
</head>
<body>
  <div class="container">
     <div class="border3">
      <div class="row">
      <div class="col-md-9">
      <h1><p class="margin">Edit Article</p></h1>
     <p class="border1">
              <p class="margin">by filling up the following form you can create a new sub-category</p>
          </p>
      </div>
  </div>
</div>
<p class="border1"></p>
<form role="form" method="POST">
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{?>

    <input name='f1' value='". $id ."' type='hidden'/>

    <p class="margin">
    <div class="form-group" style="width: 1150px">
      <label for="txt">Sub-Category Name:</label>
      <input type='text' name='f2' class="form-control" id="txt" value="<?php echo $row['sub_name'];?>">
    </div>
    </p>

    <p class="border1"></p>  
    <p class="margin">
    <div class="form-group" style="width: 1150px">
       <label for="txt">Article Name:</label>
      <input type='text' name='f3' class="form-control" id="txt" value="<?php echo $row['article_name'];?>">
    </div>
    </p>
    <p class="border1"></p>

<p class="margin">
<div style="width: 1200px">

    <div>
    <p><b>Description:</b></p>
        <textarea id="editor" name="f4" rows="30" cols="100"><?php echo $row['art_description'];?></textarea>
    </div>

<?php
    }; ?>
     <br/>
    <div class="row">
    <div class="col-md-11">
    <div class="normaldiv" style="float: right">
      <button type="button" class="btn btn-primary" id="btnClear">Clear</button>
      <input class="btn btn-primary" type="submit" name="submitt" value="Update">
   </div>
</div>
</form>


</div>
</body>
</html>

I think you have mistake at passing id

Try editing this line

<input name='f1' value='". $id ."' type='hidden'/>

to

 <input name='f1' value='".$row['id']."' type='hidden'/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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