传参数不好的地方在于每次传进去参数以后,都要根据不同的状态然后在php页面写sql语句执行。可能是select ,或者是insert 或者是delete,

解决方案 »

  1.   

    那你再增加一个参数 用来表示 是否用 select 还是 insert 或是 delete
      

  2.   

    不管咋整都不能把SQL写JS里,那岂不是太容易SQL注入了
      

  3.   

    不管咋整都不能把SQL写JS里,那岂不是太容易SQL注入了
      

  4.   


    楼上的意思是我传到php界面根据参数
    组合出不同的语句 like <?php
      $par = $_POST['par'];
      switch($par[0])
      {
      case "s" select name from xx where xx ...组合成select查询
      case 'i' insert into tb_name values(xx,xx) ..组合 insert语句
      case 'd' delete from name where xx 组合 delete语句
      }
    ?>这样传递的参数太多。表名也需要传递。还要传递列名等等。很麻烦
      

  5.   


    我的 ajaxcommon.php实际上是这样设计的class AjaxCom extends conn
    {
    protected $_sql = null;
    protected $_conn = null;

    //构造函数
    public function __construct()
    {
    //没有接受到参数
    if(count($_POST["sql"]) == 0 || !$_POST['sql'])
    {
    echo 'error';
    exit;
    }
    else
    $this->_sql = explode("[~]",iconv("UTF-8","gb2312",trim($_POST["sql"])));
    $this->_conn = new conn();

    }
    //执行语句
    public function execute()
    {
    $msg = '';
    $this->_conn->autocommit(false);
    foreach($this->_sql as $value)
    {
    //查询 一般只有一条语句
    if(strtolower(substr(trim($value),0,6)) == 'select')
    {
    $rs = $this->_conn->fetch_one($value);
    }
    else
    {
    $this->_conn->query($value);
    $rs = $this->_conn->rows();
    }
    if(!isset($rs))
    {
    $msg = 'error';
    break;
    }
    }

    $msg = $rs;
    if(strtolower($msg) == 'error')
    //语句出错回滚事务
    $this->_conn->rollback();
    else
    //如果语句全部执行提交事务
    $this->_conn->commit();
    echo $msg;
    }
    }

    $ajax = new AjaxCom();
    $ajax->execute();