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;?>

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;?>

    You want to try that again? Put your code between [‍code=php]...[/code] tags so that it retains its formatting and remains legible.

    But I think the relevant part, based on looking to see where in your code your error message is written, might be
    (!filter_var($input_date, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){ $date_err = "Please enter a valid date."; }
    or however that's really formatted. It doesn't allow hyphens or commas, so the user would get an error if they wrote "third of June, Nineteen eighty-four".

    (You might want to consider providing three fields for the date so that you don't have to guess what format your users are using to separate years, months and days.)

    How are you doing with your other problems?

      thanks weedpacket sorry new to all this i have managed to sort it and my other stuff. literally spent hours and hours doing stuff altering bits here and there its frustrating when i can ask someone for advice and probably sort it instantly but that way i will not learn. I really appreciate people like yourself who take time out to be patient with me i can assure you its not underestimated. Thanks

        Write a Reply...