【问题标题】:Inserting data in oracle database using php使用php在oracle数据库中插入数据
【发布时间】:2011-01-08 07:34:07
【问题描述】:

下面的代码正在生成这个

Warning: oci_execute() [function.oci-execute]: 
ORA-00911: invalid character in F:\wamp\www\SEarch Engine\done.php  on line 17

代码是...

<?php
include_once('config.php');
$db = oci_new_connect(ORAUSER,ORAPASS,"localhost/XE");

$url_name=$_POST['textfield'];
$keyword_name=$_POST['textarea'];
$cat_news=$_POST['checkbox'];
$cat_sports=$_POST['checkbox2'];
$anchor_text=$_POST['textfield2'];
$description=$_POST['textarea2'];

$sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) 
    VALUES( 9,".'{$url_name}'.",".'{$anchor_text}'.",".'{$description}'.")";



$result=oci_parse($db,$sql1);
oci_execute($result);





?>

【问题讨论】:

    标签: php oracle


    【解决方案1】:

    如果不查看生成的 SQL 的样子、您发布的字符集以及数据库使用的字符集,很难说。

    将未经过滤的用户内容拼接到 SQL 语句中并将其发送到数据库是灾难的根源。虽然 PHP 中的其他 DB API 具有转义功能,但 IIRC 这对 Oracle 不可用 - 您应该使用数据绑定。

    C.

    【讨论】:

      【解决方案2】:

      您需要在要插入的 varchar 字段周围加上单引号(我认为是 url_name、anchor_text 和 description)。您当前拥有的单引号只是将这些值设为字符串,但在 Oracle 中,varchar 字段需要在它们周围加上单引号。试试这个:

      $sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) VALUES( 9,'".'{$url_name}'."','".'{$anchor_text}'."','".'{$description}'."')";
      

      我在任何地方都没有 PHP 来测试它,但这应该在你的值周围创建单引号。

      因为实际上您最终将在数据库上执行的 sql 看起来像这样:

      insert into URL
      (
       Url_ID,
       Url_Name,
       Anchor_Text,
       Description
      ) 
      VALUES
      ( 
       9,
       'My Name',
       'My Text',
       'My Description'
      )
      

      主要文章 Binding Variables in Oracle and PHP 似乎已关闭,但这里是 Google Cache Version,详细介绍了如何在 PHP 中绑定变量。您肯定希望这样做是为了 1) 性能和 2) SQL 注入的安全性。

      另外,我的 PHP 有点生疏,但看起来您也可以像这样执行原始查询语句:

      $sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) values ( 9, '$url_name', '$anchor_text', '$description')";
      

      编辑
      此外,您需要转义从表单变量接收的数据中可能存在的任何单引号。在 Oracle sql 字符串中,您需要将单引号转换为 2 个单引号来转义它们。请参阅标题为“如何插入包含引号的字符串?”的 here 部分

      【讨论】:

      • @sayket:如果答案解决了您的问题,请不要忘记将答案标记为已接受和/或投票。
      • thnkx 但现在我发现它实际上不起作用.....它正在插入 {$url_name},{$anchor_text},{description} 而不是从 $_post 分配给这些变量的值方法....
      • @sayket:底部的那个应该可以。我还添加了有关在表单信息中转义单引号的额外信息。
      【解决方案3】:

      这是因为您在查询字符串中有未加引号的引号字符。试试这个:

      $sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) 
        VALUES( 9,\".'{$url_name}'.\",\".'{$anchor_text}'.\",\".'{$description}'.\")";
      

      【讨论】:

        【解决方案4】:

        这里有一些问题。首先,变量不会被插入到用单引号括起来的字符串中。试试这个简单的脚本,看看我的意思:

        $a = 'hi';
        print 'Value: $a'; // prints 'Value: $a'
        

        对比

        $a = 'hi';
        print "Value: $a"; // prints 'Value: hi'
        

        其次,您需要先对变量进行转义,然后再使用它们来构造 SQL 查询。任何 POST 变量中的单个“'”字符都会中断您的查询,从而给您一个来自 Oracle 的无效语法错误。

        最后,也许最重要的是,我希望这只是示例代码?您正在使用未经过滤的用户输入来构建一个 SQL 查询,这使您容易受到 SQL 注入攻击。转义变量至少可以防止最糟糕的攻击,但您仍然应该进行一些验证。切勿使用“受污染”的数据来构建查询。

        【讨论】:

          【解决方案5】:

          切勿将用户输入直接插入 SQL。使用oci_bind_by_name() 准备安全声明。作为副作用,这也将解决您遇到的错误(这是一个引用错字)。代码看起来像

          $url_name = $_POST['textfield'];
          $anchor_text = $_POST['textfield2'];
          $description = $_POST['textfield3'];
          
          $sql = 'INSERT INTO URL(Url_ID,Url_Name,Anchor_Text,Description) '.
                 'VALUES(9, :url, :anchor, :description)';
          
          $compiled = oci_parse($db, $sql);
          
          oci_bind_by_name($compiled, ':url', $url_name);
          oci_bind_by_name($compiled, ':anchor', $anchor_text);
          oci_bind_by_name($compiled, ':description', $description);
          
          oci_execute($compiled);
          

          【讨论】:

            【解决方案6】:

            如果你还在开始开发,我想建议你直接使用AdoDB而不是oci_函数。

            您上面的代码可以使用AdoDB 重写,如下所示:

            <?php
            include_once('config.php');
            
            $url_name=$_POST['textfield'];
            $keyword_name=$_POST['textarea'];
            $cat_news=$_POST['checkbox'];
            $cat_sports=$_POST['checkbox2'];
            $anchor_text=$_POST['textfield2'];
            $description=$_POST['textarea2'];
            
            //do db connection
            $adodb =& ADONewConnection("oci8://ORAUSER:ORAPASS@127.0.0.1/XE");
            if ( ! $adodb )
            {
              die("Cannot connect to database!");
            }
            //set mode
            $adodb->SetFetchMode(ADODB_FETCH_BOTH);
            
            //data for insert
            $tablename = 'URL';
            $data['Url_ID'] = 9;
            $data['Url_Name'] = $url_name;
            $data['Anchor_Text'] = $anchor_text;
            $data['Description'] = $description;
            
            $result = $adodb->AutoExecute($tablename, $data, 'INSERT');
            if ( ! $result )
            {
              die($adodb->ErrorMsg());
              return FALSE;
            }
            //reaching this line meaning that insert successful
            

            在我上面的代码中,您只需要创建一个关联数组,以列名作为键,然后为正确的列分配值。数据清理由AdoDB 自动处理,因此您不必为每一列手动处理。

            AdoDB 是多数据库库,因此您只需在应用程序中进行最少的代码更改即可更改数据库引擎。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多