exceedinglife Posted January 31, 2019 Share Posted January 31, 2019 Hey all, I have a CRUD php project I am working on. I have insert working reading and deleteing. The only one I have left is update. My page submits the form but when the btn is clicked my page refreshes and does not update in the db. All the controls are cleared. Im assuming I’m missing something simple or just have a small mistake that is running past me, <?php error_reporting(E_ALL & ~E_NOTICE); // include config for Database require_once "php/config.php"; // Declare the variables that will be used. $name = $language = $datenow = ""; $nameerror = $langerror = $dateerror = ""; // Process form data when its submitted if(isset($_POST["id"]) && !empty($_POST["id"])) { // Get hidden input value. $id = $_POST["id"]; $input_name = trim($_POST["name"]); if(empty($input_name)) { $nameerror = "Name is required. "; } elseif (!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))) { $nameerror = "Please enter a valid name. "; } else { //$namesafe = mysqli_real_escape_string($connection, $input_name); $name = $input_name; } // Validate language entered $input_lang = trim($_POST["language"]); if(empty($input_lang)) { $langerror = "Please enter a language. "; } else { $language = $input_lang; } // Get current Date and Time $currentDate = date("Y-m-d H:i:s"); // OR $datetimeobj = new DateTime(); $datetimeobj->format("Y-m-d H:i:s"); //if(empty($nameerror) && empty($langerror) && empty($dateerror)) { if($name != "" && $language != "") { $sql = "UPDATE users SET name=:name, language=:language, " . "date=:date WHERE id=:id"; error_log($sql); if($stmt = $pdoConnect->prepare($sql)) { $stmt->bindParam(":name", $param_name); $stmt->bindParam(":language", $param_lang); $stmt->bindParam(":date", $param_date); $stmt->bindParam(":id", $param_id); $para_name = $name; $param_lang = $lang; $param_date = $date; $param_id = $id; if($stmt->execute()) { header("location: index.php"); exit(); } else { echo "Something went wrong, try again later."; } } } else { $errormsg = '<div > 'All fields are required to continue</div>'; } unset($stmt); unset($pdoConnect); } else { if(isset($_GET["id"]) && !empty(trim($_GET["id"]))) { $id = trim($_GET["id"]); $sql = "SELECT * FROM users WHERE id = :id"; if($stmt = $pdoConnect->prepare($sql)) { $stmt->bindParam(":id", $param_id); $param_id = $id; if($stmt->execute()) { if($stmt->rowCount() == 1) { $row = $stmt->fetch(PDO::FETCH_ASSOC); //$id = $row["id"]; $name = $row["name"]; $language = $row["language"]; $userdate = $row["date"]; } else { header("location: error.php"); exit(); } } else { echo "Something went wrong with UPDATE, try again later."; } } // Close $stmt statement unset($stmt); // Close connection unset($pdoConnect); } else { // URL doesn't contain valid 'id' parameter. header("location: error.php"); exit(); } } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted January 31, 2019 Share Posted January 31, 2019 Any error messages? What's your form? Quote Link to comment Share on other sites More sharing options...
exceedinglife Posted January 31, 2019 Author Share Posted January 31, 2019 No error messages. My form submits and clears the data when its valid but the data isnt updated Form is <form action="<?php echo htmlspecialchars(basename($_SERVER["REQUEST_URI"])); ?>" method="post" class="rounded" id="formUpdate"> <div class="form-group row"> <label for="txtid" class="col-sm-3"><b>Id:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtid" name="txtid" disabled value="<?php echo $row["id"]; ?>" /> </div> </div> <div class="form-group row"> <label for="txtname" class="col-sm-3"><b>Name:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtname" name="txtname" value="<?php echo $row["name"]; ?>" /> </div> </div> <?php if(isset($nameerror)) { echo '<span id="error"><b>' . $nameerror . '</b></span>'; } ?> <div class="form-group row"> <label for="txtlang" class="col-sm-3"><b>Language:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtlang" name="txtlang" value="<?php echo $row["language"]; ?>" /> </div> </div> <?php if(isset($langerror)) { echo '<span id="error"><b>' . $langerror . '</b></span>'; } ?> <div class="form-group row"> <label for="txtdate" class="col-sm-3"><b>Date:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtdate" name="txtdate" disabled value="<?php echo $row["date"]; ?>" /> </div> </div> <?php if(isset($dateerror)) { echo '<span id="error"><b>' . $dateerror . '</b></span>'; } ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="submit" class="btn btn-lg btn-primary btn-block" name="submit" value="Update"/> <a href="index.php" class="btn btn-lg btn-danger btn-block" role="button">Cancel</a> </form> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 31, 2019 Share Posted January 31, 2019 (edited) the posted form processing code contains a php syntax error, as indicated by the color highlighting stopping. there's no way it is running. only one one of the form field names matches between the php code and the form, so even if the form processing code runs, it is not operating on the correct field names. at a minimum, you need to set php's error_reporting to E_ALL and set it in the php.ini on your system. you can put the setting into your code but since you code never runs when there is a php syntax error, you won't see the error unless the setting is set in the php.ini. Edited January 31, 2019 by mac_gyver Quote Link to comment Share on other sites More sharing options...
exceedinglife Posted January 31, 2019 Author Share Posted January 31, 2019 Ok I will try that with php.ini One of the errors I found was para_id and it was suppose to be param_id Another issue was $param_date = $date; and it suppose to be $param_date = $currentDate; I changed my button <input ...> to <button></button> My page just continues to load Quote Link to comment Share on other sites More sharing options...
exceedinglife Posted January 31, 2019 Author Share Posted January 31, 2019 looks like i set this up awhile back ; Common Values: ; E_ALL (Show all errors, warnings and notices including coding standards.) ; E_ALL & ~E_NOTICE (Show all errors, except for notices) ; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.) ; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors) ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED ; Development Value: E_ALL ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT ; http://php.net/error-reporting error_reporting=E_ALL Quote Link to comment Share on other sites More sharing options...
exceedinglife Posted January 31, 2019 Author Share Posted January 31, 2019 My page continuely just keeps spinning on my btn click. In the network tab it opens a new tab (my php) and it has a yellow circile for google chrome - Provisional headers are shown In my form the correct data is shown - in form data. So its getting the correct values. idk if this is anything. General Request URL: http://localhost/php_crud/update.php?id=13 Referrer Policy: no-referrer-when-downgrade Quote Link to comment Share on other sites More sharing options...
requinix Posted February 1, 2019 Share Posted February 1, 2019 You have various error message variables in there. Make sure they show up in your form and you might get a clue as to what's wrong. Also use error_reporting=-1 and you might get more clues. Even better clues, in fact. But if the page isn't even loading at all now then you have some other problem. I don't see any loops so it's not like the script is stuck in one. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.