Increase +1 in the database

0

Well, I wanted to count +1 in mysql, and I'm not getting it.

Code below

<?php
$con=mysqli_connect("localhost","root","asdasd","dbb");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$valor = mysqli_query ($con, "SELECT * site_stats (item) VALUES");  
$novoValor = $valor + 1;  

mysqli_query($con,"SELECT * FROM site_stats");
mysqli_query($con,"INSERT INTO site_stats (item) VALUES ('$novoValor')");

mysqli_close($con);
?>
    
asked by anonymous 20.07.2018 / 02:07

2 answers

1

In addition to the form already shown, you can also do with the following code:

$con = mysqli_connect("localhost", "root", "asdasd", "dbb") or die("Failed to connect to MySQL: " . mysqli_connect_error());

mysqli_query($con, "UPDATE 'site_stats' SET 'item' = 'item'+1");

It will work the same way.

    
20.07.2018 / 03:34
1

Just fixing your code would look like this:

PHP

$con=mysqli_connect("localhost","root","asdasd","dbb");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$consulta = "SELECT item FROM site_stats";  

$result = $con->query($consulta);


while($dados = $result->fetch_array()){
//retorna o valor do campo item
   $valor = $dados["item"];
} 
$novoValor = $valor + 1;  

//atualiza o campo item
$sql = "UPDATE site_stats SET item = '$novoValor'";
$data = mysqli_query($con, $sql);

mysqli_close($con);
    
20.07.2018 / 02:55