Error in the Database

Asked

Viewed 64 times

0

I am trying to make every time the user clicks a button happen an UPDATE, but for some reason gives the No database selected, what’s wrong with that code?

Update.PHP

<?php
 require_once 'Classes/BancoDAO.php';
 $codTarefa=$_GET['codTarefa'];

require_once "Classes/TarefasVO.php";
require_once "Classes/TarefasDAO.php";

 $objTarefa = new TarefasVO();
 $objBDTarefa = new TarefasDAO();

$mysqli = new mysqli('localhost', 'root', '', 'bdpi');

$sqlDetalhes= "Select * from tarefas where codigo_TAREFA='$codTarefa'";
$rsDetalhes= mysqli_query($mysqli, $sqlDetalhes) or die (mysqli_error($mysqli));

$tblDetalhes=mysqli_fetch_array($rsDetalhes);

$status=($tblDetalhes['status_TAREFA']+1);

 $objTarefa->setCodigoTarefa($codTarefa);
 $objTarefa->setStatusTarefa($status);

  $objBDTarefa->EditaStatusTarefa($codTarefa, $objTarefa);

  $codigo=$tblDetalhes['codidoAtividade_TAREFA'];

   header("location:Atividade.php?codAtividade=$codigo");


?>

Taskforce.phph -> Taskforce

  public function EditaStatusTarefa($codTarefa,$tmp){

  $mysqli = new mysqli('localhost', 'root', '', 'bdpi');

   $sqlEditaSN= "Update tarefas set status_TAREFA=";
$sqlEditaSN.="'".$tmp->getStatusTarefa()."'";
$sqlEditaSN.=" where codigo_TAREFA = '$codTarefa'";

mysqli_query($mysqli,$sqlEditaSN) or die(mysqli_error($mysqli));

$sqlDetalhes= "Select * from tarefas where codigo_TAREFA='$codTarefa'";
$rsDetalhes= mysqli_query($mysqli, $sqlDetalhes) or die (mysqli_error($mysqli));

$tblDetalhes=mysqli_fetch_array($rsDetalhes);

$codigo=$tblDetalhes['codidoAtividade_TAREFA'];

header("location:Atividade.php?codAtividade=$codigo");

}
  • 1

    $codigo=$tblDetalhes['codidoAtividade_TAREFA']; change to $codigo=$tblDetalhes[0]['codidoAtividade_TAREFA'];

  • Comment on the line header("location:Atividade.php?codAtividade=$codigo"); and below $sqlEditaSN you create these lines for testing: echo $sqlEditaSN; die(); then comment here the result that was printed on the page.

1 answer

0


The problem is that you are not selecting the database. The way you made the connection is object oriented, however its query is in procedural form.

Change that line:

$mysqli = new mysqli('localhost', 'root', '', 'bdpi');

To:

$mysqli = mysqli_connect('localhost', 'root', '', 'bdpi');

Thus, your connection with the bank will also be in procedural form.

  • You have a mysql syntax error, apparently your $codTarefa variable is empty. You can test your query on any mysql Workbench (for example: https://dev.mysql.com/downloads/workbench/). Another test you can do is replace in line: $codTarefa=$_GET['codTarefa'];

  • @Rodrigosandrini I think the problem is on this line $sqlEditaSN.="'".$tmp->getStatusTarefa()."',";... For this concatenation with a comma at the end followed by a WHERE will give error

  • @J.Jones does not forget to erase these or die() when everything is working properly.

  • @J.Jones the problem may be in the use of "quotes". This mysql_* is the old way of programming, if I remember correctly, the fields of the table of numerical value between "quotes", type "1" or "2", generates a syntax error. Change this line to $sqlDetalhes= "Select * from tarefas where codigo_TAREFA=$codTarefa";

Browser other questions tagged

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