I am making a php mysql query, what I want is that actualize automatically each time you enter a data and not have to do it for the browser
this is the query in php:
<?php
include "conexion.php";
$consulta ="SELECT subject,tid,username FROM mybb_threads ORDER BY tid DESC LIMIT 15";
$resultado = mysql_query($consulta)
echo "<table border='0'><td>";
while($fila = mysql_fetch_array($resultado)){
echo " <div >
<a target='_parent' href='showthread.php?tid=".$fila['tid']."'> " . $fila["subject"] ."</a></td><tr><td>Por: " .$fila["username"]."</div><br>";
}
echo '</td></table>';
mysql_close($enlace);
?>
How could I do that automatically actualize?
regards
excuse is that I speak Spanish almost do not understand the English
if you update data automatically
If I understand you correctly, you want the query to be executed automatically each time there is new data in your table or database. Meaning, if you get new information in the table "mybb_threads", you want the user to see this new data without updating the browser.
To achieve this you need to work with Javascript, and something called Ajax. Bascally what you do is that every minute or whatever, you force the browser to call a php file.
You can read more here about the jQuery .ajax() function which is a great place to start:
http://api.jquery.com/jQuery.ajax/
You can also check out the Wiki that explain the method quite good:
http://en.wikipedia.org/wiki/Ajax_(programming)
Related
I'm using php to instantiate a number of divs depending on how many folders there are in a directory.
And each such directory has a number of pictures in there and I'm just getting the first one to set as image in the currently instantiated div.
<?php
$dir = "../assets/wallpapers/pc/";
$themes = array_diff(scandir($dir), array('.', '..'));
foreach ($themes as $t) { ?>
<?php
$tdir = $dir . $t;
$imgdir = $tdir . "/" . scandir($tdir)[2];
?>
<div class="div_5" onclick="select_theme('<?=$t;?>');">
<img src="<?=$imgdir;?>">
<div><span><?=ucwords($t);?></span></div>
</div>
<?php } ?>
I know it's a bit confusing but my concern is that on the div onclick event, I call a javascript function (select_theme) and as it's parameter I'm passing a php variable $t. The select_theme function basically gets the parameter and sends a ajax post to update the database. My concern is that when I go F12 on the browser I can change the select_theme parameter manually and therefore I'm scared that in that way the person would be able to type anything and the ajax would send whatever string the person wants.... and so on. Is this the proper way to do this?
SOLVED! Look at Nick's comment.
i have a php page where i press a button to edit on another page the mysql db records:
page name 'distintaBase.php'
here there is a list of names that are products name. Each product cointains a list of components.
When i press the edit button for one product i'm redirect to the new page 'modifDistBase.php'.
the $dist_base variable is passed to the new page. I use it on the page title
<h2>Modifica distinta base <?php echo $_GET['dist_base']; ?></h2>
and then to load the mysql db
<?php
$dist_base = $_GET['dist_base'];
$sql = "SELECT $dist_base.Id, $dist_base.Designator, $dist_base.Quantity, $dist_base.Description, $dist_base.Package,
$dist_base.Manufacturer, $dist_base.Pn_Manufacturer, $dist_base.Pn_Utterson, $dist_base.Mounted
FROM $dist_base";
?>
pressing ADD button i add a new component
if(isset($_POST['add_item'])){
$designator = $_POST['designator'];
$quantity = $_POST['quantity'];
$description = $_POST['description'];
$package = $_POST['package'];
$manufacturer = $_POST['manufacturer'];
$pn_manufacturer = $_POST['pn_manufacturer'];
$pn_utterson = $_POST['pn_utterson'];
$mounted = $_POST['mounted'];
$sql = "INSERT INTO $dist_base (designator,quantity,description,package,manufacturer,pn_manufacturer,pn_utterson,mounted)
VALUES ('$designator','$quantity','$description','$package','$manufacturer','$pn_manufacturer','$pn_utterson','$mounted')";
if ($conn->query($sql) === TRUE) {
echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';
} else {
echo "Errore: " . $sql . "<br>" . $conn->error;
}
}
there is something wrong probably on the javascript.
echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';
the new component is added to the database (for example with #5 id) but do not appear on the page modifDistBase.php
if i press refresh on the browser or f5 now i can see the new component (#5 id) but on the database a new one is added #6 with the same items as #5
PS
- header is already sent by menĂ¹ page
- have tried window.location.href with same result
Why don't you try with:
header('Location: modifDistBase.php?dist_base='.$dist_base);
instead of echo javascript you can redirect immediatelly to the page + the variable
there are some thing to fix on your code
Note the javascript you output with
echo '<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>';
will be precisely
<script>window.location.replace("modifDistBase.php?dist_base=" + $dist_base.value)</script>
Without checking the correctness of the js related to your goal, it's immediate to see you should write instead
echo '<script>window.location.replace("modifDistBase.php?dist_base="' . $dist_base . '")</script>';
to inject the php value into the javascript string.
(you've also forgot a double quote b.t.w)
If you have doubts regarding your javascript, you can inspect the page on the browser and look at it (right click and "inspect element" or "analyse element").
Mind it is not recommended that you take the user input and use it as it is in an SQL instruction. You should do some sanification and use a prepared statement or escape the values before putting them in the sql.
That said, the javascript redirection you do looks a bit odd, maybe you can redirect from the php script without asking the client to do so. Look at the header php function for instruction about redirection done server side.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
Actually, I have a javascript variable I pass this to PHP variable and use this variable as a MySQL query when I submit the query the page gets reloaded and value of the javascript variable is finished and therefore query doesn't work
I just want a modification in my code or there is another relevant solution regarding my problem then kindly please help me.
Everything works fine when echo the PHP variable it shows me the variable only problem is in the query of my SQL that in the query the PHP variable which has javascript variable is not working.
<script>
var buttontext="esteem";
<?php
$ff ="<script>document.write(buttontext);</script>";
?>
</script>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="beelist";
$conn=mysqli_connect($servername,$username,$password,$dbname);
error_reporting(0);
$connDB= mysqli_select_db($conn,'beelist');
if($_POST['sub'])
{
echo $ff;
$code=$_POST['Bid'];
if($code!=""){
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '$code' && name = '$ff'";
$data = mysqli_query($conn,$query);
$res1=mysqli_fetch_array($data);
if ($res1) {
echo '<script> alert("Beacon found")</script>';
echo '<script> showp();</script>';
}
else {
echo '<script> alert("Beacon ID is wrong!")</script>';}
}else{
echo '<script> alert("Beacon ID is required")</script>';
}
}
?>
As I said in the comments
Where do I start, Client and Server (JS and PHP) are separate. One runs on the server one runs on the clients computer. PHP goes first and as such only PHP can affect JS (as that is part of the output of the PHP) the JS's state cannot be known in PHP as it's on a entirely different computer. Basically you are left with making a request, either from a page reload (such as form submission) or AJAX where you can pass that data back to the server so it can work on it.
Basically what you have now is $ff literally is this text:
$ff ="<script>document.write(buttontext);</script>";
And because you don't echo it, it's actually never passed to the Client as part of the source.
Your query is like this:
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '$code' && name = '<script>document.write(buttontext);</script>'";
It's too broad of a topic for me to really give you a working answer, and there are plenty of tutorials out there that can do it better justice them me. But hopefully, you understand what is going on now.
PS. you can test this easily by doing echo $query; right after the query. Also be aware you should not put PHP variables directly in SQL, or you risk SQLInjection type attacks against your site. For example if $_POST['Bid']="' OR 1 -- your query would be this:
$query="SELECT beaconid FROM `customer` WHERE `beaconid` = '' OR 1 --' && name = '<script>document.write(buttontext);</script>'";
Where -- is the start of a comment in MySQL. So now I just selected your entire table by injecting SQL commands into your Query, which is a very bad thing.
Cheers!
So the main problem is, when i make some query just after a post[] or whatever and its just in the main code of the php (not in a function) it works perfectly fine..
But, when i try and have that query inside a php function,it never works, and kinda hiding any other code on the rest of the page... I have a few divs after that php and sql query code, and they just dont show when i try to query through the function..
<script type="text/javascript">
function callPhpVoteAdd(name) {
var result = <?php voteAdd(name); ?>;
alert(result);
return false;
}
</script>
<?php
echo '</br>';
$allsql = "SELECT * FROM voting ORDER BY votes DESC";
$result = mysqli_query($conn, $allsql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$name = $row["name"];
$votes = $row["votes"];
echo '<form method="post">
name: "' .$name. '" - votes: "' .$votes. '" <input type="submit" name="'.$name.'" value="'.$name.'" onclick="callPhpVoteAdd(this.name)"/><br>
</form>';
}
}
function voteAdd($name) {
if($conn->query("UPDATE voting SET votes = votes + 1 WHERE name = '".$name."' ") === TRUE) {
echo "<script>alert('fuck yeah');</script>";
}
echo "shit";
}
So the button pressed calls the js function, which calls the php function
I guess you're misunderstanding the way JS and PHP works. PHP works server side, and JS, in your case, client side. And so, PHP generate the script/page before it's sent to the browser, so when JS runs it, all the <?php ?> are already replaced by static data.
So, to put it shortly and simply, you just can't call a PHP function from JS after your page is loaded, the way you tried to (because the call isn't there anymore - check your source code from your browser).
You need either to :
use a form with an "action" attribute ; this will send the data to server, and call the php script set in "action", where you can handle the sent data, and then reload the page with the new/updated data,
use ajax (almost the same thing : this sends data to server and call a php script to handle them, but instead of reloading the page, the output of the PHP script - with the data you need - will be sent back to JS, to a callback function, where you'll be able to use them to modify the page).
Hello to this great community!
I've already learned a lot by reading many of the questions and answers here. So here is my current problem:
I'm currently creating a simple code which includes PHP, MySQL and jQuery for a simple news ticker. The aim is to check the database for changes without the need of reloading the whole webpage. If there is a change in the database a javascript is executed to reload the whole webpage. With this I want to avoid, that the user has to reload the webpage manually.
This works nicely!
But there is one problem: When too many users are on the webpage a "Too many connections"-Mysql-error occurs. I think this happens because of the many parallel running updates()-functions.
Do you have an idea how to optimize this code?
<script>
function updates(){
$('#updates').load('updates.php');
}
setInterval("updates()", 1000);
</script>
<div id="updates"></div>
updates.php
<?php
// Get latest value the database
$result = mysql_query("SELECT update FROM updates_db");
$row = mysql_fetch_object($result);
// Compare the value from the database with the current value which is saved in a session
if($_SESSION['update'] != $row->update) {
// If the values do not match, update the session and reload the whole webpage
$_SESSION['update'] = $row->update;
echo '<script>
location.reload();
</script>'
}
Try something like this instead of refreshing the whole page.
PHP
<?php
header('Content-type: application/json');
$result = mysql_query("SELECT update FROM updates_db");
$row = mysql_fetch_object($result);
if (($update = $_SESSION['update']) != $row->update) {
$_SESSION['update'] = $row->update;
echo json_encode(['update' => null]);
exit;
}
echo json_encode(['update' => $update]);
Javascript
$.post('updates.php')
.done(function( data ){
if( data.update){
//update DOM with new update
}
});
And increase your timeout interval. One request/1000ms is very frequent.