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

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.

Related

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.

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.

How do I PHP echo exactly what is stored in a MySQL field?

I feel like there is probably a very simple answer for this, but I've spent about 20 minutes searching and can't find anything.
Basically, I am using PHP to query a table and output the results as a list, using the primary key column (COL_1) of the table to create a link for each record that will bring the user to a detail page for that record. It works fine when the data in COL_1 is a straight-forward string such as "TEST". The edit link will then be detail.php?COL_1=TEST The detail page works by querying the database using the data passed by the link. So in this case it would do a select on the table where COL_1 = 'TEST' and return the correct record.
However, when new line characters are stored in COL_1 things get a bit complicated. For instance, if 'TEST\r\nTEST' is stored in COL_1, when the original query of the entire table is done, $row['COL_1'] for that line will give me 'TESTTEST', which gets passed to the detail page as detail.php?COL_1=TESTTEST, the detail page does a select on the table where COL_1 = 'TESTTEST', and it returns nothing.
However, if I manually link to detail.php?COL_1=TEST\r\nTEST the detail page will query on 'TEST\r\nTEST' and return the correct record.
So basically what I need is a way to do a query and have $row['COL_1'] return 'TEST\r\nTEST' instead of 'TESTTEST'. Does this make sense? How would I go about doing this?
As for why the table is set up like this, don't ask me. I didn't design it. I'd never design keys that can include line breaks like this. But I do have to interact with this table. Bah.
You should encode values that are passed in the URL:
echo urlencode("TEST\r\nTEST");
However, why would TEST\r\nTEST be a primary key? That's crazy. Maybe you need to rethink how you are doing things. Primary keys as integers work nicely.

Split data received into separate fields

Problem : Data supplied is not ok for me to handle directly.
Description : I'm using curl to get data I need from another website.
However the data supplied on this website is formatted in a way that I need to split it to be able to work with it.
Example:
I'm scraping a div (soccer competition), but the match is supplied in one field, I need to have the 2 teams separated into 2 fields.
MysqlDB Field holds after the scrape:
team 1 - team 2
I need to separate that DB info into 2 columns
preg_match would be able to do this, but there are many opponents, so its a hassle like this.
Right now, I added (manually which is a pain) a team_id column and added myteam to the games which involve my team to the db table and did a where team_id = myteam
This way I can at least sort the competition table and just get my teams games.
This is the data I'm talking about getting with curl:
$value['Wedstrijd'] = htmlentities($value['Wedstrijd'], ENT_QUOTES);
So right now I'm scraping the field and putting it in the db.
I'm wondering, how can I separate the content of $value['Wedstrijd'] into 2 writes.....
Is this even possible?
I'm not able to post the entire code here, the formatting gets screwed.
Using substr and strrpos I was able to get the first team out the field,
I thought setting it to -1 would give me the other way around, which it actually does, but it doesn't go back till the - symbol, it just gives me 1 letter/symbol then.
substr($wedstrijd,0,strrpos($wedstrijd,'-')); this would return the first team, but I'm not sure how to use this to get the team after the - symbol.
use strpos + substr or preg_match

AJAX response from database creates duplicate entries

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.

Categories