-3

I can't find what is wrong with the code. It is suppose to allow inserting of products to my SQL table, but the html won't show up in my browser. I ran a similar code and it ran, but my didn't. So I'm just a little confused. Which problems are still remaining in my code?

<html>
  <head>
    <title>Create New Product</title>
  </head>
  <body>
<?php
if (isset($_POST["CreateSubmit"]))
{
  validate_form();
} else
{
  $messages = array();
  show_form($messages);
}
?>
<?php
function show_form($messages)
{
  //Assign post values if exist
  $newproduct = "";
  $productprice = "";
  $productID = "";

  if (isset($_POST["newproduct"]))
    $newproduct = $_POST["newproduct"];
  if (isset($_POST["productprice"]))
    $productprice = $_POST["productprice"];
  if (isset($_POST["productID"])) ;
  $productID = $_POST["productID"];
  echo "<p></p>";
  echo "<h2> Enter New Product</h2>";
  echo "<p></p>";
  ?>

  <h5>Fill out New Product Information and click Submit to create.</h5>
  <form name="createproduct" method="POST" action="InsertEcom.php">
    <table border="1" width="100%" cellpadding="0">
      <tr>
        <td width="157">Product Name:</td>
        <td><input type="text" name="newproduct" value='<?php echo $newproduct ?
     >' size="30"></td>
      </tr>
     <tr>
    <td width="157">Price:</td> 
    <td><input type="text" name="productprice" value=' <?php echo $productprice ?
    >' size="30"></td>
     </tr>
     <tr>
    <td width="157">Product ID:</td>
    <td><input type="text" name="productID" value=' <?php echo $productID ?>'
                   size="30"></td>
      </tr>
      <tr>
        <td width="157"><input type="submit" value="Submit" name="CreateSubmit">
        </td>
        <td>&nbsp;</td>
      </tr>
    </table>
  </form>
  <?php
} //End Show Form
?>


<?php
function validate_form()
{
  $messages = array();
  $redisplay = false;
//Assign values
  $newproduct = $_POST["newproduct"];
  $productprice = $_POST["productprice"];
  $productID = $_POST["productID"];
  $product = new ProductClass($newproduct, $productprice, $productID);
  $count = countProduct($product);
//Check for products that already exist and Do insert
  if ($count == 0)
  {
    $res = insertProduct($product);
    echo "<h3>New Product Inserted!</h3> ";
  } else
  {
    echo "<h3>A product with that ID already exist.</h3>";
  }
}//End Validate Form

function countProduct($product)
{
//Connect to the database
  $mysqli = connectdb();
  $newproduct = $product->getNewproduct();
  $productprice = $product->getProductprice();
  $productID = $product->getProductID();

//Connect to the database
  $mysqli = connectdb();
//Define the Query
//For Windows MYSQL String is case insentisitive
  $Myquery = "SELECT count(*) AS count FROM Products WHERE productID='$productID'";
  if ($result = $mysqli->query($Myquery))
  {
    /*Fetch the results of the query*/
    while ($row = $results->fetch_assoc())
    {
      $count = $row["count"];
    }
    /*Destroy the result set and free the memory used for it */
    $result->close();
  }
  $mysqli->close();
  return $count;
}//End count Product

function insertProduct($product)
{
//Connect to the database
  $mysqli = connectdb();
  $newproduct = $product->getNewproduct();
  $productprice = $product->getProductprice();
  $productID = $product->getProductID();
//Now we can insert
  $Query = "
    INSERT INTO Products
    (newProduct, productPrice, productID)
    VALUES
    ('$newproduct', '$productprice', '$productID')
  ";
  $Success = false;
  if ($result = $mysqli->query($Query))
  {
    $Success = true;
  }
  $mysqli->close();
  return $Success;
}//End Insert Product

