Hi guys i have downloaded a crud script and altered it to suit my needs.
one of the filelds was a text field and i have altered this to a date.
the original code was ,
Name
<?php echo $name_err;?>
I have altered this to,
Date
<?php echo $date_err;?>
when i submit the date though it returns an error please enter a valid date ? this also does the same thing on update script. Have i missed something or need to alter something.
Here are my codes,
create.php,
<?php // Include config file require_once "config.php"; // Define variables and initialize with empty values $date = $venue = $price = ""; $date_err = $venue_err = $price_err = ""; // Processing form data when form is submitted if($_SERVER["REQUEST_METHOD"] == "POST") { // Validate name $input_date = trim($_POST["date"]); if(empty($input_date)) { $date_err = "Please enter a date."; } elseif(!filter_var($input_date, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){ $date_err = "Please enter a valid date."; } else{ $date = $input_date; } // Validate address $input_venue = trim($_POST["venue"]); if(empty($input_venue)){ $venue_err = "Please enter an venue."; } else{ $venue = $input_venue; } // Validate salary $input_price = trim($_POST["price"]); if(empty($input_price)){ $price_err = "Please enter the price."; } elseif(!ctype_digit($input_price)){ $price_err = "Please enter a positive integer value."; } else{ $price = $input_price; } // Check input errors before inserting in database if(empty($date_err) && empty($date_err) && empty($price_err)){ // Prepare an insert statement $sql = "INSERT INTO gigs (date, venue, price) VALUES (?, ?, ?)"; if($stmt = mysqli_prepare($link, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "sss", $param_date, $param_venue, $param_price); // Set parameters $param_date = $date; $param_venue = $venue; $param_price = $price; // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ // Records created successfully. Redirect to landing page header("location: index.php"); exit(); } else{ echo "Something went wrong. Please try again later."; } } // Close statement mysqli_stmt_close($stmt); } // Close connection mysqli_close($link); } ?> Create Record .wrapper{ width: 500px; margin: 0 auto; }Create Record
Please fill this form and submit to add a gig record to the database.
" method="post">Date
<?php echo $date_err;?>
venue
<?php echo $venue; ?>
<?php echo $venue_err;?>
price
<?php echo $price_err;?>
Cancel
update.php,
<?php // Include config file require_once "config.php"; // Define variables and initialize with empty values $date = $venue = $price = ""; $date_err = $venue_err = $price_err = ""; // Processing form data when form is submitted if(isset($_POST["id"]) && !empty($_POST["id"])){ // Get hidden input value $id = $_POST["id"]; // Validate name $input_date = trim($_POST["date"]); if(empty($input_date)){ $date_err = "Please enter a date."; } elseif(!filter_var($input_date, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){ $date_err = "Please enter a valid date."; } else{ $date = $input_date; } // Validate address address $input_venue = trim($_POST["venue"]); if(empty($input_venue)){ $venue_err = "Please enter a venue."; } else{ $venue = $input_venue; } // Validate salary $input_venue = trim($_POST["venue"]); if(empty($input_price)){ $price_err = "Please enter the price."; } elseif(!ctype_digit($input_price)){ $price_err = "Please enter a positive integer value."; } else{ $price = $input_price; } // Check input errors before inserting in database if(empty($date_err) && empty($venue_err) && empty($price_err)){ // Prepare an update statement $sql = "UPDATE gigs SET date=?, venue=?, price=? WHERE id=?"; if($stmt = mysqli_prepare($link, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "sssi", $param_date, $param_venue, $param_price, $param_id); // Set parameters $param_date = $date; $param_venue = $venue; $param_price = $price; $param_id = $id; // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ // Records updated successfully. Redirect to landing page header("location: index.php"); exit(); } else{ echo "Something went wrong. Please try again later."; } } // Close statement mysqli_stmt_close($stmt); } // Close connection mysqli_close($link); } else{ // Check existence of id parameter before processing further if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){ // Get URL parameter $id = trim($_GET["id"]); // Prepare a select statement $sql = "SELECT * FROM gigs WHERE id = ?"; if($stmt = mysqli_prepare($link, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "i", $param_id); // Set parameters $param_id = $id; // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ $result = mysqli_stmt_get_result($stmt); if(mysqli_num_rows($result) == 1){ /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */ $row = mysqli_fetch_array($result, MYSQLI_ASSOC); // Retrieve individual field value $date = $row["date"]; $venue = $row["venue"]; $price = $row["price"]; } else{ // URL doesn't contain valid id. Redirect to error page header("location: error.php"); exit(); } } else{ echo "Oops! Something went wrong. Please try again later."; } } // Close statement mysqli_stmt_close($stmt); // Close connection mysqli_close($link); } else{ // URL doesn't contain id parameter. Redirect to error page header("location: error.php"); exit(); } } ?> Update Record .wrapper{ width: 500px; margin: 0 auto; }Update Record
Please edit the input values and submit to update the record.
Date
<?php echo $date_err;?>
Venue
<?php echo $venue; ?>
<?php echo $venue_err;?>
Price
<?php echo $price_err;?>
Cancel