-3

For some reason it won't update, instead it will add the new data. I am a beginner, and I have hard time trying to find out the error.

Read function was working very properly and insert works just fine as well. Its just the update that doesn't work properly or inserts instead of updating the query or data.

<?php include "db.php"; ?>
<?php include "functions.php" ?>
<?php
  if(isset($_POST['submit']))

  {$username = $_POST['username'];

      $password = $_POST['password'];

      $id = $_POST['id'];

      $querys = "UPDATE users SET ";

  $querys .= "username = '$username', ";

  $querys .= "password = '$password' ";

  $querys .= "WHERE id = $id ";


  $result = mysqli_query($connection, $querys);

  if(!$result)
      {
          die('Query FAILED'. mysqli_error($connection));
      }
  }

  ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" 
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css
      ">
  </head>
  <body>
    <div class="container">
      <div class="col-xs-6">
        <form action="login_create.php" method="post">
          <div class="form-group">
            <label for="username">Username</label>
            <input type="text" name="username" class="form-control">
          </div>
          <div class="form-group">
            <label for="password">Password</label>
            <input type="password" name="password" class="form-control">
          </div>
          <div class="form-group">
            <select name="id" id="">
            <?php
              showAllData();
              ?>
            </select>
          </div>
          <input class="btn btn-primary" 
            type="submit" name="submit" value="Update">
        </form>
      </div>
    </div>
  </body>
</html>

Thank you for the help.

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
  • 2
    "for some reason it won't update, instead it will add the new data." No. An update statement **only** updates an existing row. It does not insert. Also, this code suggests you are developing a very insecure website with multiple major security problems. –  Jan 04 '16 at 08:18
  • Then you must be calling the wrong script from your `
    ` tag. This looks quite likely as this form has `action="login_create.php"` maybe that should be `action="login_update.php"`
    – RiggsFolly Jan 04 '16 at 09:22
  • Thank you very much, it seemed like that problem but when i fixed that it ran to another problem. I dont know about how insecure the website is because I am just a beginner. But thank you very much for the heads up. – D. Maharjan Jan 04 '16 at 22:34

1 Answers1

-1

hi check this is a little example for update

if(isset($_POST["submit"])) {

$link=Conect(); 
$id=$_POST["id"];
$value1=$_POST["value1"];
$value2=$_POST["value2"];
$value3=$_POST["value3"];
$query = "UPDATE table_name SET field1='".$value1."',
 field2='".$value2."',
 "."field3='".$value3."' WHERE id='".$id."'";
$action =mysql_query($query,$link);

    $result=mysql_query($query,$link) or die("Error: ".mysql_error());
    echo "was update the code: ".$id;

}

and good luck

BlackHack123
  • 349
  • 1
  • 10
  • This appears to be a generic version of the update statement already shown in the question, and as such does not answer the question. Also, like the question, it is poor code that is extremely vulnerable to SQL injection attacks. –  Jan 05 '16 at 07:20