警告:mysqli_real_escape_string()期望参数1为mysqli,字符串给定 [英] Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given

查看:1739
本文介绍了警告:mysqli_real_escape_string()期望参数1为mysqli,字符串给定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有一个安全区域,可以添加/更新/删除数据库条目。我正在尝试将脚本从mysql更新到mysqli。一切都有效,除了更新部分。当我填写新信息并点击更新时,它会给我这个错误:


警告:mysqli_real_escape_string()期望参数1到
字符串在
/home3/tarb89/public_html/aususrpg.net/charbase/updated.php在线19中给出


我不确定这里有什么问题;我是一个完整的新手,所以我很抱歉。



这是我的update.php代码:

 <? php 
//创建连接
$ con = mysqli_connect(xxx,xxx,xxx,xxx);

//检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();
}


$ id = $ _GET ['id'];

$ result = mysqli_query($ con,SELECT * FROM characters WHERE id ='$ id');
$ my_array = array($ c_z);
extract($ my_array);

?>

< form id =FormNameaction =../ charbase / updated.phpmethod =postname =FormName>
< table width =448border =0cellspacing =2cellpadding =0>

< tr>
< td width =150align =right>< label for =name>名称:< / label>< / td>
< td>< input name =namemaxlength =255type =textvalue =<?php echo stripslashes($ name)?>>< / td>
< / tr>

< tr>
< td colspan =2align =center>
< input name =type =submitvalue =Update>
< input name =idtype =hiddenvalue =<?php echo $ id?>>
< / td>
< / tr>

< / table>
< / form>

我对update.php页面的另一个问题是当它显示表时,输入应该是显示已在数据库中列出的信息;目前,它显示名称字段,但输入部分为空。它应该显示数据库中已有的名称数据(然后我可以将其替换为我想要更新的任何内容。)



这是updated.php代码:

 <?php 
//创建连接
$ con = mysqli_connect(xxx,xxx , XXX, XXX);

//检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();
}

$ id = $ _POST ['id'];

$ name = mysqli_real_escape_string(trim($ _ POST [name]),$ con);

$ rsUpdate = mysqli_query($ con,UPDATE characters SET name ='$ name'
WHERE id ='$ id');

if($ rsUpdate){echoSuccess updated; } else {die('Invalid query:'.mysql_error()); }
?>

< a href =index.php>返回索引< / a>

任何帮助将不胜感激。这件事让我疯狂。






不同的问题:见上面的update.php代码。



当我点击名为更新信息的数据库条目旁边的链接时,我需要更新.php?id = charactersid



目前,它显示一个空的输入表单。我可以输入信息并点击更新,它会正确更新。



然而,当我被带到update.php页面时,我希望它显示输入除了值之外的格式应该等于数据库中已有的值。



例如:



目前它是什么显示:



名称:[空输入框]



我希望它显示:



名称:[在数据库中列出的当前名称]



所以输入的值应该是该字符的信息;所以当我想要更新时,我可以看到已经列出的那个角色的信息,并且改变/删除/添加任何我需要的东西。



更有意义吗?

解决方案

你正以相反的方式使用它:

  string mysqli_real_escape_string(mysqli $ link,string $ escapestr)

所以它应该是:

$ p $ $ name = mysqli_real_escape_string($ con,trim($ _ POST [name]));

来源: http://php.net/mysqli_real_escape_string



由于您使用的是MySQLi,我建议您跳入准备好的语句而不是real_escape,像这样:

 <?php 
//您的数据库信息
$ db_host ='host' ;
$ db_user ='user';
$ db_pass ='pass';
$ db_name ='database';

// POST数据
$ id = $ _POST ['id'];
$ name = trim($ _ POST [name]);

$ con = mysqli_connect($ db_host,$ db_user,$ db_pass,$ db_name);
if($ con> connect_error)
{
die('Connect Error('。mysqli_connect_errno()。')'。mysqli_connect_error());
}

