PHP + MySQL taking a 3rd variable - php

I have a C# code and a PHP that gets info I send from C#.
So I need to have such code:
<?php
$link = mysqli_connect('localhost','root','');
$database = mysqli_select_db($link,'serial');
$serial = $_GET['serial'];
$hwid = $_GET['hwidin'];
$sql = "SELECT * FROM serial WHERE serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
/*
0 = Wrong HWID
1 = HWID is correct
2 = HWID left empty
3 = No serial with that key
*/
if(strlen($hwid) < 1)
{
echo "2";
}
else
{
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
}
else
{
$sql = "UPDATE serial SET hwid='$hwid' WHERE serial='$serial'";
echo "1";
if(mysqli_query($link, $sql))
{
echo $row['hwid'];
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
}
}
else
{
echo "3";
}
}
So what I want to do is add some checking, for example:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
If echo "1" {
$sql = "SELECT time FROM serial Where serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
if $result > 1 {
echo "55";
}
I need somehow to combine it with hwid checker and serial checker.
I want to check "time" column variable and use > < 1 to decide what to echo.

Related

Displaying data from database issue

I'm trying to display all the rows in my data from my data of my specific user but its only displaying 1 row and not updating.
the code is -
$user = $_SESSION["username"];
$sql = "SELECT * FROM transactions WHERE sender OR receiver = ?";
if ($sql = $conn->prepare($sql)) {
$sql->bind_param("s",$user);
if ($sql->execute()) {
$result = $sql->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<br> sender: ". $row["sender"]. " - receiver: ". $row["receiver"]. " -amount:" . $row["amount"] . "<br>";
}
}
$sql->close();
$conn->close();
}else{ echo "0 result"; }
}
?>
You are using wrong SQL formation & passing just receiver value. I suggest below changes:
Replaced
1) WHERE sender OR receiver = ? with WHERE sender=? OR receiver = ?
2) $sql->bind_param("s",$user); with $sql->bind_param("ss",$user,$user);
$user = $_SESSION["username"];
$sql = "SELECT * FROM transactions WHERE sender=? OR receiver = ?";
if ($sql = $conn->prepare($sql)) {
$sql->bind_param("ss",$user,$user);
if ($sql->execute()) {
$result = $sql->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<br> sender: ". $row["sender"]. " - receiver: ". $row["receiver"]. " -amount:" . $row["amount"] . "<br>";
}
}
$sql->close();
$conn->close();
}else{ echo "0 result"; }
}

Why is isset($_POST['like']) not working when I click on submit button?

