Jump to content

Benny

New Members
  • Posts

    1
  • Joined

  • Last visited

Everything posted by Benny

  1. Hi, I am using your code on my site but I have different column names... I changed these thinking I could find a replace the names in the records.php but when i go to the records.php my value names do not echo the data... why is this? I cannot get my head road why the values do not work... I need to add another 10 columns to the records.php so what would I need to amend on the records sheet for this to work? You help would be much appreciated. Regards Benny <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include("connect-db.php"); // creates the new/edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($Site = '', $bloc ='', $error = '', $id = '') { ?> <body> <div class="container theme-showcase"> <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1> <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> <form action="" method="post"> <div> <?php if ($id != '') { ?> <input type="text" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <strong>Site Ref: *</strong> <input type="text" name="SiteRef" value="<?php echo $Site; ?>"/> <strong>Block: *</strong> <input type="text" name="block" value="<?php echo $bloc; ?>"/> <p>* required</p> <input type="submit" name="submit" value="Submit" /> </div> </form> </div> <?php include"footer.php"?> <?php } /* EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $SiteRef = htmlentities($_POST['SiteRef'], ENT_QUOTES); $block = htmlentities($_POST['block'], ENT_QUOTES); // check that SiteRef and block are both not empty if ($SiteRef == '' || $block == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($SiteRef, $block, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE FireRiskAssessment SET SiteRef = ?, block = ? WHERE id=?")) { $stmt->bind_param("ssi", $SiteRef, $block, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: view.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the record from the database if($stmt = $mysqli->prepare("SELECT * FROM FireRiskAssessment WHERE id=?")) { $stmt->bind_param("i", $id, $SiteRef, $block); $stmt->execute(); $stmt->bind_result($id, $SiteRef, $block); $stmt->fetch(); // show the form renderForm($SiteRef, $block, NULL, $id); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: view.php"); } } } /* NEW RECORD */ // if the 'id' variable is not set in the URL, we must be creating a new record else { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // get the form data $SiteRef = htmlentities($_POST['SiteRef'], ENT_QUOTES); $block = htmlentities($_POST['block'], ENT_QUOTES); // check that SiteRef and block are both not empty if ($SiteRef == '' || $block == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($SiteRef, $block, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT FireRiskAssessment (SiteRef, block) VALUES (?, ?)")) { $stmt->bind_param("ss", $SiteRef, $block); $stmt->execute(); $stmt->close(); } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } // redirec the user header("Location: view.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?>
×
×
  • Create New...