dsa122870 2009-10-28 09:18
浏览 65
已采纳

使用javascript和php通过ajax运行MySQL查询

I was looking through some code on a project of mine and thinking about all the php pages that I call with ajax that just run a simple update or insert query and it made me think. What if I could essentially run an insert or update sql query from javascript.

assuming I am using the prototype javascript framework for ajax and php on the server side.

would this work?

js:

<script type="text/javascript">
// table is string containing table name
// fields is an array of field names
// values is an array of values
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 (";
    // purposefully used fields array in for loop so we get matching number of 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');  // connect to the mysql server and select database
    mysql_query($_POST['query']);
?>

Obviously this is a simple example, just interested to know if this would work and I could replace the lot of small php pages that are each running a separate query?

  • 写回答

3条回答 默认 最新

  • doudi7782 2009-10-28 09:19
    关注

    Don't do that!

    It will allow anyone to do what ever he likes with your database!

    He would be able to send any sql command to your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)