Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php在oracle数据库中插入数据_Php_Oracle - Fatal编程技术网

使用php在oracle数据库中插入数据

使用php在oracle数据库中插入数据,php,oracle,Php,Oracle,下面的代码正在生成此 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=$_PO

下面的代码正在生成此

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);





?>

如果看不到生成的SQL是什么样子、您正在发布什么字符集以及数据库正在使用什么字符集,很难说

将未过滤的用户内容拼接到SQL语句中并将其发送到DB会导致灾难。虽然PHP中的其他DB API有一个转义函数,但IIRC不适用于Oracle—您应该使用数据绑定


C.

您需要在插入的
varchar
字段周围加上单引号(我假定是url\u名称、锚定\u文本和描述)。当前使用的单引号只是将这些值设置为字符串,但在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'
)
$sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) values ( 9, '$url_name', '$anchor_text', '$description')";
<?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
本文的主要内容似乎已经结束,但下面是一篇详细介绍如何在PHP中绑定变量的文章。您肯定希望这样做是为了1)SQL注入的性能和2)安全性

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

insert into URL
(
 Url_ID,
 Url_Name,
 Anchor_Text,
 Description
) 
VALUES
( 
 9,
 'My Name',
 'My Text',
 'My Description'
)
$sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) values ( 9, '$url_name', '$anchor_text', '$description')";
<?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
编辑

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

的部分,这是因为查询字符串中有未引用的引号字符。请尝试以下方法:

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

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

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

其次,在使用变量构造SQL查询之前,需要对其进行转义。任何POST变量中的单个“'”字符都会中断查询,从而导致Oracle出现无效语法错误


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

切勿将用户输入直接插入SQL。用于准备安全语句。作为一个副作用,这也将修复您得到的错误(这是一个引用错误)。代码看起来像

$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);

如果您仍在开始开发,我想建议您直接使用而不是
oci\uu
函数

您的上述代码可以使用以下方法重写:

insert into URL
(
 Url_ID,
 Url_Name,
 Anchor_Text,
 Description
) 
VALUES
( 
 9,
 'My Name',
 'My Text',
 'My Description'
)
$sql1="insert into URL(Url_ID,Url_Name,Anchor_Text,Description) values ( 9, '$url_name', '$anchor_text', '$description')";
<?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

@sayket:别忘了将答案标记为已接受,如果它解决了您的问题,请投票。thnkx但现在我看到它实际上不起作用了……它插入了{$url\u name}、{$anchor\u text}、{description},而不是从$\u post方法中分配给这些变量的值……@sayket:底部的那个应该起作用。另外,我在表单的信息中添加了关于转义单引号的额外信息。