How to Increment an integer in php - php

I want to Increment an integer in every time +1. Now my code just at first time work after that don't work it does not move to number 2.At first time I can see number 1 in my table but that number do not Increment at second time.
PHP code:
<?php
include 'connt.php';
$Id = $_POST['Id'];
$sql = "UPDATE student SET Posts =? WHERE Id=?" ;
$stmt = $con->prepare($sql);
$Posts =+1;
$stmt->bind_param("ss",$Posts,$Id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
?>

Your $Posts is never set. Incrementing it will always leave it at 1. You don't need to read values or increment in PHP, you can do it right in your query:
<?php
include 'connt.php';
$Id = $_POST['Id'];
$sql = "UPDATE student SET Posts = Posts+1 WHERE Id=?" ;
$stmt = $con->prepare($sql);
$stmt->bind_param("s",$Id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

You can simply use $Posts++ to increment your variable.

Your mistaken here,
$Posts =+1;
You probably meant something like this,
$Posts++;
// Or,
$Posts += 1;
Edit:
Hey You dont even have $Post defined in the first place !

With this code (if this is all the code), your $Posts variable is always 1.
You should first read the number of posts from the database, then increment it by one and then update it to the new value.

Related

unable to properly use the resqult of an sql query in php

What I'm trying to do is take the number of rows that meet a certain requirement, and depending on the number that do, the script will do different things.
my code:
$sql2 = "SELECT COUNT(email)FROM subscribers WHERE id= ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param("s", $id);
$stmt2->execute();
$callbacks = $stmt2->get_result();
while($rows = $callbacks->fetch_assoc()){
$results = $rows;
}
$stmt2->close();
if ($results <2001){
$email = trim($_POST["email"]);
}else{
$email_err = "This user has reached the limmit to what their plan can allow .";
}
The problem I'm having is that I'm unsure of how to actually use the number of rows.
In this configuration, it doesn't care what the number of results is, it just posts them anyways.
If I do something like this id_err = $results; to see what the query result is it gives me an array to string conversion error.
As hardware intensive as it sounds for this kind of operation, if you think should query the rows directly and count them with PHP please show me how.
I have also tried doing some variation on $results = $rows['count'] with different names such as 'total', and 'result' but they still post. when I try to see what the query result with id_err = $results; it flat out ignores it and posts anyways.
to avoid any confusion for on what $id_err is, it stops the script from posting if it ever gets used.
and to clarify this SQL query is within a conditional statement.
thank you in advance.
The count function returns all records as one row with the count.. so remove the while and compare the appropriate index.
$sql2 = "SELECT COUNT(email) as the_count FROM subscribers WHERE id= ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param("s", $id);
$stmt2->execute();
$callbacks = $stmt2->get_result();
$rows = $callbacks->fetch_assoc();
$stmt2->close();
if ($rows['the_count'] < 2001){
$email = trim($_POST["email"]);
}else{
$email_err = "This user has reached the limmit to what their plan can allow .";
}

I'm having problem with my sample project

edited...
hi guys can anyone help me with my website
I just want to get a specific comment in the comment area just like in facebook
<?php
require 'db.php';
$sql = 'SELECT * FROM comment WHERE postID = postID';
$statement = $connection->prepare($sql);
$statement->execute();
$comment = $statement->fetch(PDO::FETCH_OBJ);
?>
what I mean is that if I comment in the first post it will just fetch the current comment and it will not display on the other post.
Here's what your query is currently doing:
get records from the table comment where the field postID = postId
Which of course, won't work, you're using a string (without quotes) as your value. You need to bind the parameter and pass the value in, e.g.
$sql = 'SELECT * FROM `my_table` WHERE `some_field` = :myVal;';
$res = $conn->prepare($sql);
$res->execute([':myVal' => $_GET['id']]);
$data = $res->fetchAll(PDO::FETCH_ASSOC);
Here, I pass the bind a param (:myVal) and then prepare that statement. You then pass in the value for :myVal (in this case, a $_GET of the query param id).
Then fetch, var_dump/print_r that, and you should see table data in your script.
I believe answer would look like this:
<?php
require 'db.php';
$postId = 1; // get it somewhere, probably from $_GET
$sql = 'SELECT * FROM comment WHERE postID = ?';
$statement = $connection->prepare($sql);
$statement->execute( [$postId]);
$comment = $statement->fetch(PDO::FETCH_OBJ);
?>
Explanations, copied from comment to original question:
Your SQL condition postID = postID literally says any comment. You need to pass actual post ID there.

Why do i need to execute the statement twice?

$sql = "SELECT firstName , lastName FROM People WHERE born='1934'" ;
$stmt = $db->prepare($sql);
echo "<p>Execute the SQL-statement:<br><code>$sql</code><p>";
$stmt->execute();
// Get the results as an array with column names as array keys
$res = $stmt->fetchColumn();
$stmt->execute();
$res2 = $stmt->fetchColumn(1);
$ANSWER = $res." ".$res2;
If i remove the second execute(); statement the $res2 variable is empty.
Why is that? When i already have retrieved the results once/executed the statement.
if i input 1 as the parameter in the first fetchColumn(); i get the lastName DB column, so the results are there already.
The reason i'm using fetchColumn(); is that i need the result as string and not an array. I just cant understand why it doesnt work without the second execute, it seems like the result set gets destroyed or something after the first fetch and i need to execute it again? That sounds weird.

PHP/MySQL keeping track of uses

I'm trying to update my tables with a total number of uses when a function is called. What I need to do is grab the former number of uses, and add one. I have a pretty good general idea how to go about this, but I don't quite know how to get unique ID's with their respective uses. This is what I have so far...
<?php
$query = $conn->query('select name, uses from users');
$i = 0;
while($r = $query->fetch(PDO::FETCH_OBJ)){
$name[$i] = $r->name;
$uses[$i] = $r->uses;
$i = $i+1;
}
if(isset($_POST['mName']))
{
$mName = urldecode($_POST['mName']);
$mUses = $uses + 1;
"UPDATE users
SET uses=:uses WHERE name=:name";
$stmt = $conn->prepare($sqlUPDATE);
$stmt->bindParam(':uses', $mUses);
$stmt->bindParam(':name', $mName);
$stmt->execute();
?>
I can see my issue is that I'm assigning the uses variable to an array, I don't know how to get it specific to the username. Is this a simple SQL query I'm missing?
You can store the data in an array called $uses with the key being the user name and the value being the number of uses. Then if you detect a POST with the mName parameter set, you can reference your $uses array with that name and get the number of uses, and add 1.
<?php
$query = $conn->query('select name, uses from users');
while($r = $query->fetch(PDO::FETCH_OBJ)){
$uses[$r->name] = $r->uses;
}
if(isset($_POST['mName'])) {
$mName = urldecode($_POST['mName']);
$mUses = $uses[$mName] + 1;
$sqlUPDATE = "UPDATE users SET uses=:uses WHERE name=:name";
$stmt = $conn->prepare($sqlUPDATE);
$stmt->bindParam(':uses', $mUses);
$stmt->bindParam(':name', $mName);
$stmt->execute();
}
?>
Though there is no error checking and handling in here. If there happens to be a POST with mName and that name doesn't exist, nothing will update, but nothing will insert for new users. Also, instead of using a name, it would be better to use an id for the user if possible.

Short code to get 2 values from one row with php pdo

Possibly stupid question, but can not find answer.
I need to get values from two columns of the same row.
And then set variables with each value.
Here I get one value from column Number and then define variable $NumberPostRegister1
$stmt = $db->prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1");
$stmt->bindParam(':Number1', $row_id1);
$stmt->execute();
$NumberPostRegister1 = $stmt->fetchColumn();
echo $NumberPostRegister1 .' NumberPostRegister1<br>';
Here I get second value from column IfDraft and then define variable $IfDraft1
$stmt = $db->prepare("SELECT IfDraft FROM 2_1_journal WHERE Number = :Number1");
$stmt->bindParam(':Number1', $row_id1);
$stmt->execute();
$IfDraft1 = $stmt->fetchColumn();
echo $IfDraft1 .' NumberPostRegister1<br>';
Two queries and rather long code.
How to do the same using one query and shorter/simpler code?
$stmt = $db->prepare("SELECT IfDraft, Number FROM 2_1_journal WHERE Number = ?");
$stmt->execute(array($row_id1));
list($IfDraft, $Numer) = $stmt->fetch();

Categories