$ sql =UPDATE characters SET name =?WHERE id =?;
if(!$ result = $ con> prepare($ sql))
{
die('Query failed:('。$ con> errno。')'。 $ CON组>错误);

$ b $ if(!$ result-> bind_param('si',$ name,$ id))
{
die('绑定参数失败: ('。$ result-> errno。')'。$ result-> error);
}

if(!$ result-> execute())
{
die('执行失败:('。$ result-> errno。 ')'。$ result->错误);
}

$ result-> close();
$ con> close();
echo成功更新;
?>
< a href =index.php>返回索引< / a>

选择字符名称:

 <?php 
//您的数据库信息
$ db_host ='host';
$ db_user ='user';
$ db_pass ='pass';
$ db_name ='database';

// POST数据
$ id = $ _POST ['id'];

$ con = mysqli_connect($ db_host,$ db_user,$ db_pass,$ db_name);
if($ con> connect_error)
{
die('Connect Error('。mysqli_connect_errno()。')'。mysqli_connect_error());
}

$ sql =SELECT name FROM characters WHERE id =?;
if(!$ result = $ con> prepare($ sql))
{
die('Query failed:('。$ con> errno。')'。 $ CON组>错误);

$ b $ if(!$ result-> bind_param('i',$ id))
{
die('绑定参数失败:('。 $ result-> errno。')'。$ result-> error);
}

if(!$ result-> execute())
{
die('执行失败:('。$ result-> errno。 ')'。$ result->错误);
}

$ result-> store_result();
if($ result-> num_rows == 0)
{
die('找不到字符......');
}

$ result-> bind_result($ name);
$ result-> fetch();
$ result-> close();
$ con> close();
echo $ name;


I have a secured area of my site where I can add/update/delete database entries. I am trying to update the script from mysql to mysqli. Everything works except for the "update" part. When I fill in the new information and click "update", it gives me this error:

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in /home3/tarb89/public_html/aususrpg.net/charbase/updated.php on line 19

I'm not sure what is wrong here; I'm a complete newbie, so my apologies.

Here is my update.php code:

<?php
    // Create connection
$con=mysqli_connect("xxx","xxx","xxx","xxx");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$id = $_GET['id'];

$result = mysqli_query($con,"SELECT * FROM characters WHERE id = '$id'");
$my_array = array($c_z);
extract($my_array);

?>

<form id="FormName" action="../charbase/updated.php" method="post" name="FormName">
<table width="448" border="0" cellspacing="2" cellpadding="0">

<tr>
<td width="150" align="right"><label for="name">Name: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php echo stripslashes($name) ?>"></td>
</tr>

<tr>
<td colspan="2" align="center">
<input name="" type="submit" value="Update">
<input name="id" type="hidden" value="<?php echo $id ?>">
</td>
</tr>

</table>
</form>

The other issue I have with the update.php page is when it displays the table, the inputs should display the information already listed in the database; currently, it shows the name field but the input section is empty. It should display the name data already in the database (then I can replace it with whatever I want it to update as.)

Here is the updated.php code:

<?php
// Create connection
$con=mysqli_connect("xxx","xxx","xxx","xxx");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$id = $_POST['id'];

$name = mysqli_real_escape_string(trim($_POST["name"]), $con);

$rsUpdate = mysqli_query($con,"UPDATE characters SET name='$name'
WHERE id='$id'");

if($rsUpdate) { echo "Successfully updated"; } else { die('Invalid query: '.mysql_error()); }
?>

<a href="index.php">Back to index</a>

Any help would be greatly appreciated. This thing is driving me bonkers.


Different issue: see above update.php code.

When I click on a link next to a database entry, called "Update Information", it takes me to update.php?id=charactersid

Currently, it displays an empty input form. I can input information and hit "update" and it WILL update correctly.

However, when I am taken to the update.php page, I want it to display the input form except the values should all equal what is already in the database.

For example:

What it currently shows:

Name: [empty input box]

What I want it to show:

Name: [current name listed in the database]

So the value of the input should be the information for that character; so that when I want to update, I can see what is already listed as information for that character and change/delete/add whatever else I need to.

Does that make more sense?

解决方案

You're using it the opposite way:

string mysqli_real_escape_string ( mysqli $link , string $escapestr )

So it should be:

$name = mysqli_real_escape_string($con, trim($_POST["name"]));

Source: http://php.net/mysqli_real_escape_string

Since you're using MySQLi I would suggest you to just jump into prepared statements rather than real_escape, like this:

<?php 
// Your database info 
$db_host = 'host'; 
$db_user = 'user'; 
$db_pass = 'pass'; 
$db_name = 'database';

// POST data
$id = $_POST['id'];
$name = trim($_POST["name"]);

$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

$sql = "UPDATE characters SET name = ? WHERE id = ?"; 
if (!$result = $con->prepare($sql))
{
    die('Query failed: (' . $con->errno . ') ' . $con->error);
}

if (!$result->bind_param('si', $name, $id))
{
    die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}

if (!$result->execute())
{
    die('Execute failed: (' . $result->errno . ') ' . $result->error);
}

$result->close();
$con->close();
echo "Successfully updated";
?>
<a href="index.php">Back to index</a>

To select the character name:

<?php 
// Your database info 
$db_host = 'host'; 
$db_user = 'user'; 
$db_pass = 'pass'; 
$db_name = 'database';

// POST data
$id = $_POST['id'];

$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

$sql = "SELECT name FROM characters WHERE id = ?";
if (!$result = $con->prepare($sql))
{
    die('Query failed: (' . $con->errno . ') ' . $con->error);
}

if (!$result->bind_param('i', $id))
{
    die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}

if (!$result->execute())
{
    die('Execute failed: (' . $result->errno . ') ' . $result->error);
}

$result->store_result();
if ($result->num_rows == 0)
{
    die('No character found...');
}

$result->bind_result($name);
$result->fetch();
$result->close();
$con->close();
echo $name;

这篇关于警告:mysqli_real_escape_string()期望参数1为mysqli,字符串给定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