必填字段的空函数

时间:2014-01-16 01:48:35

标签: php string

晚上好!

我一直在努力自己解决这个问题,但我不能。我知道这很简单,但我找不到它。该脚本有效,但是当它出现时会显示一些问题,你会忽略它们并引入数据并且它可以完美地运行。我想我需要一个if条款,但我不知道如何应用。我是新手,我非常感谢你的帮助。非常感谢!

这是问题所在:

Undefined index : busca in ...
Undefined variable busqueda 
Faltal error call to a member function fetch array on a non object in ....

我认为当页面打开时,代码运行为空,没有数据,这就是我接收这些消息的原因。谢谢你的帮助

<head>
<title>SISTEMA DE REGISTRO DE PRODUCTOS CTE</title>
<style type="text/css">
#enviar {
    text-align: right;
}
.letrasbody {
    font-family: Arial, Helvetica, sans-serif;
    color: #FFF;
    text-align: center;
}
.letra {
    color: #000;
    font-family: "Lucida Console", Monaco, monospace;
    text-align: center;
}
</style>
</head>

<body>
<?php

session_start();

require "conexiondb.php";
 ?>
<form name="busquedas" method="POST" action="busqueda.php">
  <table width="1142" border="0" align="center">
    <tr>
      <th colspan="2" bgcolor="#0033FF" scope="col"><p>&nbsp;</p>
        <p class="letrasbody">SISTEMA DE CONTRO Y REGISTRO DE PRODUCTOS Y SERVICIOS</p>
        <p class="letrasbody">CTE </p>
      <p>&nbsp;</p></th>
    </tr>
    <tr>
      <td width="491" bgcolor="#FFFFFF">&nbsp;</td>
      <td width="641">&nbsp;</td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF" class="letra"><p>Numero de orden o </p>
      <p>numero de cedula</p></td>
      <td><input type="text" name="busca" id="busca"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" id="buscar" value="Buscar" /></td>
    </tr>
  </table>
</form>


<?php
$busca = $_POST['busca'];
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    if (!empty ($_POST['busca'])){
        echo "Indique su busqueda";
    }
}
if ($busca!=""){ 
    $busqueda=$con->query("SELECT * FROM clientes C INNER JOIN producto P ON C.serial  = P.serial WHERE P.serial  like '%".$busca."%' or c.cedula like '%".$busca."%' " );

}

echo "<table border=1> 
    <tr>
    <th>Cliente</th>
    <th>Cedula</th>
    <th>Orden numero</th>
    <th>Email</th>
    <th>Barrio</th>
    <th>Telefono</th>
    <th>Producto</th>
    <th>Marca</th>
    <th>Almacen</th>
    <th>Dano</th>
    <th>Fecha de recepcion</th>
    <th>Tecnico</th>
    <th>Estado</th>
    <th>Sede</th>
    <th>Fecha de entrega</th>
    </tr>";

$rowColors = Array('#A6A6FF','#FFFFFF'); $nRow = 0;
while ($muestra=$busqueda->fetch_array()){
    echo '<tr style="background-color:'.$rowColors[$nRow++ % count($rowColors)].';">';

    echo '<td>' .$muestra['cliente'].'</td>';
    echo '<td>' .$muestra['cedula']. '</td>';
    echo '<td>' .$muestra['serial']. '</td>';
    echo '<td>' .$muestra['email']. '</td>';
    echo '<td>' .$muestra['barrio'].'</td>';
    echo '<td>' .$muestra['telefono']. '</td>';
    echo '<td>' .$muestra['producto']. '</td>';
    echo '<td>' .$muestra['marca']. '</td>';
    echo '<td>' .$muestra['almacen']. '</td>';
    echo '<td>' .$muestra['dano'].'</td>';
    echo '<td>' .$muestra['fecharecepcion']. '</td>';
    echo '<td>' .$muestra['tecnico']. '</td>';
    echo '<td>' .$muestra['estado']. '</td>';
    echo '<td>' .$muestra['sede']. '</td>';
    echo '<td>' .$muestra['entregaacliente']. '</td>';


}

mysqli_close ($con)

?>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

您正在通过整个页面混合代码,以便在发出POST请求时以及何时可能没有POST请求。

例如:如果未发出POST请求,则$_POST['busca']未定义,但您在检查POST请求之前放置了该行。 $busqueda的情况相同;该变量仅在发出POST请求时定义,但是当您使用它时,您不会检查该变量。

您应该在发出POST请求时以及何时不清楚地分隔代码。您还可以初始化变量并在使用它们时检查初始化值。

答案 1 :(得分:0)

这是因为当您首次加载页面时,不会向页面发布任何数据。您可以在尝试对数据执行任何操作之前添加此检查。

<?php
if (isset( $_POST['busca']) // if the data is posted
{
//all your code here.
}

答案 2 :(得分:0)

您的代码中有很多问题。

您必须在生成任何输出之前放置session_start()。它应该在文件的最开头。

在检查$busca = $_POST['busca'];之前指定$_SERVER["REQUEST_METHOD"] == "POST"。因此,当您显示表单时,您正在执行此分配,而不仅仅是在用户提交表单时。但$_POST['busca']未设定时间,因此您会收到有关Undefined index 'busca'的警告。

此测试是倒退的:

if (!empty ($_POST['busca'])){
    echo "Indique su busqueda";
}

当字段不为空时,您显示错误,但是当 为空时,您应该显示错误,对吗?

您只能在设置$busqueda时执行查询并指定$busca。但是后来你有了循环:

while ($muestra=$busqueda->fetch_array()){

即使未设置$busqueda。这就是为什么你得到Undefined variable $busqueda错误以及关于在非对象上调用fetch_array的错误(因为$busqueda未定义)。

您需要检查脚本的所有逻辑,并确保它仅在给出表单输入时执行用于处理表单的部分。否则它应该只显示空表格。

相关问题