datatable:edit and delete data - php

i have a datatable which connected to DB...
i want make the data inside datatable can be edit and delete...
but of course,,after make change at datatable the data inside DB also change..
what's code to do that...??i've been try like this:
$("#datalist tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function(){
$(this.ntr).removeClass("row_selected");
});
$(event.target.parentNode).addClass("row_selected");
});
i'm newbie at datatable..that code is what i've been reach..and i dont know how to delete data..
1. i can edit/delete data after click at <tbody>
2. show option to edit/delete data
3. if choose delete show (are you sure to delete this data?)
4. the script connected to DB (so we can control DB data)
5. data at DB updated

Ok, I'm assuming you're using PHP and MySQL... You have to send a SQL query into your database with mysql_query(). Typically an easy way to do this is to go into phpMyAdmin, run the query you want on some debug data, then copy the generated php code:
also, please ignore the disparity in the id field... the idea should still be clear.
The highlighted text in step 2 should be your SQL query to send through PHP. Usually edits/selections/etc. will be shown above the table view in the Browse tab for the SQL query reference.
The next step is to (in best practice) define a $sql variable that contains your query. In this case, I'd put the highlighted text from step 2 into this variable:
$sql = "DELETE FROM faq_1278475018 WHERE id = 9 LIMIT 1";
Then with PHP, run the query: $result = mysql_query($sql);
That deletes things. Keep fiddling with phpMyAdmin and pay attention to the SQL query box to get clues on what SQL queries to run.
The easiest place I've found to follow is w3schools: PHP and MySQL Introduction
That should get you started...
To answer the comment:
You'll want to pass commands to your PHP script using $.post().The syntax is explained here: jQuery $.post()
You also might want to make sure the user is "logged in" in some fashion and that you've taken considerable security measures to ensure that someone doesn't manually post data to your script other than when you want them to.

Related

Executing PHP code stored in SQL DB

I've read a few posts about this that suggest using eval but then say how its dangerous (I don't know why) but I don't think it will work for my current need.
My issue is that I have a blog post db table which has a column that contains all the content of the post. In this content I want to include a poll that lets the user vote a or b. This requires a database call to get the current votes to display to the user after they have voted . I'm guessing javascript would be possible but then anyone could look at the source code and see the query I'm making to the database which is risky. I have the php code working fine I just need a way to process it after code has been obtained from the DB

My php script shows results as deleted, yet phpmyadmin shows results still

I wrote a script for someone where they can easily delete quiz results from a database. The general gist of the script is user enters a username, php script queries the database for quiz results pertaining to the username. Then displays results
User can click delete link. Once this link is clicked, the results are then deleted and the database is re-queried showing updated results.
Everything seems to work fine on this end until I log into phpMyadmin to see that none of the results were deleted.
Question is can this be something wrong with the database itself? Or PHPmyAdmin?
Is there something wrong with how I'm doing my script?
Here is my code in this text file: http://andreawine.ladev.co/code-example.txt
$query1 = "DELETE FROM aw_wpsqt_user_data WHERE quizID='".$_GET['quizID']." AND username='".$_GET['username']."'";
It appears that this line is missing a closing quotation mark around quizID. Try correcting it to be:
$query1 = "DELETE FROM aw_wpsqt_user_data WHERE quizID='".$_GET['quizID']."' AND username='".$_GET['username']."'";
I should also mention that this isn't exactly the safest way to delete information, since it appears that I could use SQL injection for most of these fields (especially considering these $_GET variables appear on the URI! Take a look at http://codex.wordpress.org/WordPress_Coding_Standards#Formatting_SQL_statements for WordPress's standards for SQL statements.

Generate an Output Based on Form Options

