Date Not Saving in Mysql from Php Registration Form

前端 未结 4 1369
别跟我提以往
别跟我提以往 2021-01-29 08:46

I am having trouble saving the date of birth in Mysql Db from Php registration form, what am I doing wrong?

  1. Check.php

    $month = $_POST[\'month\'];
相关标签:
4条回答
  • 2021-01-29 08:54

    You should

    • fetch the correct POST-vars!!!!
    • use the dot sign for string concaternation
    • do a correct quotation
    • take care of cases in varnames ($day is not $Day)
    • dont put the unfiltered $_GET or $_POST vars into a query string
    • dont use mysql_ functions, they are deprecated

    this query should do what you want without errors:

    $month = $_POST['Birthday_Month']; 
    $day = $_POST['Birthday_day']; 
    $year = $_POST['Birthday_Year']; 
    
    $date = $year . "-" . $month . "-" . $day;
    
    $query = "INSERT INTO users (FirstName,LastName,Phone,DOB) VALUES ( '" . $FirstName . "','". $LastName . "','" . $Phone . "','" . $date . "')";
    
    mysql_query($query) or die(mysql_error());
    

    EDIT : I saw that you try to fetch the wrong POST-vars! You fetched $_POST['day'] but you need to fetch $_POST['Birthday_day'] which is the name of your select-field

    0 讨论(0)
  • 2021-01-29 08:58

    You are doing wrong in concating in mysql query.

    $FirstName + "','" 
    

    above is wrong.

    use it as below.

    '". $FirstName ."',
    

    Edit

    $date = $year."-".$Month."-".$Day;
    
    INSERT INTO users 
        (FirstName,LastName,Phone,DOB)
    VALUES
    ( '". $FirstName ."','". $LastName ."','". $Phone ."','". $date ."')
    
    0 讨论(0)
  • 2021-01-29 09:06

    Mysql query should be format like

    mysql_query("INSERT INTO users(FirstName,LastName,Phone,DOB)VALUES ( '".$FirstName."','". $LastName . "','" . $Phone . "','" . $Year . "-" . $Month + "-" . $Day . '")");
    
    0 讨论(0)
  • 2021-01-29 09:21

    As others guys are saying, why not using PDO or MySQLi?

    Your code will be more simple to code, and your code can be maintained over the time with other newer versions of PHP (MySQL FUNCTION ARE DEPRECATED FROM PHP5.5), e.g :

    <?php
    
        $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $dob = $_SESSION['dob'];
        $_SESSION['dob'] = $day . "-" . $month . "-" . $year;
    
        $stmt = $pdoconnect->prepare('INSERT INTO users(FirstName,LastName,Phone,DOB) VALUES (:firstname, :lastname, :phone, :dob)')
        $stmt->execute(array(
            'firstname' => $FirstName,
            'lastname' => $LastName,
            'phone' => $Phone,
            'dob'=> $dob
        ));
    
        echo "Successful Registration!"; 
    
    ?>
    

    I include a solution for simplify DOB using session.

    EDIT (adding how to use pdo_mysql) :

    // $DNS contain host (mysql:host=localhost by default), the database name and MySQL port (port=3606 by default).
    $dns = 'mysql:host=localhost;dbname=beathost_cms;port=3606';
    // $user --> MySQL USER
    $user = 'root';
    // $pass --> MySQL PASSWORD
    $pass = 'root*';
    try {
    
                $pdoconnect = new PDO($dns, $user, $pass);
                $pdoconnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    
            } catch (PDOException $e) {
                die ('An error occured with MySQL: ' . $e->getMessage());
            }
    
    0 讨论(0)
提交回复
热议问题