PHP和MYSQL - (mysqli_num_rows)总是等于0

时间:2017-03-14 19:43:37

标签: php mysql

我总是收到消息"在数据库中存在已知的情况。"

但即使我把DB中存在的套装号码,它仍然提供相同的信息。如果我从脚本中删除此部分,最后的消息"更新确定"看起来好像发生了DB更新,但是当我检查数据库时,没有任何改变。

我想知道与DB的交互是否没有发生,由于某种原因我不知道它是什么。

表格是这样的:



<html>

<style>

::-webkit-input-placeholder  { color:#CDCDCD; }
input:-moz-placeholder { color:#CDCDCD; }
textarea:-moz-placeholder { color:#CDCDCD; }

</style>


<form name="saque" action="https://equadsaude.000webhostapp.com/bancodados_atualizar.php" method="POST">

<table>

<tr>
<td>Processo</td>         </tr>

<tr>
<td><input name="n1" placeholder="somente algarismos"></td>
</tr>

<tr>
<td>Valor total sacado</td>   </tr>

<tr>
<td><input name="n4" placeholder="00000.00"></td>
</tr>

<tr>
<td>Observações e Data </td> </tr>

<tr>
<td><input type="text" name="n3" ></td>
</tr>

<tr>
<td col span="3"><input type="submit" name="submit" value="Atualizar"></td>
</tr>
</table>
</form>
</html>
&#13;
&#13;
&#13;

档案.php是这样的:

<?php

$conectar = new mysqli("localhost","id1019345_dados_zzzz","xxxx", "id1019345_sobras") or die(mysqli_error());
$processo = $_POST[ 'n1' ] ;
$valor_sacado = $_POST[ 'n4' ] ;
$observacoes = $_POST[ 'n3' ] ;

//testind POST in DB
$teste = mysqli_query($conectar, "SELECT 'id' FROM 'Tab_Index' WHERE 'Processo' = '$processo' ");
while (mysqli_num_rows($conectar, $teste) == 0)
{
echo "<p>Suit informed doesn't exist in DB.</p>";  exit(mysqli_error());
}


//subtract
$sql_seleciona = mysqli_query($conectar, "SELECT 'Valor_sobra' FROM 'Tab_Index' WHERE 'Processo' = '$processo' ");
while ($query_row = mysqli_fetch_assoc($conectar, $sql_seleciona))
    {
    foreach($query_row as $key => $value)
                       {
                       $resultado = $value-$valor_sacado;
                       }
    }

//adding observations
$sql_seleciona2 = mysqli_query ($conectar, "SELECT 'Observacoes' FROM 'Tab_Index' WHERE 'Processo' = '$processo' ");
while ($query_row2 = mysqli_fetch_assoc($conectar, $sql_seleciona2))
    {
    foreach($query_row2 as $key => $value)
                       {
                       $resultado2 = $query2."/". $observacoes;
                       }

     }

//Update DB
$sql_alterar = mysqli_query($conectar, "UPDATE 'Tab_Index' SET 'Valor_sobra' =  '$resultado1', 'Observacoes' =  '$resultado2' WHERE 'Processo' = '$processo' ");

if  ( isset ($sql_alterar) )
{
print "<h3> Update OK </h4>\n" ;
}
else 
{ 
print "<h3> Error updating </h4>\n" ;
}


  ?>

DB有4列:id,Processo(BIGINT),Valor_sobra(DECIMAL 7,2),Observacoes(VARCHAR)。主机:localhost USERNAME:id1019345_dados_zzzz SENHA:xxxx DB:id1019345_sobras

3 个答案:

答案 0 :(得分:1)

看起来你的SQL无效。如果你检查mysqli_error()的值,你可能会看到一些东西。

您不应该在列名或表名周围加引号。 (你可以使用反引号。)

SELECT 'id' FROM 'Tab_Index' WHERE 'Processo' = '$processo' 

完全无效。

SELECT 'id' FROM Tab_Index WHERE 'Processo' = '$processo' 

将选择文字字符串'id'而不是id列

SELECT id FROM Tab_Index WHERE Processo = '$processo' 

这就是你想要的。

另请参阅上面关于SQL注入漏洞的说明以及此SO答案:

How to create a secure mysql prepared statement in php?

答案 1 :(得分:0)

删除'Tab_Index' .write周围的单引号`Tab_Index`。因为单引号内的变量被视为普通字符串

所以写下查询如"SELECT `id` FROM `Tab_Index` WHERE `Processo` = $processo" 等等......

答案 2 :(得分:0)

我同意@scaisEdge关于小心sql注入,因为直接将变量传递给查询没有被清理,因此会让你受到攻击。

根据该评论,我建议你看看PreparedStatements(http://php.net/manual/en/mysqli.prepare.php

 $conexion = new mysqli(...);
 $prepStmt = $conexion->stmt_init();

 $prepSql  = 'SELECT `id` FROM `Tab_Index` WHERE `Processo` = ?';
 $prepStmt->prepare($conexion, $prepSql);
 $prepStmt->bind_param('s', $proceso);
 $prepStmt->execute();

 $numResultados = $prepStmt->num_rows;

注意:虽然功能mysqli_num_rows存在,但documentation并未直接提及$mysqli_result->num_rows 的程序功能

最后,通过查看代码,我注意到你正在直接实现所有内容,我建议你研究一些框架,它可以帮助你抽象和简化所有这些功能,并为你提供更多的结构和可伸缩性。

您可以看一下的一些(尝试寻找最新版本

  • Laravel
  • 的Zend
  • 多尔康
  • Yii