I've searched this relentlessly and I cannot find an answer to my quandary - I think the problem is that I'm not exactly sure WHAT to search for! Anyhow, my problem is this...
Scenario:
Imagine having a database of, for arguments sake, recipes and you wanted to give users the ability to generate a list of recipe suggestions based on various criteria.
In the form of a web page, I would want this to be a series of drop down boxes on the left. The user would select their filter options from the drop down boxes and, after pressing the submit button, would be presented with a list of recipes that fit the criteria.
So they could, for example select 'Meal Type', 'Calories', 'Cooking Time' and then (after hitting submit) get back a list of recipe suggestions that fit the bill.
(Ideally they would appear without the need for reloading the page and would be contained within a slider to browse through, but I can probably crack that part if I get the underlying part sorted...)
Requirement:
I just need to know - at a top level - which technologies I would use to achieve this (and the process of how they work together).
I'm guessing I'd need a MySQL dB with recipes that are tagged with criteria, then use the form and php to pull from the database. Is this correct?!
Seems like such a common requirement, but I can find no good reading on how to achieve this..
Take a look at the PHP guide to prepared statements. You'll be basically writing a select statement against the table where your data resides, with the where clause of the select statement being the parameters selected by your user in the form.
PHP Prepared Statements
The reason you want to stick to prepared statements is that they are generally more secure against attacks on your site via the form, using SQL injection.
For the end to end solution, your front end will submit to a PHP page which will then handle the criteria specified by the user, translating those into the prepared statement which will find the data from your table. The table itself will need to have columns which correspond to the criteria. That gets more into database design which is a much larger topic to cover here, however there are plenty of guides out there for that.
Really you want to break the solution down into the subcomponents, then find various guides out there to tackle the parts. Good luck, hope this helps. :)
The process:
1) User makes selection and clicks submit button. That will initiate a HTTP POST to a PHP page (can be the same PHP page) where you would collect these information.
2) Open a MySql database connection with PHP. You would need to know SQL to pull data from MySql DB. Depending on how your database schema is laid out.
3) Once you pull this information display them as a checkbox where user can click.
This is the bare minimum.
If you wanna get a bit more fancy you can use JQuery ajax post (http://api.jquery.com/jQuery.post/) so the page does not refresh.
Good luck and I'm already hungry. haha.
When the user selects options in your form and submits it, this will run a PHP script on the server. The server retrieves the option parameters, and uses them to construct a SQL query. For instance, the script might contain something like:
$criteria = array();
if (isset($_POST['calories'])) {
$criteria[] = 'calories < '.$_POST['calories']);
}
if (isset($_POST['time'])) {
$criteria[] = 'cooking_time < '.$_POST['time']);
}
...
$query = 'SELECT * FROM recipes WHERE '.implode(' AND ', $criteria);
You use a library like mysqli or PDO to perform the query and get the results. Then you loop through the results, and format them into an HTML table which will be returned to the user as the resulting web page.
Or the PHP script could return the results as JSON data. Your web page could use AJAX to get this data from the server, then use JavaScript to format it into a table.

PHP Do something on MySQL Row Added

I need a way to constantly (in a loop) check if a new MySQL row was added, and if so, do some thing with it, specifically send a notification to users that it pertains to, but I can handle that. I just need to know how to execute code when the number of MySQL rows changes.
You might wish to consider using a MySQL trigger on insert and/or delete:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://net.tutsplus.com/tutorials/databases/introduction-to-mysql-triggers/

Put mysql data into html table with edit buttons

I'm attempting to make a ticket system for out smaller company. I've made a submission form that saves to a mySQL database. I can save the data into the table and pull the date into a html table. I've been searching all day, however, to figure out how to put in a cell that will allow me to click a button, link, or whatever to change that row to completed. I have a field in the database for it, but I don't know how to get that cell into the table or how to get the link to understand which row I'm talking about. I've been searching Google for awhile and only getting how to make a html table with mySQL data or how to INSERT into a mySQL table.
You'll need 3 things: an UPDATE statement, the field in the row you want to update, and some unique way of identifying which row (entry) you want to update.
You'll probably want to create a link something similar to:
<a href='/path/to/update.php?id=myUniqueIdentifier'>complete</a>
and on the php page you'll want to use the $_GET['id'] passed (make sure to use msyql_real_escape_string()) to UPDATE your row and set your completed flag to whatever you want.
Lynda.com has a solid MySQL essentials video that has something very similar to what you are looking for. It would definitely be a good starting point for building your own custom solution. Or I'm sure you could just use their user interface to interact with mysql inside the browser.
I have no affiliation with Lynda.com - I just used their site to learn almost everything i know about programming.

Categories