I have a form with some textbox that loads the data from the database. I have created also a button where I can update the data but currently it's not working and I don't have any clue why.
Here's my query:
<?php
include('connect.php');
if (isset($_POST['btnUpdate'])){
$query = "UPDATE users SET fname = '".$_POST['fname']."', lname = '".$_POST['lname']."' WHERE user_id = '".$_POST['id']."'";
$result = mysql_query($query);
if ($result) {
die("<strong>Updated</strong>");
} else {
die("<strong>Error ".mysql_error()."</strong>");
}
}
?>
Here's my form:
<form class="form-horizontal" role="form" action="" method="post">
<input type="hidden" name="id" value="<?php echo $row['user_id']; ?>"/>
<div class="form-group">
<label class="col-lg-3 control-label">First name:</label>
<div class="col-lg-8">
<input class="form-control" name="fname" type="text" value="<?php echo $row['fname'];?>">
</div>
<label class="col-lg-3 control-label">Last name:</label>
<div class="col-lg-8">
<input class="form-control" name="lname" type="text" value="<?php echo $row['lname'];?>">
</div>
<label class="col-md-3 control-label"></label>
<div class="col-md-8" >
<input type="button" name="btnUpdate" class="btn btn-primary" value="Save Changes">
<span></span>
<input type="reset" class="btn btn-default" value="Cancel">
</div>
</div>
</form>
I've tried everything but it won't update my database. It doesn't have any error whatsoever when I checked it on the browser. I'm running out of ideas. Anyone know what part of my code is wrong? I'm still a beginner to php and I cannot seem to understand what's wrong here. Any ideas how to solve this problem would be greatly appreciated. Thanks
Posting as a community wiki; I feel no rep should come of it.
<input type="button">
does not by default propagate POST. Therefore you either need to use a "submit" type:
Either <input type="submit">
or <button type="submit">
.
References:
type
The type of the button. Possible values are:
You're also open to a serious SQL injection if your site is live or intended to go live.
You should start looking into switching over to either the mysqli_
or PDO api and with a prepared statement.
References: