html - Edit Records PHP Form -
i have these php pages lets me add together record database. (in case members) works in sense can add, delete , view. im not sure how edit(or update functionality working.
here db connection code:
<?php // server info $server = 'localhost'; $username = 'root'; $password = ''; $database = 'gamgam'; // connect database $connection = new mysqli($server, $username, $password, $database); ?>
here add together code:
<!doctype html> <html> <head><title>insert users</title></head> <body> <h2>insert user confirmation</h2> <form action="<?php $_server['php_self']?>" method="post"/> <br> <?php require_once('connection.php'); echo "<label for='memberid' >member id:</label>"; echo "<input type='text' name='memberid' id='memberid' />"; echo "<br /><br />"; echo "<label for='username' >username:</label>"; echo "<input type='text' name='username' id='username' />"; echo "<br /><br />"; echo "<label for='password' >password:</label>"; echo "<input type='password' name='password' id='password' />"; echo "<br /><br />"; echo "<label for='fname' >firstname:</label>"; echo "<input type='text' name='fname' id='fname' />"; echo "<br /><br />"; echo "<label for='lname' >lastname:</label>"; echo "<input type='text' name='lname' id='lname' />"; echo "<br /><br />"; echo "<label for='address' >address:</label>"; echo "<input type='text' name='address' id='address' />"; echo "<br /><br />"; echo "<label for='email' >email:</label>"; echo "<input type='text' name='email' id='email' />"; echo "<br /><br />"; echo "<input type='submit' name='submit' value='submit' />"; echo "<input type='reset' value='clear' />"; echo "<br /><br />"; ?> </form> </section> <p><a href='login.php'>login</a></p> <?php if(!isset($_post['submit'])) { echo 'please register'; } else { $memberid = $_post['memberid']; $username = $_post['username']; $password = $_post['password']; $fname = $_post['fname']; $lname = $_post['lname']; $address = $_post['address']; $email = $_post['email']; $query = "insert `members` (memberid, username, password, firstname, lastname, streetaddress, email) values ('$memberid', '$username', '$password', '$fname', '$lname', '$address', '$email')"; mysqli_query($connection, $query) or die(mysqli_error($connection)); $rc = mysqli_affected_rows($connection); if ($rc==1) { echo '<h4>the database has been updated next details: </h4> '; echo 'memberid: '.$memberid.'<br />'; echo 'username: '.$username.'<br />'; echo 'password: '.$password.'<br />'; echo 'first name: '.$fname.'<br />'; echo 'last name: '.$lname.'<br />'; echo 'address: '.$address.'<br />'; echo 'email: '.$email.'<br />'; } else { echo '<p>the info not entered database time.</p>'; } } ?> </body> </html>
here view code:
<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <title>view records</title> </head> <body> <table border="1" style="width:100%" > <?php /* view.php displays info 'players' table */ // connect database include('connection.php'); // results database $result = mysqli_query($connection, "select * members") or die(mysqli_error()); // loop through results of database query, displaying them in table while($row = mysqli_fetch_array( $result )) { // echo out contents of each row table echo "<tr>"; echo '<td>' . $row['memberid'] . '</td>'; echo '<td>' . $row['username'] . '</td>'; echo '<td>' . $row['password'] . '</td>'; echo '<td>' . $row['firstname'] . '</td>'; echo '<td>' . $row['streetaddress'] . '</td>'; echo '<td>' . $row['email'] . '</td>'; echo '<td><a href="edit.php?memberid=' . $row['memberid'] . '">edit</a></td>'; echo '<td><a href="delete.php?memberid=' . $row['memberid'] . '">delete</a></td>'; echo "</tr>"; } // close table> echo "</table>"; ?> <p><a href="insert_user.php">add new record</a></p> </body> </html>
and here delete code:
<?php // connect database include('connection.php'); // confirm 'code' variable has been set if (isset($_get['memberid'])) { // 'memberid' variable url $memberid = $_get['memberid']; // delete record database if ($stmt = $connection->prepare("delete members memberid = ? limit 1")) { $stmt->bind_param("i",$memberid); $stmt->execute(); $stmt->close(); } else { echo "error: not prepare sql statement."; } $connection->close(); // redirect user after delete successful header("location: view.php"); } else { // if 'code' variable isn't set, redirect user header("location: view.php"); } ?>
i have gone through many basic php form templates online trying incorporate have done accomplish results have not had success. code needs written website have functionality edit records created in database without going through phpmyadmin. help apreciated.
edit
just add
, need read record first , populate field values.
start code add
, like:
<?php $memberid = (int) $_get['memberid']; ?> <form action="<?php $_server['php_self']?>" method="post"/> <br> <input type="hidden" name="memberid" value="<?php echo $memberid; ?>" <?php require_once('connection.php'); $result = mysqli_query($connection, "select * members memberid = $memberid") or die(mysqli_error()); // loop through results of database query, displaying them in table $row = mysqli_fetch_assoc($result); extract($row); echo "<label for='memberid' >member id:</label>"; echo "$memberid"; // fellow member id should not editable echo "<br /><br />"; echo "<label for='username' >username:</label>"; echo "<input type='text' name='username' id='username' value="$username" />"; echo "<br /><br />";
the php code have query like
`update `members` set `username` = '$username' ... `memberid` = '$memberid'"
php html mysql sql
No comments:
Post a Comment