S
S
SinSin352020-05-09 03:37:18
PHP
SinSin35, 2020-05-09 03:37:18

How to connect two php files to one database?

There are 3 php files: the first is the main index.php , the second contains a function that creates a specific div and connects to the main file via require_once, the third file is a database connection and connects to the first via include_once.
Now there is a need to connect the database to the second file, but $conn gives NULL there.
Second file:

<?php
include_once("db_connection.php");
function component($productimage,$productname,$producttext,$productdiscount,$productprice,$productid){
$element = "
<div class=\"col-md-4 col-sm-6 my-3 my-md-0\">
  <form action=\"index.php\" method=\"post\">
    <div class=\"card shadow\">
      <a href='./index.php?product=$productid'>
      <div id=\"card-top\">
        <img src=\"$productimage\" alt=\"$productname\" class=\"img-fluid card-img-top\">
      </div>
      <div class=\"card-body\">
        <h5 class=\"card-title\">
          $productname
        </h5>
      </a>
        <h6>
          <i class=\"fas fa-star\"></i>
          <i class=\"fas fa-star\"></i>
          <i class=\"fas fa-star\"></i>
          <i class=\"fas fa-star\"></i>
          <i class=\"far fa-star\"></i>
        </h6>
        <p class=\"card-text\" id=\"description\">
          $producttext
        </p>";
        if($productprice==$productdiscount){
          $element.="<h5>
          <span class='price' id='disc'> $productprice P</span>
          </h5>";
        }
        else{
          $element.="<h5>
          <small><s class='text-secondary' id='nondisc'>$productdiscount</s></small>
          <span class='price' id='disc'> $productprice P</span>
          </h5>";
        };
        $sql = "SELECT * FROM zakaz WHERE Prod_id=".$productid.";";
        var_dump($sql);
        $result = mysqli_query($conn,$sql);
        $resultCheck = mysqli_num_rows($result);
        if ($resultCheck > 0){
        $element.="<button type='submit' href='./index.php' class='btn btn-warning my-3' name='add'>Добавить в корзину</button>
              <input type='hidden' name='product_id' value='$productid'>";}
        else{
        $element.="<button type='submit' href='./index.php' class='btn btn-warning my-3' name='add' disabled>Добавлено в корзину</button>
          <input type='hidden' name='product_id' value='$productid'>";					
        };
        $element.="
      </div>
    </div>
  </form>
</div>";
echo $element;
}
?>


Third file:
<?php 
$dbServername = "localhost";
$dbUsername="root";
$dbPassword="";
$dbName="kursor";

$conn=mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);

if (!$conn){
  die("Connection failed: ".mysqli_connect_error());
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AUser0, 2020-05-09
@SinSin35

You need to study variable scope .
And in the case of the above code, there are two solutions: kosher and idiologically correct - to insert $conn into the list of function arguments, and the second, for which they are strongly scolded - to make a declaration inside the function global $conn;. Of course, a third method is also possible, include("db_connection.php");inside a function, but this is already terry bydlockerism and a stupid waste of free connections to MySQL.

F
FanatPHP, 2020-05-09
@FanatPHP

It is necessary to connect not a DB, but HTML to a file.
You don't get cramps yourself when you look at this

<div class=\"col-md-4 col-sm-6 my-3 my-md-0\">
  <form action=\"index.php\" method=\"post\">
    <div class=\"card shadow\">
      <a href='./index.php?product=$productid'>

?
Didn't you get tired of writing all these sticks and quotes?
Now, why do you need a database in HTML?
It is not easier to execute your request in the main file, in the same place where other requests are made, and in this, firstly, write HTML as is,
<div class="col-md-4 col-sm-6 my-3 my-md-0">
  <form action="index.php" method="post">
    <div class="card shadow">
      <a href="./index.php?product=<?=$productid?>">
      <div id="card-top">

and secondly, no function will be needed, and instead of calling the function, it will be necessary to simply include this file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question