function getDbparms();
{
  $trimmed = file('parms/dbparms.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPPTY_LINES);
  $key = array();

  $vals = array();
  foreach ($trimmed as $line)
  {
    $pairs = explode("=", $line);
    $key[] = $pairs[0];
    $vals[] = $pairs[1];
  }//foreach
//Combine Key and Values into an array
  $mypairs = array_combine($key, $vals);
//Assign values to ParametersClass
  $myDbparms = new DbparmsClass($mypairs['username'], $mypairs['password'],
    $mypairs['host'], $mypairs['db']);
//Display the Parameters values
  return $myDbparms;
}//End getDbparms

function connectdb()
{
  //Get the DBParameters
  $mydbparms = getDbparms();
//Try to connectdb
  $mysqli = new mysqli($mydbparms->getHost(), $mydbparms->getUsername(),
    $mydbparms->getPassword(), $mydbparms->getDb());
  if ($mysqli->connect_error)
  {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
  }
  return $mysqli;
}

class DbparmsClass
{
  //property declaration
  private $username = "";
  private $password = "";
  private $host = "";
  private $db = "";

  //constructor
  public function __construct($myusername, $mypassword, $myhost, $mydb)
  {
    $this->username = $myusername;
    $this->password = $mypassword;
    $this->host = $myhost;
    $this->db = $mydb;
  }

  //Get methods
  public function getUsername()
  {
    return $this->username;
  }

  public function getPassword()
  {
    return $this->password;
  }

  public function getHost()
  {
    return $this->host;
  }

  public function getDb()
  {
    return $this->db;
  }

  //Set methods
  public function setUsername($myusername)
  {
    $this->username = $myusername;
  }

  public function setPassword($mypassword)
  {
    $this->password = $mypassword;
  }

  public function setHost($myhost)
  {
    $this->host = $myhost;
  }

  public function setDb($mydb)
  {
    $this->db = $mydb;
  }
}    //End DBparms class

//Class to construct Products with getters/settes
class ProductClass
{
  //property declaration
  private $newproduct = "";
  private $productprice = "";
  private $productID = "";

  //Constructor
  public function __construct($newproduct, $productprice, $productID)
  {
    $this->newproduct = $newproduct;
    $this->productprice = $productprice;
    $this->productID = $productID;
  }

  //Get Methods
  public function getNewproduct()
  {
    return $this->newproduct;
  }

  public function getProductprice()
  {
    return $this->productprice;
  }

  public function getProductID()
  {
    return $this->productID;
  }

  //Set Methods
  public function setNewproduct($value)
  {
    $this->newproduct = $value;
  }

  public function setProductprice($value)
  {
    $this->productprice = $value;
  }

  public function setProductID($value)
  {
    $this->productID = $value;
  }
}//End ProductClass
?>
  </body>
</html>
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
l.brsn
  • 7
  • 3

3 Answers3

1

The php tags need to start and end on the same line.

?>

You should make use of error reporting to debug potential issues while developing as it revealed this error immediately.

<?php error_reporting(E_ALL); ?>

   <tr>
       <td width="157">Product Name:</td>
       <td><input type="text" name="newproduct" value='<?php echo $newproduct ?
     >' size="30"></td>
      </tr>
     <tr>
    <td width="157">Price:</td> 
    <td><input type="text" name="productprice" value='<?php echo $productprice ?
    >' size="30"></td>
     </tr>
Barry Thomas
  • 389
  • 4
  • 20
1

On lines 42 and 47, you have the ?> php ending tag splitted to two lines. This is not correct, the ?> must be on the same line, without any chars in between.

On line 106, you probably have typo, because you have

if ($result = $mysqli->query($Myquery)) { /*Fetch the results of the query*/ while ($row = $results->fetch_assoc() )

there. You store result of query to $result variable, but then you use $results variable, so probably you want to to this: $row = $result->fetch_assoc()

And on line 136, you have: function getDbparms();. The functions in php should not have semicolon after the name, so it should be function getDbparms() instead.

When you write in PHP, I highly recommend you to use some IDE, which watches for basic errors like this. I discovered all of the above mentioned errors by simply copypasting this code to PhpStorm IDE and then just reading errors.

Matěj Račinský
  • 1,679
  • 1
  • 16
  • 28
  • Ah okay. Thank you I will look into PHPStorm. I have been trying to find a good IDE because all I use now is Notepad – l.brsn May 03 '17 at 12:00
  • @l.brsn PhpStorm is paid, but there are two possibilities to have it legally for free. You can have the EAP, which is for free, or, if you are student, you can have licence for all JetBrains IDEs for free, all you need is valid ISIC. But there are also many other very good IDEs which can be pertty useful, for example netbeans or eclipse. For more details and more IDEs, take a look for example at [this comparison](https://www.phpclasses.org/blog/post/482-best-php-ide-of-the-year-comparison.html) – Matěj Račinský May 03 '17 at 15:47
0
<!DOCTYPE html>

<html>

<head>


    <title>Create New Product</title>
</head>

<body>
    <?php
    if (isset($_POST["CreateSubmit"])) {
        validate_form();
    } else {
        $messages = array();
        show_form($messages);
    }
    function show_form($messages)
    {
        //Assign post values if exist
        $newproduct="";
        $productprice="";
        $productID="";

        if (isset($_POST["newproduct"])) {
            $newproduct=$_POST["newproduct"];
        }
        if (isset($_POST["productprice"])) {
            $productprice=$_POST["productprice"];
        }
        if (isset($_POST["productID"])) {
            $productID=$_POST["productID"];
        }


        echo "<p></p>";
        echo "<h2> Enter New Product</h2>";
        echo "<p></p>";
        ?>

        <h5>Fill out New Product Information and click Submit to create.</h5>
        <form name="createproduct" method="POST" action="InsertEcom.php">
            <table border="1" width="100%" cellpadding="0">
                <tr>
                    <td width="157">Product Name:</td>
                <td><input type="text" name="newproduct" value='<?php echo $newproduct ?>' size="30"></td>
                </tr>
                <tr>
                    <td width="157">Price:</td>
                <td><input type="text" name="productprice" value='<?php echo $productprice ?>' size="30"></td>
                </tr>
                <tr>
                    <td width="157">Product ID:</td>
                <td><input type="text" name="productID" value='<?php echo $productID ?>' size="30"></td>
                </tr>
                <tr>
                    <td width="157"><input type="submit" value="Submit" name="CreateSubmit">
                    </td>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </form>
        <?php
    } //End Show Form

    function validate_form()
    {
        $messages = array();
        $redisplay = false;
        //Assign values
        $newproduct = $_POST["newproduct"];
        $productprice = $_POST["productprice"];
        $productID = $_POST["productID"];
        $product = new ProductClass($newproduct, $productprice, $productID);
        $count = countProduct($product);
        //Check for products that already exist and Do insert
        if ($count==0) {
            $res = insertProduct($product);
            echo "<h3>New Product Inserted!</h3> ";
        } else {
            echo "<h3>A product with that ID already exist.</h3>";
        }
    }//End Validate Form
    function countProduct($product)
    {
        //Connect to the database
        $mysqli = connectdb();
        $newproduct = $product->getNewproduct();
        $productprice = $product->getProductprice();
        $productID = $product->getProductID();

        //Connect to the database
        $mysqli = connectdb();
        //Define the Query
        //For Windows MYSQL String is case insentisitive
        $Myquery = "SELECT count(*) as count from Products
where productID='$productID'";
        if ($result = $mysqli->query($Myquery)) {
            /*Fetch the results of the query*/
            while ($row = $results->fetch_assoc()) {
                $count=$row["count"];
            }
            /*Destroy the result set and free the memory used for it */
            $result->close();
        }
        $mysqli->close();
        return $count;
    }//End count Product

    function insertProduct($product)
    {
        //Connect to the database
        $mysqli = connectdb();
        $newproduct = $product->getNewproduct();
        $productprice = $product->getProductprice();
        $productID = $product->getProductID();
        //Now we can insert
        $Query = "INSERT INTO Products
(newProduct, productPrice, productID)
VALUES ('$newproduct', '$productprice', '$productID')";
        $Success=false;
        if ($result = $mysqli->query($Query)) {
            $Success=true;
        }
        $mysqli->close();
        return $Success;
    }//End Insert Product

    function getDbparms()
    {
        $trimmed = file('parms/dbparms.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPPTY_LINES);
        $key = array();

        $vals = array();
        foreach ($trimmed as $line) {
            $pairs = explode("=", $line);
            $key[] = $pairs[0];
            $vals[] = $pairs[1];
        }//foreach
        //Combine Key and Values into an array
        $mypairs = array_combine($key, $vals);
        //Assign values to ParametersClass
        $myDbparms = new DbparmsClass($mypairs['username'], $mypairs['password'],
        $mypairs['host'], $mypairs['db']);
        //Display the Parameters values
        return $myDbparms;
    }//End getDbparms

    function connectdb()
    {
        //Get the DBParameters
        $mydbparms = getDbparms();
        //Try to connectdb
        $mysqli = new mysqli($mydbparms->getHost(), $mydbparms->getUsername(),
        $mydbparms->getPassword(), $mydbparms->getDb());
        if ($mysqli->connect_error) {
                die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
        }
        return $mysqli;
    }
    class DbparmsClass
    {
    //property declaration
        private $username="";
        private $password="";
        private $host="";
        private $db="";
    //constructor
        public function __construct($myusername, $mypassword, $myhost, $mydb)
        {
            $this->username = $myusername;
            $this->password = $mypassword;
            $this->host = $myhost;
            $this->db = $mydb;
        }
     //Get methods
        public function getUsername()
        {
              return $this->username;
        }
        public function getPassword()
        {
              return $this->password;
        }
        public function getHost()
        {
            return $this->host;
        }
        public function getDb()
        {
            return $this->db;
        }
    //Set methods
        public function setUsername($myusername)
        {
             $this->username = $myusername;
        }
        public function setPassword($mypassword)
        {
            $this->password = $mypassword;
        }
        public function setHost($myhost)
        {
             $this->host = $myhost;
        }
        public function setDb($mydb)
        {
            $this->db = $mydb;
        }
    }    //End DBparms class
    //Class to construct Products with getters/settes
    class ProductClass
    {
    //property declaration
        private $newproduct="";
        private $productprice="";
        private $productID="";

    //Constructor
        public function __construct($newproduct, $productprice, $productID)
        {
            $this->newproduct = $newproduct;
            $this->productprice = $productprice;
            $this->productID = $productID;
        }
    //Get Methods
        public function getNewproduct()
        {
            return $this->newproduct;
        }
        public function getProductprice()
        {
            return $this->productprice;
        }
        public function getProductID()
        {
            return $this->productID;
        }
     //Set Methods
        public function setNewproduct($value)
        {
             $this->newproduct = $value;
        }
        public function setProductprice($value)
        {
               $this->productprice = $value;
        }
        public function setProductID($value)
        {
            $this->productID = $value;
        }
    }//End ProductClass
    ?>
</body>

</html>
xxneno
  • 101
  • 2