Increase +1 in database

Asked

Viewed 327 times

-1

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);
?>
  • I’m already wearing it, but for some reason it’s not climbing +1

  • Want to insert or update (update) a record?

  • For example, the system I want to do is it has the 1 view and after that when someone view again increase +1

2 answers

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

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.

Browser other questions tagged

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