I was looking through some code in my project and thinking about all the php pages that I call with ajax that just run a simple update or insert a request, and that made me think. What if I can essentially run the insert or update the sql query from javascript.
Assuming I'm using a prototype javascript framework for ajax and php on the server side.
will it work?
JS:
<script type="text/javascript">
function mysql_insert(table,fields,values) {
var sql = "INSERT INTO " + table + "(";
for(i=0; i<fields.length; i++) {
sql = sql + "`"+fields[i]+"`";
}
sql = sql + ") VALUES (";
for(i=0; i < fields.length; i++) {
sql = sql + "'"+values[i]+"'";
}
sql = sql + ");";
var par = 'query='+sql;
var ajax = new Ajax.Request('sql.php',{method:'post',parameters:par,onComplete:function(res) { }});
}
</script>
PHP:
<?php
include('db.php');
mysql_query($_POST['query']);
?>
Obviously this is a simple example, it’s just interesting to know if this will work, and I could replace many small php pages, each of which runs a separate request?
source
share