Practical PHP and MySQLBuilding Eight Dynamic Web Applications phần 6

Sau khi bạn kiểm tra xem nếu có được nhấp nút Submit, bạn sử dụng một chức năng đặc biệt gọi là checkdate (). PHP chức năng này là thông qua một tháng, ngày, và năm trong số và xác định liệu sự kết hợp của giá trị hợp lệSau khi mã này ban đầu, bảo vệ các trang từ những người không đăng nhập. | CHAPTER 7 Building an Online Auction Site 247 db mysql_connect dbhost dbuser dbpassword mysql_select_db dbdatabase db After this initial code protect the page from people who are not logged in. Use the usual trick of checking to see if a USERNAME session variable exists db mysql_connect dbhost dbuser dbpassword mysql_select_db dbdatabase db if isset _SESSION USERNAME FALSE header Location . config_basedir . ref newitem Begin processing the form if isset _SESSION USERNAME FALSE header Location . config_basedir . ref newitem if _POST submit validdate checkdate _POST month _POST day _POST year After you check to see if the Submit button has been clicked you use a special function called checkdate . This PHP function is passed a month day and year in numbers and determines whether the combination of values is a valid date. This function is useful for determining invalid dates such as February 31 2005. In this new line of code the variables from the form are passed to the function. If the date is valid the function returns TRUE if not the function returns FALSE. Next you check the result of the function and act accordingly. First check to see if the date is valid if _POST submit validdate checkdate _POST month _POST day _POST year if validdate TRUE concatdate _POST year . - . sprintf 02d _POST day . - . sprintf 02d _POST month . . _POST hour . . _POST minute . 00 If the date is valid the numbers are concatenated to form a valid MySQL date. MySQL dates come in the form 0000-00-00 00 00 year month day hour minute . Imagine that the user selected 10 as the day 12 as the month 2005 as the year 11 248 Practical PHP and MySQL as the hour and 30 as the minute. With these numbers the valid date would be 2005-12-10 11 30. The sprintf function which you used earlier to pad prices with zeros was used again this time to ensure that single digits have a leading zero so 1 would become 01 and so on . This is important for the date to be a valid MySQL date. .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
19    82    2    28-04-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.