So I have a basic php page and it calls another php page that contains my mysql query and output. Pretty simple. I am using a jquery refresh statement below to auto refresh the div that the query is in:
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#records').load('user_records.php').fadeIn("slow");
}, 30000); // refresh every 30 seconds
</script>
Here is my user_records.php file:
<?php
$username = $_GET['user'];
include('connect.php');
$did = array();
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
while($row = $result->fetch_assoc()){
$user_id = $row['id'];
}
$sql = "SELECT * FROM assign_did WHERE user_id = '$user_id'";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
while($row = $result->fetch_assoc()){
$did_id = $row['did_id'];
$sql1 = "SELECT did FROM did WHERE id = '$did_id'";
$result1 = mysqli_query($link, $sql1, MYSQLI_STORE_RESULT);
while($row1 = $result1->fetch_row()){
$did[] = $row1[0];
}
}
$did_arr = array();
foreach($did as $newdid){
$did_arr[] = '`extension` LIKE \'%'.$newdid.'\'';
}
echo "<div id='records'><h1 align='center'>Today's Transfers</h1>
<table cellspacing='0' cellpadding='0'>
<tr>
<th> Customer Name</th><th>Phone Number</th><th>Disposition</th><th>DID</th><th>Date Called</th>
</tr>
";
$sql3 = 'SELECT * FROM `vicidial_did_log` WHERE ('.implode(" OR ", $did_arr) . ')';
$result3 = mysqli_query($link, $sql3, MYSQLI_STORE_RESULT);
while($row3 = $result3->fetch_assoc()){
$caller_id_number = $row3['caller_id_number'];
$extension = $row3['extension'];
...echo the rows into rows and columns...
?>
So it refreshes user_records.php that has a div with id 'records'. That div performs the mysql query and echos is. My problem is that when it does the refresh, the query just goes blank. It refreshes the div, because I have a few things that echo/output before the query starts and starts outputting. Not sure what I am doing wrong here or what code you guys need to see. I hope someone can help. Thank you.
Related
So I am trying to do an history for my site but when i do the query select with the jquery variable it doesn't work. HERE is the table that shows the values from db and here is the pop up box that opens to show details but I want to show the values from each row that I click
Here is the jquery code:
var idocorrencia;
$(document).on("click","#listagem tr td a", function(e){
e.preventDefault();
idocorrencia = $(this).parent().attr("idlista");
$("#listagem caption").text($(this).text());
console.log(idocorrencia);
alert(idocorrencia);
$.post( "historico.php", { idoc: idocorrencia })
$.ajax({
method:"POST",
url:"historico.php",
data:{idoc : "idlista"},
dataType: 'json',
});
});
Here is the php:
$id = $_POST['idoc'];
$result = mysqli_query($conn, "SELECT id FROM ocorrencia where id=$id");
$row = mysqli_fetch_assoc($result);
$idoc = isset($_POST['idoc']) ? $_POST['idoc'] : $row['id'];
Try to do it like this:
if (isset($_POST['idoc'])) {
$id = $_POST['idoc'];
$result = mysqli_query($conn, "SELECT id FROM ocorrencia where id='" . mysqli_real_escape_string($conn, $id) . "'");
if($result!==false && mysqli_num_rows($result)>0){
$row = mysqli_fetch_assoc($result);
$idoc = $row['id'];
}
}
UPDATE
and here is the same script with prepared statements:
if (isset($_POST['idoc'])) {
$statement = mysqli_prepare($conn, "SELECT id FROM ocorrencia where id=?");
mysqli_stmt_bind_param($statement, 's', $id);
$id = $_POST['idoc'];
mysqli_stmt_execute($statement);
$result = mysqli_stmt_get_result($statement);
if ($result !== false && mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$idoc = $row['id'];
}
}
Here I have used procedural style, as the original script was like that. But it can be easily rewritten in object oriented style.
I'm trying to delete one line from a table, however and even following the examples on stackoverflow, I can't reach a solution.
One of the Important things before start, is that I have the url's hidden, so I don't see the Get url, even the splice don't delete the right line on the table.
HTML:
<button id="{{value.username}}" type='button' type="button" ng-click="delete(value.username, $index)" class="btn btn-primary">Delete</button></td>
JS
$scope.delete = function(deletingId, index){
console.log(deletingId);
$http.get("../admin/deleted.php?username=" + deletingId)
.success(function(data){
$scope.data.splice(index, 1);
console.log('dadasdas');
})
}
PHP
$id = $_GET ['username'];
$sql = "SELECT * FROM members";
$records = mysql_query($sql);
if(isset($_GET['username'])){
$id = $_GET ['username'];
$delete = "DELETE FROM members WHERE username= '$id'";
$res = mysql_query($delete) or die ("FAILED" .mysql_error());
}
Am I doing anything wrong ( the scope is working, the php src is correct, but even doing an echo on php it doesn't fulfill nothing) since I've the url's hidden is there anyway to make it as a post?
Thanks in advance
Try this (I will use Mysqli, because I don't use Mysql);
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$id = $_GET['username'];
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strtolower($id) == strtolower($row['username'])) {
$sql = "DELETE FROM users WHERE id=".row['id'];
$conn->query($sql);
}
}
} else {
echo "No accounts registered!";
}
So, deleting stuff with SQL, is case sensitive. So, I hope/think you have a auto increment id in your database. Then this will 100% surely work!
$from = $_POST['from'];
$to = $_POST['to'];
$message = $_POST['message'];
$query = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$fromID = $row['user_id'];
}
I'm trying to have $formID be the user_id for a user in my database. Each row in the Users table is like:
user_id | user_name | user_type
1 | Hristo | Agent
So I want $from = 1 but the above code isn't working. Any ideas why?
Try this:
$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);
$query = "SELECT * FROM Users WHERE user_name = '$from' LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$fromID = $row['user_id'];
}
Also, make sure that:
You have connected to the database
You do get data from the post, try var_dump with your vars eg var_dump($from)
Use mysql_fetch_assoc instead
while($row =mysql_fetch_assoc($result)){ $fromID = $row['user_id'];
}
though it should.
try this code
$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);
$query = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$fromID = $row['user_id'];
echo $fromID;
if it will throw no errors but still print no id, add this line
var_dump($row);
and post here it's output
not that you shouldn't use a user name but user id to address particular user.
please friends i want to run a javascript code to load a php page which is based on $_GET variable. here is the actual page:
<?php
$online = mysqli_query($con, "SELECT * FROM (SELECT user as user FROM online) as a INNER JOIN
(SELECT one as f1 FROM friends WHERE two='$my_id'
UNION SELECT two as f2 FROM friends WHERE one='$my_id') as b
ON a.user = b.f1");
while($query = mysqli_fetch_array($online)){
$active = $query['user'];
$first = getuser($active, 'first');
$nick = getuser($active, 'nick');
$last = getuser($active, 'last');
?>
<?php
echo "<font class='driver'>$first $nick $last</font>";
<div style="width:100%;height:82%;overflow-x:no;overflow-y:auto;" id="show">
<script type="text/javascript" src="jscript.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#show').load('active_chat.php')
}, 2000);
});
</script>
</div>
so i want this line $('#show').load('active_chat.php') to look something like this $('#show').load('active_chat.php?id=$active') so that it will display information entered by only a user for each $active, here is the active_chat.php;
<?php
include 'connect.php';
include 'functions.php';
$my_id = $_SESSION['user_id'];
$user = $_GET['id'];
$query = mysqli_query($con, "SELECT * FROM all_msgs WHERE (my_id='$my_id' AND user_id='$user') OR (my_id='$user' AND user_id='$my_id') AND delet !='$my_id' ORDER BY id DESC");
while($sql = mysqli_fetch_array($query)){
$msg = $sql['msg_body'];
$sender = $sql['my_id'];
$first = getuser($sender, 'first');
$nick = getuser($sender, 'nick');
$last = getuser($sender, 'last');
$photo = getuser($sender, 'photo');
echo $first." ".$nick." ".$last;
} ?>
In php, i have used an sql query to fetch the data like :-
$sql="Select * from xyz";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
error_log( "id" . $row["id"]);
}
Now i want to use all the values attained using $row["id"] in javascipt function and use it in a variable (var j , lets say). How to do this?
Try something like this:
<?php
$sql="Select * from xyz";
$errorsData = array();
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
error_log( "id" . $row["id"]);
$errorData[]=$row["id"]
}
$errorJson = json_encode($errorsData);
?>
<script>
var errorJson = <?= $errorJson ?> ;
console.log(errorJson);
<script>
Good luck!!
in jquery I would have done like this,
php
$sql="Select * from xyz";
$arr_json = array();
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
error_log( "id" . $row["id"]);
$arr_json[] = $row['id'];
}
echo json_encode($arr_json);
jquery
$.get(
<THAT CALLED URL>,
function(ret_data){
console.log(ret_data);
/* you can use the ret_data as ret_data.<INDEX> */
}
);