php - How can I get my dropdown to goto a page and display a selected record? -
first time poster , new mysqli, sorry if question not correct. have table (staff) id, staffname , profile. wish able edit/add records selecting staff member dropdown , after submit, go new page called add-edit-staff.php.
i have created page add-edit-staff.php, , works perfectly. if put manually type "add-edit-staff.php" in address bar of browser form comes add new record. if manually type "add-edit-staff.php?id=1" in address bar of browser form comes edit record id 1.
now issue, have page called view-staff.php. page has form dropdown displaying field "staffmember". want able select record dropdown , go corresponding record on add-edit-staff.php page based on id of record. when select record, comes straight mt add-edit-staff.php page. because view-staff.php page not sending id of record in url.
i have attached code , appreciate advice on how resolve issue. said, new , have been trying adapt code suit goal cant seem head around it.
thanks in advance..
add-edit-staff.php
<?php include_once 'includes/db_connect.php'; include_once 'includes/functions.php'; sec_session_start(); ?> <?php // creates new/edit record form function renderform($staffmember = '', $profile ='', $error = '', $id = '') { ?> <form action="" method="post"> <div> <?php if ($id != '') { ?><input type="hidden" name="id" value="<?php echo $id; ?>" /> <?php $id; ?><?php } ?> <table width="675" border="0" cellspacing="10"> <tr> <td valign="top"><strong>staff member</strong></td> </tr> <tr> <td valign="top"><input name="staffmember" type="text" name="staffmember" size="147" value="<?php echo $staffmember; ?>"</td> </tr> <tr> <td><strong>staff profile</strong></td> </tr> <tr> <td><textarea name="profile" cols="110" rows="10"><?php echo $profile; ?></textarea></td> </tr> </table> <p></p> <input type="submit" name="submit" value="submit" /> </div> </form> </body></html> <?php } /* edit record */ // if 'id' variable set in url, know need edit record if (isset($_get['id'])) { // if form's submit button clicked, need process form if (isset($_post['submit'])) { // make sure 'id' in url valid if (is_numeric($_post['id'])) { // variables url/form $id = $_post['id']; $staffmember = htmlentities($_post['staffmember'], ent_quotes); $profile = htmlentities($_post['profile'], ent_quotes); // check staffmember , profile both not empty if ($staffmember == '' || $profile == '') { // if empty, show error message , display form $error = 'error: please fill in required fields!'; renderform($staffmember, $profile, $error, $id); } else { // if fine, update record in database if ($stmt = $mysqli->prepare("update staff set staffmember = ?, profile = ? id=?")) { $stmt->bind_param("ssi", $staffmember, $profile, $id); $stmt->execute(); $stmt->close(); } // show error message if query has error else { echo "error: not prepare sql statement."; } // redirect user once form updated header("location: view-staff.php"); } } // if 'id' variable not valid, show error message else { echo "error!"; } } // if form hasn't been submitted yet, info database , show form else { // make sure 'id' value valid if (is_numeric($_get['id']) && $_get['id'] > 0) { // 'id' url $id = $_get['id']; // recod database if($stmt = $mysqli->prepare("select * staff id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $staffmember, $profile); $stmt->fetch(); // show form renderform($staffmember, $profile, null, $id); $stmt->close(); } // show error if query has error else { echo "error: not prepare sql statement"; } } // if 'id' value not valid, redirect user view-staff2.php page else { header("location: view-staff.php"); } } } /* new record */ // if 'id' variable not set in url, must creating new record else { // if form's submit button clicked, need process form if (isset($_post['submit'])) { // form data $staffmember = htmlentities($_post['staffmember'], ent_quotes); $profile = htmlentities($_post['profile'], ent_quotes); // check staffmember , profile both not empty if ($staffmember == '' || $profile == '') { // if empty, show error message , display form $error = 'error: please fill in required fields!'; renderform($staffmember, $profile, $error); } else { // insert new record database if ($stmt = $mysqli->prepare("insert staff (staffmember, profile) values (?, ?)")) { $stmt->bind_param("ss", $staffmember, $profile); $stmt->execute(); $stmt->close(); } // show error if query has error else { echo "error: not prepare sql statement."; } // redirec user header("location: view-staff2.php"); } } // if form hasn't been submitted yet, show form else { renderform(); } } // close mysqli connection $mysqli->close(); ?>
view-staff.php
<form name="form1" method="post" action="add-edit-staff.php"> <select name="staffmember"> <?php // records database if ($result = $mysqli->query("select * staff order id")) { // display records if there records display if ($result->num_rows > 0) { { while ($row = $result->fetch_assoc()) { echo "<option value=\"{$row['id']}\">"; echo $row['staffmember']; echo "</option>"; } ?> <input type="submit" name="submit" value=" edit "> </form<?php } } // if there no records in database, display alert message else { echo "no results display!"; } } // show error if there issue database query else { echo "error: " . $mysqli->error; } // close database connection $mysqli->close(); ?> <a href="add-edit-staff.php"><h3 align="center">add new record</h3></a> <?php else : ?> <p> <span class="error">you not authorized access page.</span> please <a href="index.php">login</a>. </p> <?php endif; ?>
change view-staff.php this
<form name="form1" method="get" action="add-edit-staff.php"> <select name="id"> <?php // records database if ($result = $mysqli->query("select * staff order id")) { // display records if there records display if ($result->num_rows > 0) { { while ($row = $result->fetch_assoc()) { echo "<option value=\"{$row['id']}\">"; echo $row['staffmember']; echo "</option>"; } ?> <input type="submit" name="submit" value=" edit "> </form<?php } } // if there no records in database, display alert message else { echo "no results display!"; } } // show error if there issue database query else { echo "error: " . $mysqli->error; } // close database connection $mysqli->close(); ?> <a href="add-edit-staff.php"><h3 align="center">add new record</h3></a> <?php else : ?> <p> <span class="error">you not authorized access page.</span> please <a href="index.php">login</a>. </p> <?php endif; ?>
and add-edit-staff.php
<?php include_once 'includes/db_connect.php'; include_once 'includes/functions.php'; sec_session_start(); ?> <?php // creates new/edit record form function renderform($staffmember = '', $profile ='', $error = '', $id = '') { ?> <form action="" method="post"> <div> <?php if ($id != '') { ?><input type="hidden" name="id" value="<?php echo $id; ?>" /> <?php $id; ?><?php } ?> <table width="675" border="0" cellspacing="10"> <tr> <td valign="top"><strong>staff member</strong></td> </tr> <tr> <td valign="top"><input name="staffmember" type="text" name="staffmember" size="147" value="<?php echo $staffmember; ?>"</td> </tr> <tr> <td><strong>staff profile</strong></td> </tr> <tr> <td><textarea name="profile" cols="110" rows="10"><?php echo $profile; ?></textarea></td> </tr> </table> <p></p> <input type="submit" name="submit" value="submit" /> </div> </form> </body></html> <?php } /* edit record */ // if 'id' variable set in url, know need edit record if (isset($_get['id'])) { // if form's submit button clicked, need process form if (isset($_post['submit'])) { // make sure 'id' in url valid if (is_numeric($_post['id'])) { // variables url/form $id = $_post['id']; $staffmember = htmlentities($_post['staffmember'], ent_quotes); $profile = htmlentities($_post['profile'], ent_quotes); if ($stmt = $mysqli->prepare("update staff set staffmember = ?, profile = ? id=?")) { $stmt->bind_param("ssi", $staffmember, $profile, $id); $stmt->execute(); $stmt->close(); } // show error message if query has error else { echo "error: not prepare sql statement."; } // redirect user once form updated header("location: view-staff.php"); } // if 'id' variable not valid, show error message else { echo "error!"; } } // if form hasn't been submitted yet, info database , show form else { // make sure 'id' value valid if (is_numeric($_get['id']) && $_get['id'] > 0) { // 'id' url $id = $_get['id']; // recod database if($stmt = $mysqli->prepare("select * staff id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $staffmember, $profile); $stmt->fetch(); // show form renderform($staffmember, $profile, null, $id); $stmt->close(); } // show error if query has error else { echo "error: not prepare sql statement"; } } // if 'id' value not valid, redirect user view-staff2.php page else { header("location: view-staff.php"); } } } /* new record */ // if 'id' variable not set in url, must creating new record else { // if form's submit button clicked, need process form if (isset($_post['submit'])) { // form data $staffmember = htmlentities($_post['staffmember'], ent_quotes); $profile = htmlentities($_post['profile'], ent_quotes); // check staffmember , profile both not empty if ($staffmember == '' || $profile == '') { // if empty, show error message , display form $error = 'error: please fill in required fields!'; renderform($staffmember, $profile, $error); } else { // insert new record database if ($stmt = $mysqli->prepare("insert staff (staffmember, profile) values (?, ?)")) { $stmt->bind_param("ss", $staffmember, $profile); $stmt->execute(); $stmt->close(); } // show error if query has error else { echo "error: not prepare sql statement."; } // redirec user header("location: view-staff2.php"); } } // if form hasn't been submitted yet, show form else { renderform(); } } // close mysqli connection $mysqli->close(); ?>
Comments
Post a Comment