Page 2 of 2

Re: passing data from html to php

Posted: Thu Jan 14, 2016 10:21 am
by slade powers
hello, all. i've been working at this for 2 days trying to discover what i've done wrong. i can't seem to get past the error with regard to the pdo execute statement. it's preventing me from passing the values to the db. it's driving me crazy.

any ideas at all will send me to do more research. thanks all.

for anyone interested to help you have my kudos.

again, this is the php:

Code: Select all

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=new_order", $username, $password);
	
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';

}catch(PDOException $e) {
     echo $e->getMessage();
}
?>

<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")

{
$errors = [];
}

if(empty($errors)) {

// prepare sql and bind parameters
    $query = "INSERT INTO details (description, temple, quantity, price)
            VALUES (:description, :temple, :quantity, :price)";
}
    $stmt = $dbh->prepare($query);
        $stmt->bindParam(':description', $_POST['description']);
        $stmt->bindParam(':temple', $_POST['temple']);
        $stmt->bindParam(':quantity', $_POST['quantity']);
        $stmt->bindParam(':price', $_POST['price']);
       $stmt->execute();

/*** close the database connection ***/
$dbh = null;
and this is the error message:
"Connected to database
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null' in /home/ewff/public_html/prac0.php:66 Stack trace: #0 /home/ewff/public_html/prac0.php(66): PDOStatement->execute() #1 {main} thrown in /home/ewff/public_html/prac0.php on line 66"

its driving me crazy 8O, yall.

Re: passing data from html to php

Posted: Thu Jan 14, 2016 10:35 am
by Celauran
Where's the form? What's the value of $_POST?

Re: passing data from html to php

Posted: Thu Jan 14, 2016 10:56 am
by slade powers
i attempted to change the handler from $stmt to $dbh because that's what the other PDO handlers are. i still get a fatal error message but it's different this time:
"Connected to database

Fatal error: Call to undefined method PDO::bindParam() in /home/ewff/public_html/php.php on line 62"
is that better than the PDOXception messages i was getting with regard to integrity constraint violation(see above error message)?
regardless i would need to work out line 62:

Code: Select all

        $dbh->bindParam(':description', $_POST['description']);
help

Re: passing data from html to php

Posted: Thu Jan 14, 2016 11:39 am
by Christopher
slade powers wrote:Fatal error: Call to undefined method PDO::bindParam()

Code: Select all

        $dbh->bindParam(':description', $_POST['description']);
help
bindParam() is a method of PDOStatement. Your variable $dbh is of type PDO. Your variable $stmt is of type PDOStatement. It should be something like:

Code: Select all

        $description = isset($_POST['description']) ? $_POST['description'] : '';
        $stmt->bindParam(':description', $description);
I thought we identified that the problems was $_POST['description'] is not defined. With this kind of code, you must verify if (isset($_POST['description']) && $_POST['description']) for the value -- and give the user an error message if description is required. All four POST variables must be verified that they are set and have valid values. Some may require a check they are != '', others (like quantity and price) should be checked for is_numeric(). Please validate all value coming from the request ($_POST, $_GET, $_REQUEST, etc.).

The basic logic for forms is:

Code: Select all

if (the form is submitted i.e., is POST)
     Filter and Validate all form values
     if (all parameters have valid values)
          Form accepted. Process values.
          Redirect to done page to (deal with resubmit problem)
          exit;
     else // parameters have invalid values
          Generate error messages
else // form not submitted, so first time on page
     Initialize form values

Display form