Cant base64 decode from mysqli - php

I'm currently trying to echo information from my SQL database which I had to encode in order for it to efficiently write into the table; now when I try to echo the information, WHILE decoding it, the page displays nothing, I'm not entirely sure what I'm doing wrong at this point.
<?php
$type = $_SESSION['SESS_ACC_TYPE'];
$login = $_SESSION['SESS_LOGIN_NAME'];
$log = base64_decode(''.$row['log'].'');
if ($type == '2') {
$qry = "SELECT log FROM logs ";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $qry);
while($row = mysqli_fetch_assoc($result)){
echo ''.$log.'';
}
}
if ($type == '1') {
$qry = "SELECT log FROM logs WHERE login = '.$login.'";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $qry);
while($row = mysqli_fetch_assoc($result)){
echo ''.$log.'';
}
} else {
//do nothing
}
?>

There are many errors with your code:
I fixed them.
$type = $_SESSION['SESS_ACC_TYPE'];
$login = $_SESSION['SESS_LOGIN_NAME'];
if ($type == '2') {
$qry = "SELECT `log` FROM `logs`;";
}
if ($type == '1') {
$qry = "SELECT `log` FROM `logs` WHERE `login` = '" . $login . "';";
}
$result = mysqli_query($GLOBALS["___mysqli_ston"], $qry);
while($row = mysqli_fetch_assoc($result)) {
echo base64_decode($row['log']);
}

since mysql 5.6.1 there are mysql base64 decode/encode functions
select FROM_BASE64('....') ...
select TO_BASE64('....') ...
http://base64decode.net/mysql-from-base64

Related

prevent insert same id if the user/student not put timeout

i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>

Using transactions in PHP and MySQL

I'm a learner of PHP. This commit and rollback process does the task at each line. How can I commit and rollback all at once?
<?php
$con = mysql_connect('localhost','user', 'abcdefg');
if(!con)
{
echo "Can't connect to db.";
}else {
mysql_select_db('test');
}
if(!file_exits('test.csv')
{
echo "Can't find the file.";
}
$ar_1 = file('test.csv', FILE_IGNORE_NEW_LINES);
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === true)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}else {
//Rollback
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
}
}
mysql_close($con);
?>
you need to commit out of the loop, i used $bool to see if there somewhere the $result isn't true
it will look something like :
edit:
breaking from the loop when something is wrong as you don't need to continue looping to other elements.
$bool = 1;
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === false)
{
//Rollback
$bool = 0;
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
break;
}
}
if($bool == 1)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}
mysql_close($con);

Not saving in database table

I want edit record in db table but it doesn't save in db table and nothing changed after i submit this form.
Here codes that i forgot to put.
<?php
require('db.php');
include("auth.php"); //include auth.php file on all secure pages
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
This is my php codes
<?php
if(isset($_POST['new']) && $_POST['new']==1)
{
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$update="update doc set title='".$title."', date='".$date."', from_to='".$from_to."', details='".$details."', d_location='".$d_location."', d_stat='".$d_stat."' where id_doc='".$id_doc."'";
mysqli_query($connection, $update) or die(mysql_error());
$status = "File Record Updated Successfully. </br></br><a href='v_doc.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
// here some else code
}
?>
Not an answer. Too long for a comment.
The issue of parametrised queries aside, I find this easier to read:
UPDATE doc
SET title = '$title'
, date = '$date'
, from_to = '$from_to'
, details = '$details'
, d_location = '$d_location'
, d_stat = '$d_stat'
WHERE id_doc = '$id_doc'
And now see about parametrised queries
Try below:
<?php
if(isset($_POST['new']) && $_POST['new']==1)
{
$id_doc=$_REQUEST['id_doc'];
$query = "SELECT * from doc where id_doc='".$id_doc."'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$update="update doc set title='".$title."', date='".$date."', from_to='".$from_to."', details='".$details."', d_location='".$d_location."', d_stat='".$d_stat."' where id_doc='".$id_doc."'";
if(mysqli_query($connection, $update)) {
$status = "File Record Updated Successfully. </br></br><a href='v_doc.php'>View Updated Record</a>";
} else {
die(mysqli_error($connection));
}
echo '<p style="color:#FF0000;">'.$status.'</p>';
} else {
// here some else code
}
?>
This should show you exact error, once you get it. show it here, so we can check and do correction.

PHP script, getting a pending request list

I have only just started writing the PHP script to power the backend of my android app. What i'm currently trying to do is run a PHP script that goes into my database called send_friendreq and the table called pending_req and gets the row toUser and adds it to an array. The only problem i'm currently having is the fact that I cannot get the PHP script to run correctly. Any help would be appreciated. Here is my code for the PHP script that I currently have. Thank you very much for the help!
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql="SELECT fromUser FROM pending_req WHERE toUser='&username'";
$result = mysqli_query($con, $sql);
$array = array();
while ($row = pg_fetch_array($result))
{
$i++;
}
for($x=0;$x<$i;$x++)
{
echo $array[$x];
}
if(!$result)
{
echo 'Failed';
}
else
{
echo json_encode($array[$x]);
echo "<br>";
}
If you have suggestions on something that would work better / more efficiently / safer, please let me know!
If I am not wrong, you are trying to get json of results from your query. try this code.
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
$i=0;
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql = 'SELECT fromUser FROM pending_req WHERE toUser='. $username ;
$result = mysqli_query($con, $sql);
if(!$result) {
echo 'Failed';
}elseif($result){
$myArray = array();
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
you query syntax seems to be not correct plz modify as :
$sql="SELECT fromUser FROM pending_req WHERE toUser='$username'";

Check empty rows query

In fact I am working on a small PHP script but I am facing a problem right now.The problem is that i want to check if my query return records this is my mysqli query:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi"
$check = $conn->query($sql)
while($row = $check->fetch_assoc()) {$tocheck = $row['content'];}
I don't want to check the number of rows of this query to see if it is null.I want to check if all $row['content'] are empty.
How about this:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi";
$check = $conn->query($sql);
$contentAllEmpty = true;
while ($row = $check->fetch_assoc()) {
if (strlen($row['content']) > 0) {
$contentAllEmpty = false;
}
}
if ($contentAllEmpty) {
//do something
}
use ==
while ($row = $check->fetch_assoc()) {
if ($row['content'] == '') {
... code here
}
}
To get back only records where the content column is not empty -
$sql = "SELECT * FROM `specs` WHERE `btitleid` = $id AND `phoneid` = $aydi AND (`content` IS NOT NULL OR `content` != '') ";

Categories