0

I would like to be able to add children into a database, which are connected to their parents (who has a member id MID). I believe that the error lies within the date format (atleast that's what I believe), I have also tried to use strtotime($dob), however this didn't change anything.

   $name = htmlspecialchars($_GET['Name']);
   $dob = $_GET['DOB'];
   $newDOB = date("Y-m-d", $dob);
   $mid = $_GET['mid'];

   if(isset($_GET['Name'], $_GET['DOB'], $_GET['mid']))
       $alert = true;
   if(!empty($name) && !empty($newDOB) && !empty($mid))
       add_family_member($mid, $newDOB, $name);

The function that adds the member:

function add_family_member($mid, $dob, $name)
{
    global $con;
    $sql = "INSERT INTO Children(MID, DOB, Name) VALUES(?, ?, ?)";
    $stmt = $con->prepare($sql);
    if($stmt)
    {
        $b = $stmt->bind_param("iss", $mid, $dob, $name);
        if($b)
        {
            $e = $stmt->execute();
            if($e)
                return true;
        }
    }

    return false;
}
4
  • try to remove if($b){} or echo $b Commented Jun 10, 2014 at 11:49
  • You can't mix PDO and MySQLi. Btw DateTime is specially made for... date and times ;-) Commented Jun 10, 2014 at 12:11
  • are you getting any insert at all or error ? if error, post it Commented Jun 10, 2014 at 12:34
  • Where am I mixing PDO with MySQLi? Commented Jun 12, 2014 at 15:46

2 Answers 2

1
function add_family_member($mid, $dob, $name)
{
    global $con;
    $sql = "INSERT INTO Children(MID, DOB, Name) VALUES(:mid, :dob, :name)";
    $stmt = $con->prepare($sql);
    if($stmt)
    {
        return $stmt->execute(array(
             'mid' => $mid,
             'dob' => $dob,
             'name' => $name
        ));           
    }

    return false;
}

see http://www.php.net/manual/en/pdostatement.execute.php for more examples

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work, wouldn't this only work with using PDO?
you dont said what you use, but yes this is pdo.
0

Try...

function add_family_member($mid, $dob, $name)
    {
        global $con;
        $sql = "INSERT INTO Children(MID, DOB, Name) VALUES(:mid, :dob, :name)";
        $stmt = $con->prepare($sql);
        $stmt->bindParam(':mid', $mid);
        $stmt->bindParam(':dob', $dob);
        $stmt->bindParam(':name', $name);
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.