My POST method is not working in this code but if I chnage it to GET method then it works fine.
This is my code. If you change it to GET then works just fine. Even with POST, it displays likes from the database but it's not adding more like(s) when I click on the like button.
<form action="" method="POST">
<input type="hidden" name="blogID" value=<?php echo $blogID;?> />
<input id="likeButton" <?php echo isset($_POST["like"]) ? "disabled" : "";?> type = "submit" value = "Click to Like this Article" name="like"/> <br><br>
</form>
<br>
<?php
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($pageWasRefreshed )
{
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// output data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
else{
if(isset($_POST['like'])) {
$blogID=$_POST['blogID'];
$likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
$conn->query($likesQuery2);
$likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes3 = $conn->query($likesQuery3);
if ($likes3->num_rows > 0) {
// output data of each row
while($row = $likes3->fetch_assoc()) {
echo $row["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
else{
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// outputz data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
}
;?>
In else part, you are trying to check isset($_POST['like']) field in the form which not exists.
Update
else{
if(isset($_POST['like'])) {
$blogID=$_POST['blogID'];
To
else{
if(isset($_POST['blogID'])) {
$blogID=$_POST['blogID'];
Okay so the problem is not with the isset itself, it is that $pageWasRefreshed is always true and thus not reaching to your $_POST condition
Try putting your $_POST condition first, I think it will work fine
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if(isset($_POST['like'])) {
$blogID=$_POST['blogID'];
$likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
$conn->query($likesQuery2);
$likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes3 = $conn->query($likesQuery3);
if ($likes3->num_rows > 0) {
// output data of each row
while($row = $likes3->fetch_assoc()) {
echo $row["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
} else if($pageWasRefreshed ){
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// output data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}
else {
$likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID";
$likes = $conn->query($likesQuery);
if ($likes->num_rows > 0) {
// outputz data of each row
while($row2 = $likes->fetch_assoc()) {
echo $row2["likes"] . " Likes <br> <br>";
}
} else {
echo "0 Likes";
}
}

Pagination not showing results on next page

Looking for some help please. I am using the following code to filter and show results which works fine. My problem is the results aren't passing on to the second page so the page becomes blank no errors. very new to this so all help is welcome.
$num_rec_per_page=2;
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("searchresults") or die("ERROR");
if (!isset($_POST['submit'])) exit();
$vars = array('companyname','location');
$verified = TRUE;
foreach ($vars as $v) {
if (!isset($_POST[$v]) || empty($_POST[$v])) {
$verified = FALSE;
}
}
if (!$verified) {
echo "Sorry no results found, please enter your search";
exit();
}
// isset function is also used in checking for the existence of data in $_POST
if (isset($_POST["companyname"]) && $_POST["companyname"] != '') {
$companyname = mysql_real_escape_string($_POST["companyname"]);
$companyname = " AND (companyname LIKE '%$companyname%')";
}
if (isset($_POST["location"]) && $_POST["location"] != '') {
$location = mysql_real_escape_string($_POST["location"]);
$location = " AND (location LIKE '%$location%')";
}
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM reviewsandtips WHERE member_id > 0". $companyname . $location . "LIMIT $start_from, $num_rec_per_page";
$sql_result = mysql_query($sql) or die (mysql_error());
// now echo the code for the table heading
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
}
$sql = "SELECT * FROM reviewsandtips WHERE member_id > 0". $companyname . $location;
$sql_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($sql_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='codetest.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='codetest.php?page=".$i."'>".$i."</a> ";
}
echo "<a href='codetest.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
} else {
echo '<tr><td colspan="5">No results found.</td></tr>';
}

Why does my query result repeat 3 times?

the code i have below basically behaves as a friend search engine where you can add someone if you don't have them on you account. So i am trying to do the following: If exists it should just display the friend and if not then it should say add friend.
I got it to work but the tail end of the query (please see this comment "//Problem code - when i use this echo below this where it repeats 3 times") is giving me trouble where it repeats Add 3 times.
<?php
$num_rows1 = mysql_num_rows($result);
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("<div id=norequests>No results for <strong>$q
</strong></div>");
}
elseif ($rows > 0) {
while ($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
$linksys = htmlspecialchars($row['name']);
$pid = htmlspecialchars($row['system_id']);
}
print("");
}
}
else{
echo '<div id="error">No results.</div>';
}
$sql = "SELECT `Friend_id` from `friends_container`
WHERE `System_id` = '$sid'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die("Error: " . mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if ($rows == 0) {
print("");
}
elseif ($rows > 0)
{
while ($row = mysql_fetch_array($query))
{
$existing = htmlspecialchars($row['Friend_id']);
if ($existing == $pid) {
echo("<img src=$linksys />$person - Already Existing");
}
else
//Problem code - when i use this echo below this where it repeats 3 times
{
echo("Add $person");
}
}
?>
You must have 3 images stored for each account.
Your query will always result in a multiple result. what you should do is use php to convert it to something which will result in a better option or say convert the result in an array for convenience.

Query count and function not working properly

I am using the code below to select items and using the $result variable to do a count. if there are less than 1 it will say add more and if there are more than 5 it will say view all. It works for less than 1 but it won't for more than 5. Am i doing it right?
//Query
$sql = "SELECT id, name, why, date_time
FROM tabs
WHERE p_id = '$pid'
ORDER BY id
LIMIT 0, 5";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0) {
print("");
} elseif($rows > 0) {
while($row = mysql_fetch_array($query)) {
$name = $row['name'];
$w = nl2br($row['why']);
$y = $row['date_time'];
print("echoing contents here");
}
}
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
} ?>
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
}
Should be
if($rows > 5) {
echo "view all";
}
if($rows < 1) {
echo "add one";
}
Since you exhausted the result set with the previous mysql_fetch_array() loop

Categories