سؤال

This is a more concise version of a question I asked earlier in hopes of getting an answer. I've made a form that is supposed to send submitted info to the database utilizing a class.


beeradded.php

<?php
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);

//var_dump($_POST);
if ($_SESSION['logged_in']!="yes"){
    header ("Location: unauth.php");
}

require "/home/carlton/public_html/PHPproject/allincludes.php";

//if (count($_POST)>0){
//need to add in validation check here
//
//
//
//
//$validationErrors= 0;

    //if(count($validationErrors == 0 )){
        $form=$_POST;
        $beer_name = $form['beer_name'];
        $beer_type = $form['beer_type'];
        $beer_abv = $form['beer_abv'];
        $beer_rating = $form['beer_rating'];

   // }

    $new_beer = new Beer($beer_name);
    $new_beer->setBeerType($beer_type);
    $new_beer->setBeerABV($beer_abv);
    $new_beer->setBeerRating($beer_rating);

    $beereditor->addBeer($new_beer);
    echo "Beer added.";
 echo '<a href="addbeer.php"> Back to add menu </a>';  
//}


editbeers.php

<?php

class BeerEditor
{
    protected $dbconn;

    function __construct($dbconn){
        $this->dbconn = $dbconn;
    }

    function addBeer(Beer $beerObj){
        //making connection to db here
        $conn = $this->dbconn->getConnection();

        $stmt = $conn->prepare("INSERT INTO beers (beer_name, beer_type, beer_abv, beer_rating) VALUES (:beer_name, :beer_type, :beer_abv, :beer_rating)");

        $stmt->bindParam(':beer_name', $beerObj->getBeerName());
        $stmt->bindParam(':beer_type', $beerObj->getBeerType());
        $stmt->bindParam(':beer_abv', $beerObj->getBeerABV());
        $stmt->bindParam(':beer_rating', $beerObj->getBeerRating());

        $result = $stmt->execute();

        if($result === false){
            var_dump($conn->errorCode());
        }

        return $result;
    }

    function listBeers(){

        $conn = $this->dbconn->getConnection();
        $result = $conn->query('SELECT * FROM beers');

        $result->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'beers');
        $beers = $result->fetchAll();

        return $beers;
    }


}
?>

beer.php

<?php
/**
 * Description of beer
 *
 * @author root
 */
class beer {

    protected $beer_name;   //varchar(45)
    protected $beer_type;   //varchar(45)
    protected $beer_abv;    //decimal(4,2) alcohol percentage ex. 06.50
    protected $beer_rating; //char(10) 1 awful beer, 10 great beer

    public function __construct($beer_name = null){
        if ($beer_name !== null){
            $this->setName($beer_name);
        }    
        //defaults
       // $this->setType($beer_type);
       // $this->setABV($beer_abv);
       // $this->setRating($beer_rating);


    }

        public function setBeerName(){
            $this->beer_name = $beer_name;
        }
        public function getBeerName(){
                return $this->beer_name;
        }

        public function setBeerType(){
            $this->beer_type = $beer_type;
        }
        public function getBeerType(){
            return $this->beer_type;
        }

        public function setBeerABV(){
            $this->beer_abv = $beer_abv;
        }
        public function getBeerABV(){
            return $this->beer_abv;
        }

        public function setBeerRating(){
            $this->beer_rating = $beer_rating;
        }
        public function getBeerRating(){
            return $this->beer_rating;
        }
}
هل كانت مفيدة؟

المحلول

$new_beer->setBeerRating($beer_rating);
echo "Beer added.";

You will want to add a line in here where you call BeerEditor's addBeer($new_beer) function. Currently, you have nothing that calls that function, so the INSERT is never executed. Nothing in your Beer class interacts with the DB, so all you're doing is setting a local Beer object, then discarding it.

EDIT: per my comment, you will need to:

  • define a new PDO connection like $conn
  • set something like $beerEditor = new BeerEditor($conn);
  • run $beerEditor->addBeer($new_beer);

Google can help you quite a bit, here

نصائح أخرى

In addition to @doliver's answer

count($validationErrors) 

will return 1 since its an Int not an array. So nothing inside the if statement will run

It looks like $_POST=$form should be $form=$_POST. You're obliterating all of your data. Also, addBeer is not being called in your example. Also, your login system is exploitable. I could just ignore the redirect there if I'm not hitting it from a browser.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top