session variable getting unset after every ajax call - javascript

i am trying to modify a session variable in each ajax call (so as to later retreive only the new content from db ) but one session variable $_SESSION['s'] is getting unset in every alt. ajax call ! however the unmodified session variable $_SESSION["iuser"] is working fine . I am testing this on localhost / wamp and coding in npp .
<?php
session_start();
//if(isset($_SESSION["s"]))
$in=$_SESSION["s"];
//echo $_SESSION['sid'];
$link=mysql_connect("localhost","root","");
mysql_select_db("secure_scrapbook") or die("Sorry :( Connection Error");
$query="Select data,user,id from public where public=1 && id>'$in' ORDER BY id DESC";
$result=mysql_query($query);
$i=mysql_fetch_array($result);
$_SESSION["s"]=$i["id"];
//echo $_SESSION['sid'];
if(!($_SESSION["s"]==$in))
{echo 0;
echo $in;
echo $_SESSION["iuser"];
echo $_SESSION["s"];
echo "check<br>";
}
?>
--jquery /ajax -----
<script>
setInterval(function() {
$.ajax({
url: "gets.php",
type : "get" ,
dataType : 'html',
success : function(data){
//$("#public").html(data);}
$("#public").html(data+$("#public").html()+'-');}
});}, 5000); //5 seconds
</script>

May I suggest you debug your calls like this:
<?php
session_start();
if (empty($_SESSION["s"])){
echo 'EMPTY SESSION ';
die;
}
$in = $_SESSION["s"];
$link=mysql_connect("localhost","root","");
mysql_select_db("secure_scrapbook") or die("Sorry :( Connection Error");
$query="Select data,user,id from public where public=1 && id>'$in' ORDER BY id DESC";
$result=mysql_query($query);
$i=mysql_fetch_array($result);
if (empty($i["id"])){
echo 'EMPTY $i';
die;
}
$_SESSION["s"]=$i["id"];
//echo $_SESSION['sid'];
if(!($_SESSION["s"]==$in))
{echo 0;
echo $in;
echo $_SESSION["iuser"];
echo $_SESSION["s"];
echo "check<br>";
}
?>
Each time you get the message EMPTY SESSION or EMPTY $i check:
the ajax headers and try to step-by-step track your problem.
the element in the database does have an ID
the $in is within the boundries of possible ID's
the $in is a number

<?php
session_start();
//if(isset($_SESSION["s"]))
$in=$_SESSION["s"];
//echo $_SESSION['sid'];
$link=mysql_connect("localhost","root","");
mysql_select_db("secure_scrapbook") or die("Sorry :( Connection Error");
$query="Select data,user,id from public where public=1 && id>'$in' ORDER BY id DESC";
$result=mysql_query($query);
if($i=mysql_fetch_array($result))
{
$_SESSION["s"]=$i["id"];
//echo $_SESSION['sid'];
if(!($_SESSION["s"]==$in))
{
echo 0;
echo $in;
echo $_SESSION["iuser"];
echo $_SESSION["s"];
echo "check<br>";
}
}
else
echo "No more Data in table";
?>
Try this and if "No more Data in table" is echoed, it means that your db doesn't have any data satisfying the conditions you set.
One more thing, seeing your code it looks like $_SESSION["s"] will never be equal to $in so i guess you don't need to use the if condition.

Related

How to show alert box after successful or not data deletion in mssql

I want to show JavaScript alert after successful or not data deletion in MSSQL. How to do this? I have written this code but it shows only the message=success part alert everytime, even when the deletion dont work becasue of errors like "conflict with reference(foreign_key)" So when i click on this link.
echo "<a class='activater' href='ma_QualiOverviewloeschen.php?TestaufstellungID=".$row['TestaufstellungID'] ."&QualiID=".$row['QualiID'] ."' title='Qualitest löschen' data-toggle='tooltip' onclick='confirm_delete()'> <span class='glyphicon glyphicon-trash'></span></a>";
It calls the following php Page, which handle the SQL Part:
$QualiDelete =("DELETE FROM MyDB.dbo.Testaufstellung WHERE MyDB.dbo.Testaufstellung.TestaufstellungID = :TestaufstellungID");
$QualiDelete .=("DELETE FROM MyDB.dbo.AllgemeineAngaben WHERE MyDB.dbo.AllgemeineAngaben.QualiID = :QualiID");
$sth = $connection->prepare($QualiDelete);
$sth->execute(array(':TestaufstellungID' => $TestaufstellungID, ':QualiID:' => $QualiID));
if($sth)
{
header("location: ma_QualiOverview.php?message=success");
}
else
{
echo sqlsrv_errors();
header("location: ma_QualiOverview.php?message=failed");
}
$connection = null;
Back to the main page where the link is clicked the following ifelseconsider on messageshould Show me the right alert.
<?php
if($_GET['message']=='success'){
echo '<script language="javascript">';
echo 'alert("Erfolgreich gelöscht.");';
echo '</script>';
} elseif($_GET['message']=='failed'){
echo '<script language="javascript">';
echo 'alert("Nicht gelöscht, da Quali "ongoing" ist.");';
echo '</script>';
}
?>
What do i miss?
$sth will never be falsy, you have to check the return value of $sth->execute
Also, you should echo the errors after sending out the header.
Since $sth is always defined, you always get the success result
See the modified code here
$QualiDelete =("DELETE FROM MyDB.dbo.Testaufstellung WHERE MyDB.dbo.Testaufstellung.TestaufstellungID = :TestaufstellungID");
$QualiDelete .=("DELETE FROM MyDB.dbo.AllgemeineAngaben WHERE MyDB.dbo.AllgemeineAngaben.QualiID = :QualiID");
$sth = $connection->prepare($QualiDelete);//Check the value returned instead of $sth
$result = $sth->execute(array(':TestaufstellungID' => $TestaufstellungID, ':QualiID:' => $QualiID));
if($result )
{
header("location: ma_QualiOverview.php?message=success");
}
else
{
header("location: ma_QualiOverview.php?message=failed");
echo sqlsrv_errors();//Echo must be after header
}
$connection = null;

