【问题标题】:I looked over it 4 times and fixed all the errors I can find. Are there a problems with my code?我查看了 4 次并修复了我能找到的所有错误。我的代码有问题吗?
【发布时间】:2017-05-02 22:14:20
【问题描述】:

我找不到代码有什么问题。假设允许将产品插入到我的 SQL 表中,但 html 不会显示在我的浏览器中。我运行了一个类似的代码,它运行了,但我没有。所以我只是有点困惑。 我的代码中还存在哪些问题?

<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>

【问题讨论】:

  • 我放弃了。哪里出错了?
  • 也许如果你把它格式化得更好,错误就会跳出来。
  • 继续这个想法,直觉上我假设在不同的行上用?&gt;结束一个php部分会是一个问题,但我还没有尝试过,所以不能说当然。
  • 还有&lt;?php echo $newproduct ?&gt;需要一个分号&lt;?php echo $newproduct; ?&gt;
  • 所以清理格式和缩进可能会发现大量错误

标签: php html database mysqli


【解决方案1】:

php 标签需要在同一行开始和结束。

?>

您应该在开发过程中使用错误报告来调试潜在问题,因为它会立即显示此错误。

&lt;?php error_reporting(E_ALL); ?&gt;

   <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>

【讨论】:

    【解决方案2】:

    在第 42 和 47 行,您将 ?&gt; php 结束标记拆分为两行。这是不正确的,?&gt; 必须在同一行,中间不能有任何字符。

    在第 106 行,您可能有错字,因为您有

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

    那里。您将查询结果存储到$result 变量,然后您使用 $results 变量,所以您可能想要这样:$row = $result-&gt;fetch_assoc()

    在第 136 行,您有:function getDbparms();。 php中的函数名称后面不应该有分号,所以应该是function getDbparms()

    当您使用 PHP 编写代码时,我强烈建议您使用一些 IDE,它会监视此类基本错误。 我通过简单地将这段代码复制粘贴到 PhpStorm IDE 然后只是读取错误来发现所有上述错误。

    【讨论】:

    • 啊,好吧。谢谢,我会研究 PHPStorm。我一直在努力寻找一个好的IDE,因为我现在使用的只是记事本
    • @l.brsn PhpStorm 是付费的,但有两种可能合法地免费拥有它。您可以免费获得 EAP,或者,如果您是学生,您可以免费获得所有 JetBrains IDE 的许可证,您只需要有效的 ISIC。但也有许多其他非常有用的 IDE,例如 netbeans 或 eclipse。有关更多详细信息和更多 IDE,请查看 this comparison 示例
    【解决方案3】:
    <!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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      相关资源
      最近更新 更多