Problem sending something to database

Asked

Viewed 58 times

0

So, I’m trying to create a page, really simple, to send some things to the database, (id,username,code_placa) but I’m not able to, when removing the lines that give error, the page opens, however, The upload button just refreshes the page and does not send anything to the database, could you help me? Errors that appear:

Warning: include(../../Templates/Hk_Head_2.php): failed to open stream: No such file or directory in /home/iceag061/public_html/hk/badges.php on line 2

Warning: include(../../Templates/Hk_Head_2.php): failed to open stream: No such file or directory in /home/iceag061/public_html/hk/badges.php on line 2

Warning: include(): Failed opening '../../Templates/Hk_Head_2.php' for inclusion (include_path='.:/opt/php56/lib/php') in /home/iceag061/public_html/hk/badges.php on line 2

Fatal error: Call to a member function query() on null in /home/iceag061/public_html/hk/badges.php on line 4

Code:

<?php
include "../../Templates/Hk_Head_2.php";

$query = $link->query('SELECT rank FROM usuarios WHERE username = "' .$username. '"');
while($row = mysqli_fetch_array($query))
{
  $rangouser = $row['rank'];
}
if("$rangouser" == "2"){
header("Location: " . $_SERVER['HTTP_REFERER']);
  exit;
}a
if("$rangouser" == "1"){
header("Location: " . $_SERVER['HTTP_REFERER']);
  exit;
}
if("$rangouser" == "3"){
header("Location: " . $_SERVER['HTTP_REFERER']);
  exit;
}
if("$rangouser" == "4"){
header("Location: " . $_SERVER['HTTP_REFERER']);
  exit;
}

include "../../Templates/Hk_Nav.php";
?>

      <div class="container">
      <!-- Main component for a primary marketing message or call to action -->
     <div class="row">
<div class="col-md-8">
          <div class="panel panel-default">
                  <div class="panel-heading blue">
              <h3 class="panel-title"><?php echo $lang[426]; ?></h3>
            </div>
            <div class="panel-body">

<div class="formulariohk">
<form action="" method="post" enctype="multipart/form-data">
              <label><?php echo $lang[418]; ?></label>
                    <input style="margin-bottom: 10px;" type="text" required="" class="form-control" name="id" placeholder="Id Da Placa" value="" />  <br>

                     <label><?php echo $lang[422]; ?></label>
                    <input style="margin-bottom: 10px;" type="text" required="" class="form-control" name="username" placeholder="Usuario" value="" />  <br>

                    <label><?php echo $lang[423]; ?></label><br>
                    <input style="margin-bottom: 10px;" type="text" required="" class="form-control" name="code_placa" placeholder="Codigo do emblema" value="" />  <br>

                    <center><input class="btn btn-primary" name="guardar" type="submit" value="<?php echo $lang[192]; ?>" style="width: 120px;" /></center>
                      </form>

                      <?php
if ($_POST['guardar'] && $_POST['titulo']) {
$enviar = "INSERT INTO usuarios_placas (id,username,code_placa) values ('".$username."','".strip_tags($_POST['id'])."','".strip_tags($_POST['username'])."','".$_POST['code_placa']."')";

if (@$link->query($enviar)) { 

// Guardar acci贸n en Logs si se ha iniciado sesi贸n

$fecha_log = date("Y-m-d");
$accion = $lang[434];
$enviar_log = "INSERT INTO logs (usuario,accion,fecha) values ('".$username."','".$accion."','".$fecha_log."')";
$link->query($enviar_log);
// Log guardado en Base de datos

?>
<script type="text/javascript">
  location.href ="<?php echo $_SERVER['HTTP_REFERER']; ?>";
</script>
<?php
}
}
?>

</div></div>
            </div>
          </div>

        </div>
      </div><!-- /container -->

<?php 

include "../../Templates/Hk_Footer_2.php";

?>
  • 1

    Checks the file path called in include.

2 answers

1


The first three errors mean that the path or name of the files are incorrect if you need more help to resolve this issue edit your question adding the folder structure of your project

o 4° error means that the variable $link is null, ie there is no connection variable with the database, to create:

$link = new mysqli('localhost', 'username', 'password', 'database');

This is probably because the pages that are not being included is where the connection to the database is being created

Tip: instead of using a loop to store the data:

while ($row = mysqli_fetch_array($query)) {
    $rangouser = $row['rank'];
}

You can use the function fetch_all mysqli:

$rangouser = mysqli_fetch_all($query);

You can still pass a second parameter:

  • MYSQLI_ASSOC - returns an associative array
  • MYSQLI_NUM - returns a numerical array
  • MYSQLI_BOTH - returns a single array with the attributes of both.

MYSQLI_BOTH is the default value

Care: when using interpolation in the SQL statement your code is subject to SQL Injection

0

Your problem lies in the fact that your includes are being made the wrong way.

Let’s take the following example:

index.php
 /includes/footer.php

if I’m in index.php to include the footer just enough:

include 'includes/footer.php';

If I’m in footer.php and want to include the index:

include '../index.php';

Now let’s go to a scenario similar to yours:

index.php
 /includes/page/footer.php

if I’m in index.php to include footer.php just:

include 'includes/page/footer.php';]

If I’m in footer.php and want to include the index:

include '../../index.php';

The error that appears in your code clearly shows that the script cannot find the paths you typed to include them. Well I don’t know how your file organization is. So I can’t infer much. But I think if you try to change these parameters you’ll be able to solve them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.