mysql - All fields in database updated at once using PHP -
i have form comes link in table should update 1 record in database. when changed details in table , pressed submit button changed of fields in database , not 1 wanted change. below form code , table being edited.
edit user code
<?php // since form used multiple times in file, have made function reusable function renderform($userid, $username, $password, $telephone, $address1, $town, $postcode, $forename, $surname, $email, $error) { ?> <!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <title>edit user</title> </head> <body> <?php // if there errors, display them if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } ?> <form action="" method="post"> <input type="hidden" name="userid" value="<?php echo $userid; ?>"/> <div> <p><strong>id:</strong> <?php echo $userid; ?></p> <strong>username: </strong> <input type="text" name="username" value="<?php echo $username; ?>"/><br/> <strong>password: </strong> <input type="text" name="password" value="<?php echo $password; ?>"/><br/> <strong>telephone: </strong> <input type="text" name="telephone" value="<?php echo $telephone; ?>"/><br/> <strong>address: </strong> <input type="text" name="address1" value="<?php echo $address1; ?>"/><br/> <strong>town: </strong> <input type="text" name="town" value="<?php echo $town; ?>"/><br/> <strong>postcode: </strong> <input type="text" name="postcode" value="<?php echo $postcode; ?>"/><br/> <strong>forename: </strong> <input type="text" name="forename" value="<?php echo $forename; ?>"/><br/> <strong>surname: </strong> <input type="text" name="surname" value="<?php echo $surname; ?>"/><br/> <strong>email: </strong> <input type="text" name="email" value="<?php echo $email; ?>"/><br/> <input type="submit" name="submit" value="edit details"> </div> </form> </body> </html> <?php } // connect database include "config.php"; // check if form has been submitted. if has, process form , save database if (isset($_post['submit'])) { // confirm 'id' value valid integer before getting form data if (is_numeric($_post['userid'])) { // form data, making sure valid $userid = $_post['userid']; $username = $_post['username']; $password = $_post['password']; $telephone = $_post['telephone']; $address1 = $_post['address1']; $town = $_post['town']; $postcode = $_post['postcode']; $forename = $_post['forename']; $surname = $_post['surname']; $email = $_post['email']; // check firstname/lastname fields both filled in if ($username == '' || $password == '' || $telephone == '' || $address1 == '' || $town == '' || $postcode == '' || $forename == '' || $surname == '' || $email == '' ) { // generate error message $error = 'error: please fill in required fields!'; //error, display form renderform($userid, $username, $password, $telephone, $address1, $town, $postcode, $forename, $surname, $email, $error); } else { // save data database $query = $db->prepare("update user set username='$username', password='$password', telephone='$telephone', address1='$address1', town='$town', postcode='$postcode', forename='$forename', surname='$surname', email='$email' "); $query->execute(); // once saved, redirect view page header("location: view_user.php"); } } else { // if 'id' isn't valid, display error echo 'error!'; } } else // if form hasn't been submitted, data db , display form { // 'id' value url (if exists), making sure valid (checing numeric/larger 0) if (isset($_get['userid']) && is_numeric($_get['userid']) && $_get['userid'] > 0) { // query db $userid = $_get['userid']; $query = $db->prepare("select * user userid=$userid"); $query->execute(); $dbrow = $query->fetch(pdo::fetch_assoc); // check 'id' matches row in databse if($dbrow) { // data db $username = $dbrow['username']; $password = $dbrow['password']; $telephone = $dbrow['telephone']; $address1 = $dbrow['address1']; $town = $dbrow['town']; $postcode = $dbrow['postcode']; $forename = $dbrow['forename']; $surname = $dbrow['surname']; $email = $dbrow['email']; // show form renderform($userid, $username, $password, $telephone, $address1, $town, $postcode, $forename, $surname, $email, ''); } else // if no match, display result { echo "no results!"; } } else // if 'id' in url isn't valid, or if there no 'id' value, display error { echo 'error!'; } } ?>
view user info code
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="../../favicon.ico"> <title>ballymena sports</title> <!-- bootstrap core css --> <link href="bootstrap.min.css" rel="stylesheet"> <!-- custom styles template --> <link href="home2.css" rel="stylesheet"> <!-- html5 shim , respond.js ie8 support of html5 elements , media queries --> <!--[if lt ie 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="home2_template.html">ballymena sports</a> </div> <ul class="nav navbar-nav navbar-right"> <li><a href="admin_login.php">administrator</a></li> <li><a href="logout.php">log out</a></li> </ul> </div> </nav> <!-- main part of homepage --> <div class="jumbotron"> <div class="container"> <h2>users</h2> <p>this table shows registered users of ballymena sports:</p> <div class="table-responsive"> <tbody> <?php include "config.php"; $query = $db->prepare("select * user order userid asc"); $query->execute(); echo "<table id='user' class='table table-bordered'> <tr> <th>user id</th> <th>username</th> <th>forename</th> <th>surname</th> <th>email</th> <th>address</th> <th>town</th> <th>postcode</th> <th>edit user</th> <th>delete user</th> </tr>"; while ($dbrow = $query->fetch(pdo::fetch_assoc)) { $userid = $dbrow['userid']; $username = $dbrow['username']; $forename = $dbrow['forename']; $surname = $dbrow['surname']; $email = $dbrow['email']; $address1 = $dbrow['address1']; $town = $dbrow['town']; $postcode = $dbrow['postcode']; // code display information { echo "<tr> <td>$userid</td> <td>$username</td> <td>$forename</td> <td>$surname</td> <td>$email</td> <td>$address1</td> <td>$town</td> <td>$postcode</td> <td><a href='edit_user.php?userid=".$userid."'>edit</a></td> <td><a href='delete_user.php?userid=".$userid."'>delete</a></td> </tr>";} } //while ?> </tbody> </div> </table> </div> </div> <?php if(!$_session['admin_username']){ header('location:admin_login.php'); $name = $_session['admin_username']; } ?> <hr> <!-- bootstrap core javascript ================================================== --> <!-- placed @ end of document pages load faster --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="../../dist/js/bootstrap.min.js"></script> <!-- ie10 viewport hack surface/desktop windows 8 bug --> <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> <!-- header , footer later used include statements --> </body> </html>
your problem update
statement doesn't specify where
clause:
$query = $db->prepare("update user set username='$username', password='$password', telephone='$telephone', address1='$address1', town='$town', postcode='$postcode', forename='$forename', surname='$surname', email='$email' ");
you need use user id specify want update row particular user:
$query = $db->prepare("update user set username='$username', password='$password', telephone='$telephone', address1='$address1', town='$town', postcode='$postcode', forename='$forename', surname='$surname', email='$email' userid=$userid");
you should using prepared statements guard code sql injection attacks.
Comments
Post a Comment