AJAX response from database creates duplicate entries - php

I am trying to build a simple forum where the questions wil be refreshed automatically . Although it is done ,I have two problems.
1 . Since I retrieve the last update from database,last entry is dulicated.
2 . The div where I put the AJAX response always refrshes .
How can I remove the duplicate entries and make it smooth forum ,just like in facebook comment page ?

If I understand your first problem, you're saying you are doing something like:
Take the user's input (i.e. the new entry)
Write this input to your database
Select all entries, including the one you just added
Because you are also separately adding the new reply to the div client-side or something the last entry always shows up twice (but doesn't get written to the database twice).
If this is the case you could solve the problem by either omitting the last entry from your MySQL query (e.g. SELECT * FROM entries WHERE id != (SELECT MAX(id) FROM entries)) or by simply refraining from processing the entry on the client's side.
Concerning your second problem:
Instead of fetching all entries and replacing all content inside the div with the new list of entries, try only appending the new entry with .innerHTML += ... or jQuery's .append() for example. This way you won't experience any 'flickering' or jumping content with the rest of the replies.

Related

inRandomOrder() query with ajax return same elements

I have a query with Laravel
$query = Work::inRandomOrder()->where('orientation', $request->get('orientation')->paginate(11);
And I have a "load more" button who call 11 others works with each click.
But, I would like display my data in a random order. But with this query, he repeats to me several data. So in my list of works, I have 2 or 3 times the same and obviously, it's not good. Is there a way with RandomOrder () to avoid duplicates?
Thank you
I believe data is randomized each time the script is loaded, including each time loading dynamicaly 11 more data fields. One way to achieve this it to randomize, store the data in a user's session variable and then get next elements from it. However this might be very heavy.
I would go to a some easily predictable mathemtics, which will look 'random' to the end-user. For example you can get each N-th entry of a query like 1-st, 5-th, 9-th, 13-th ... if N=4 and store just the number N in the user's session.

mysql - Query to display record just added

I have two pages. One is a form that I use to simply input data that will be sent to my database and the second page that actually takes the data inputted into the form and sends it to the database and is supposed to display the information that I've just added.
Everything works fine, however I'm struggling with the query slightly. What I need it to do is display all the information for the last data inputted to the database.
The query I currently have just displays the data with the highest ID:
$sql = "SELECT * FROM Results ORDER BY ID DESC LIMIT 1";
So as an example I would be left with the following information after completing my form:
Success! Data being saved:
ID = 900 Amount = 206 Date = 2016-12-26
This is obviously just showing the data with the highest ID, but since the ID and all the data fluctuates, I need it to just show the data that has just been inputted.
I came accross this: Query to select newly added records only. But I don't believe this soultion to be viable as the database is external and I don't want to be creating new tables.
I was thinking that it might be possible to assign a hidden value to each newly added record via the query. e.g. New 1, New 2, New 3 etc. Then printing the latest record for New. However, I couldn't find anything on how to do this.
Any help would be greatly appreciated!
You must use this method to have very correct value:
Input form must send to another file that do inserting (we call it here insert.php)
insert.php must insert the data after validation and after that you can fetch the last ID number from database. Depending on the method you are working with it can be different. for example if you are using PDO you can get it by PDO::lastInsertId
after getting the ID you need to forward it to the viewing or editing page. for example view.php?id=LastInsertId. This forward have some reasons:
Codes can be cleaner.
We prevent refresh and resend inserting. for example if you do inserting inside view.php and user hit F5 to refresh the page, The insertion happening again.
This is the whole idea. you can use this method for only one page:
page.php?do=new
page.php?do=insert
forward to the page.php?do=view&id=lastInsertID
why you trying to get just inputted data from database? you can do it using HTTP POST/GET method easily.just send data as parameters and show them in second page.
If you already have the data you are inserting, you don't need to run a query to get it back from the database again, you could just ensure that the query was successful and display the data directly. Anyways:
You can get the insert ID from the last insert using the MySQLi object. For example:
$sql = "<your insert statement>"
$conn->query($sql);
$last_id = $conn->insert_id; //Id of the row you just inserted
$sql = "SELECT * FROM Results WHERE id=$last_id";
This is assuming you do the insert in the same page that you display the result.

Passing wrong data for random elements with PHP+MYSQL+JSON+FBGRAPH

Hello there;
I'm making basic script - save values, then compare it, if it exists - show different class, everything works fine with 99% of entries. I click on button, jquery posts data to mysql, and after that - data has been saved - BUT - there are some of the elements that gets written in with elementid minus one value.
How my logic works:
JSON gets values from Facebook Graph - it got userID like - 19999;
It is now splitted in array;
Array is getting arranged by names;
And all userids are my button id to pass each userid to mysql;
If user gets clicked it gets written in db;
Then it shows up - if user is there, or not...
Problem
So i click on 99% everyting is working just perfect, can't see no problem;
But then there are some entries which get written in database with wrong userid.
JSON gives value = 1999 -> same for button id = 1999;
BUT It writes value into database = 1998 [?]
WHY?
All other entries are working just fine, but in all list, there are few expectations.
So i've been googling this problem, can't find any solution or reasoning.
SQL queries
SELECT * FROM checkin WHERE (eid = '".$eid."' AND suid = '".$suid."' )
Problem solved.
It was in Jquery, i was using
var id,
and variable was called with (+id), now with (id) everything works fine.

Get values from an auto-generated table (generated by dhtmlxgrid) and set it to a php variable

EDIT: Ok, i found a way to get the cell-content and make a javascript variable (via dhtmlxgrid functions, something like var leadID = mygrid.cells(id,1).getValue();).
Now the remaining question is how i can get this javascript variable to be available in php for my sql query. On first pageload, since one of the table-cells should display the amount of comments made for this customer.
I need a query like (query is made on first pageload, to feed a cell with data)
$query_entryNo = mysql_query("select count(*) as total from comment WHERE post_id = $leadID ");
Where $leadID is the UserID you see in column 2 on screenshot.
End Edit
probably can ignore the text below...
Hello, i have something i dont know the answer and cannot find a way to do it.
Some info first:
I have an autogenerated table that is build with the help of dhtmlxgrid plugin (javascript). The table that is generated has no class or ID, also not the cells themself and i dont want to mess around with the plugin-sourcecode, for various reasons. So the table will stay class- and id-less.
Every row in this auto-generated table displays a user with his userID, name, email and all that.
I need to fetch this userid for an sql query. I would need this already on first pageload, not after some post or something like that. One column will contain the amount of comments that one made for this user and this i do with sql/php, soo i need some way to get the right db-entry. Comments are linked to the userid, thats why i need the id to be included in the php code for sql-query.
How can i achieve this? Basically the question is how i can get the id shown in column 2 into an sql query.
The screenshot shows the ID in the table that i somehow have to have in my sql query.
Since this table is generated through javascript and the comments are stored in another table i cannot use the basic dhtmlxgrid functions to do this. I have to build around my own way.
Based on our Discussion in the comments I can give you this Solution:
Send Data from PHP to Javasript (at Site-Creation time on Server side): just add a echo "<script>var data=$data; </script>", if it is more complicated you have to convert it into JSON (google that).
To send data on-the fly from Javascript to PHP you need a PHP-side recieving page, that reads your POST-Data. To send the request just use jquery.post like this:
$.post("http://example.com/ajax.php", data);
This will send an asynchronous call to your server.
So in your case, you read the values from the table via Javascript, save them into an array, and prepare the data that it will be interpreted the right way by PHP-Post mechanism:
ajaxData = "";
for (int i = 0; i < tableData.length; i++) {
ajaxData += "data[" + i + "]=" + tableData[i] + "&";
}
$.post("ajax.php", data); //This will take the relative path to your site
You see, that POST data has to be encoded the same way as GET-data.
Hope I could help.

Getting trouble in assigning id value in database in php [duplicate]

This question already has answers here:
auto increment primary leaving gaps in counting
(5 answers)
Closed 7 years ago.
i am making s simple registration page using php and mysql database.
id column of Table is AUTO INCREAMENTED
Registration Process working well i.e., data is getting Inserted in database.
currently i have 3 records in my table.
like
Raghvendra
Surendra
mohit
Now My Problem is:
When i deleted The 3rd Record i.e
mohit
My database looks like
Raghvendra
Surendra
Now, When I add another Record, my table looks like
Raghvendra
Surendra
4.varun
What is want is like
Raghvendra
Surendra
varun
sorry for my english..Help me!
Don't worry about the gaps. Either you need a unique number for each person, in which case, reusing 3 is a bad idea (does it refer to Varun or Mohit?) or you're only concerned with the names, and you can use CSS (an ordered list) to number them on the webpage when they're displayed.
It's generally very useful to preserve the order in which people are added to a database, and trying to "fill in the gaps" prevents that.
Also, remember there's no guarantee that for every user you delete, leaving a gap, there will be a new user available to fill it.
The harsh answer is to stop caring what the id is (it's really just an internal unique identifier, so no need to show it to the user).
If you are displaying a listing to the user, just use an <ol> element.
echo '<ol>';
while ($row = $results->fetch_row()) {
echo '<li>' . $row->name . '</li>';
}
echo '</ol>';
That will render the results are you expected, but you really should just treat the id as an internal thing and not worry about its actual value.

Categories