Link to home
Start Free TrialLog in
Avatar of the student
the student

asked on

PHP insert data in to database... this is not letting me in insert data...please help!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Saving Data</title>
    <a href="showDetails.php">Show All User's Profile</a>
</head>
<body>
<?php
$fName =$_POST['fName'];
$lName=$_POST['lName'];
$email=$_POST['email'];
$PhoneNo=$_POST['PhoneNo'];
$gender=$_POST['gender'];
$resumeDoc = $_POST['resumeDoc'];
 //VARIABLE TO INDICATE IF INPUT HAS 1 OR MORE ERROR
$ok=true;
//VALIDATE THE INPUTS  
if(empty($fName)){
    echo 'First Name is required <br />';
    $ok=false;
}
if(empty($lName)){
    echo 'Last Name is required <br />';
    $ok=false;
}
if(empty($email)){
    echo 'Email is required <br />';
    $ok=false;
}
if(!empty($PhoneNo)) {
    if ($PhoneNo < 1) {
        echo 'Please Enter Valid Number';
        $ok = false;
    }
    else{
        // CONVERT DOUBLE INTO INTEGER
        $phoneNo =intval($PhoneNo);
    }
}


// VARIABLE TO INDICATE IF INPUT HAS 1 OR MORE ERROR
if ($ok=true) {
    $conn = new PDO('mysql:host=ca-cdbr-azure-central-a.cloudapp.net;dbname=abcdef', 'abc', 'abc');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO uploadResume(fName,lName,email,PhoneNo,gender,resumeDoc)VALUES( :fNmame, :lName,:email,:PhoneNo, :gender,:resumeDoc);";
    $cmd = $conn->prepare($sql);
    $cmd->bindParam(':fName', $fName, PDO::PARAM_STR, 50);
    $cmd->bindParam(':lName', $lName, PDO::PARAM_STR, 50);
    $cmd->bindParam(':email', $email, PDO::PARAM_STR, 254);
    $cmd->bindParam(':PhoneNo', $PhoneNo, PDO::PARAM_INT);
    $cmd->bindParam(':gender', $gender, PDO::PARAM_NULL);
    $cmd->bindParam(':resumeDoc', $resumeDoc, PDO::PARAM_LOB);
    $cmd->execute();
    $conn = null;
    echo 'Candidate Information Saved';
}
?>



</body>
</html>
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You can find the correct ways to use PDO in this article.  Look for the PDO examples, where we use try/catch notation to find out if the PDO commands worked.  These commands usually throw PDOException when they fail.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

If you're new to PHP and want to find dependable learning resources, this article can help.  Just skip over the parts you already know from academics or work experience.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
Avatar of the student
the student

ASKER

Am I doing something wrong with PDO??
I can't really tell.  Why not try catching the PDO Exception and seeing what it says!
It doesn't show any error
That's to be expected.  You need to wrap the processes in try/catch blocks and catch the PDOException to find out what, if anything, is in the error message.  As far as I can recall, all MySQL extensions work this way.  They are silent about errors unless you make an affirmative effort to test the return values, and locate and display the error messages.

It's all shown in the (tested and working) code examples in the article.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@hielo: Good catch.  I think the if() statement will evaluate true after the assignment, so it's probably still running the query and then failing silently.  I was hoping to lead the author to learning -- how to do the error visualization that is needed when things go wrong with PDO/MySQL.