how to insert data to a mysql table using ajax (client side)
the client generates the data that should be stored in the database. I like to use ajax to save the data. can you please he开发者_运维技巧lp me?
I have used the following code in a JS file:
var queryString = "?hrefAdd=" + hrefAdd;
ajaxRequest.open("GET","ajax_request.php" ,queryString , true);
and this is the ajax_request.php
<?php
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("lln") or die(mysql_error());
mysql_query("CREATE TABLE links(
link_add varchar(50) NOT NULL,
PRIMARY KEY (link_add))")
or die(mysql_error());
$link = $_GET['hrefAdd'];
// Escape User Input to help prevent SQL Injection
$link = mysql_real_escape_string($link);
mysql_query("INSERT INTO links (link_add) VALUES ('$link')") or die(mysql_error());
?>
but nothing is inserted in the table.
You have to use server side code to store data in a database. Howerver you can use Ajax to send your data from a client to server and get it stored in database.
@nasi You can use PHP AJAX here to store the data which is given by user at client side. Following link shows how data is fetched from server side ..look at this link http://www.w3schools.com/php/php_ajax_database.asp
you can easily understand.Try to use insert query for your requirement.
You can easily accomplish this using jQuery's ajax function combined with a server side scripting language like PHP.
You would have a file, e.g. ajax.php, which will be POSTed to from the client using the jQuery ajax function.
If you are not familiar with the basic of AJAX and/or scripting languages, you should begin by reading some introductory articles and documentation.
Update: Based on your code, you really need to get back to learning the basics before anyone can help you here. You try to create a tbale every time the AJAX occurs?! Also, you just copy-pasted a javascript ajax line you saw somewhere without any context.. Of course it won't work. Please get back to learning the fundamentals before you try coding it..
精彩评论