PHP script status bar

I have created a php script that submits a query and downloads the results in a CSV. I have created a bootstrap button where users can click to download the file. Some of the reports take longer to create and I wanted to show the user some sort of status that the script is running in the background.
I tried something simple like BlockUI from inside the PHP script. Using echo '<script type="text/javascript">$.blockUI();</script>'; in the beginning of the script and echo '<script type="text/javascript">$.unblockUI();</script>'; at the end but it didn't work.
Can someone help? I don't need a progress bar or anything fancy. I just need to show some type of status while the php script is running.
HTML:
...
<td class="pull-right"><a type="button" href="report1_download_csv.php" class="btn">Download</a></td>
...
PHP:
<?php
/* Set up and execute the query. */
$sql = "SELECT
FROM TABLE ";
$stmt = sqlsrv_query( $conn, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
foreach($row AS $key => $value){
$pos = strpos($value, '"');
if ($pos !== false) {
$value = str_replace('"', '\"', $value);
}
$out .= '"'.$value.'",';
}
$out .= "\n";
}
sqlsrv_free_stmt($results);
sqlsrv_close($conn);
// Output to browser with the CSV mime type
header("Content-type: text/x-csv");
$date = date('m-d-Y-His');
header("Content-Disposition: attachment; filename=Report1_{$date}_UTC.csv");
echo "Column1, Column2\n";
echo $out;
?>
use ajax to send request to server to do operation then show a spinner or any thing else untill server give you response.

Jquery .get call not returning anything

I've been at this for hours trying to figure out why this will not work. I am trying to read data from my mysql database using php and jquery. In my php file it loops through a table and should echo the results of that table. I am using the jquery .get method to try and retrieve it but i get a 404 error file not found. The 404 error goes away and everything works fine if i change my php file to something simple like:
<?php echo 'Hello'; ?>
I believe the problem is in my php file because when i create a new file with just echoing a string and nothing else it returns everything fine.
$(document).ready(function() {
$.get("php/listtasks.php", function(data){
alert("Data: " + data);
})
});
My php file where i think there is a problem contains:
<?php // sqltest.php
require_once 'session.php';
require_once 'login.php';
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if($conn->connect_error) die($conn->connect_error);
$query = "SELECT * FROM list";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[2];
}
$result->close();
$conn->close();
?>

Calling a stored procedure in phpmyadmin from website to check login details

I am trying to call a stored procedure in php-myadmin from wordpress website to check user login details.
I have a button that points to javascript function:
<input type="submit" value="Login" onclick="checkloging()">
The javascript file contains ajax:
function checkloging() {
$.ajax({
type: "POST",
url: 'checklogin_php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
}
The ajax code is to call a php file:
<?php
//Call the proc() procedure follow
$result= mysql_query("CALL checkpassword('username', 'password');") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
for($i=0;$i<=6;$i++)
{
echo $row[$i]."<br>";
}
}
mysql_close($con);
?>
which invokes a stored procedure in phpmyadmin.
Please can any anyone help with this? I am not not sure if this is a good approach and I'm not getting it to work.
This might help you.
Note: Use and read more about mysqli
<?php
//connect to database
$connection = mysqli_connect("hostname", "user", "password", "db");
//call and run the store proc
$result = mysqli_query($connection,
"CALL checkpassword") or die("Query fail: " . mysqli_error());
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
mysql_close($con);
?>

Sending JSON Data to Ext.js

I have the following PHP code that is capturing and encoding JSON data ready to be used by an Ext.js file:
<?php
$Query = "SELECT `Department`,`DepartmentHeadID` FROM `Department`";
$Result = mysql_query($Query) or die("Error 01: " . mysql_error());
while($r = mysql_fetch_array($Result))
{
// Create JSON Data:
$rows[] = $r;
echo $r[0];
echo "<br />";
}
$Data = json_encode($r);
echo "<hr />";
echo $Data;
?>
$Data returns "false" when I echo it out by accessing the file directly.
I am then trying to capture and use this data with Ext.js and until I can resolve this "false" issue I'm a bit stuck.
No PDO being used due to the server PHP version as this is not a production environment and it's running on an internal server.
Any help greatly appreciated.
you are encoding $r in JSON.
$r is false when your loop has finished.
Encode $rows instead ;-)

Categories

Resources