文本框中的未定义变量

时间:2014-01-27 18:17:59

标签: php html mysql

正如您所看到的,我的texbox中出现了错误。任何人都可以帮我解决这个错误吗?

文本框内的错误如下:

**

> aic:

**
<br /><b>Notice</b>:  Undefined variable: aic in <b>C:\xampp\htdocs\test\index.php</b> on line <b>34</b><br />
<br /><b>Notice</b>:  Undefined variable: aic in <b>C:\xampp\htdocs\test\index.php</b> on line <b>35</b><br />
<br /><b>Notice</b>:  Undefined variable: aic in <b>C:\xampp\htdocs\test\index.php</b> on line <b>36</b><br />
<br /><b>Notice</b>:  Undefined variable: aic in <b>C:\xampp\htdocs\test\index.php</b> on line <b>37</b><br />

**

> Name List:

** 
<br /><b>Notice</b>:  Undefined variable: name in <b>C:\xampp\htdocs\test\index.php</b> on line <b>41</b><br />
<br /><b>Notice</b>:  Undefined variable: name in <b>C:\xampp\htdocs\test\index.php</b> on line <b>42</b><br />
<br /><b>Notice</b>:  Undefined variable: name in <b>C:\xampp\htdocs\test\index.php</b> on line <b>43</b><br />
<br /><b>Notice</b>:  Undefined variable: name in <b>C:\xampp\htdocs\test\index.php</b> on line <b>44</b><br />

> **Address:**

<br /><b>Notice</b>:  Undefined variable: address in <b>C:\xampp\htdocs\test\index.php</b> on line <b>48</b><br />
<br /><b>Notice</b>:  Undefined variable: address in <b>C:\xampp\htdocs\test\index.php</b> on line <b>49</b><br />
<br /><b>Notice</b>:  Undefined variable: address in <b>C:\xampp\htdocs\test\index.php</b> on line <b>50</b><br />
<br /><b>Notice</b>:  Undefined variable: address in <b>C:\xampp\htdocs\test\index.php</b> on line <b>51</b><br />

php代码:

<?php
if(isset($_POST['code'])){
include('include/connect.php');

$batchcode = $_POST['code'];
$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode LIKE '$batchcode'");
while($rows = mysql_fetch_array($sql)){
    $aic[] = $rows['aic'];
    $name[] = $rows['name'];
    $address[] = $rows['address'];
}
}
?>

html代码:

Search Batchcode:<input type="text" name="code" id="query" /><br />
<form>
<table>
<tr>
<td>
aic: <br />
<input type="text" name="optA1" value="<?php echo $aic[0] ?>" /> <br />
<input type="text" name="optA2" value="<?php echo $aic[1] ?>" /> <br />
<input type="text" name="optA3" value="<?php echo $aic[2] ?>" /> <br />
<input type="text" name="optA4" value="<?php echo $aic[3] ?>" /> <br />
</td>
<td>
Name List: <br />
<input type="text" name="optB1" value="<?php echo $name[0] ?>" /> <br />
<input type="text" name="optB2" value="<?php echo $name[1] ?>" /> <br />
<input type="text" name="optB3" value="<?php echo $name[2] ?>" /> <br />
<input type="text" name="optB4" value="<?php echo $name[3] ?>" /> <br />
</td>
<td>
Address: <br />
<input type="text" name="optC1" value="<?php echo $address[0] ?>" /> <br />
<input type="text" name="optC2" value="<?php echo $address[1] ?>" /> <br />
<input type="text" name="optC3" value="<?php echo $address[2] ?>" /> <br />
<input type="text" name="optC4" value="<?php echo $address[3] ?>" /> <br />
</td>
</form>

我不希望我的文本框被循环,因为它们中的每一个都具有将来我的javascript使用的唯一名称。

2 个答案:

答案 0 :(得分:4)

通常当您尝试回显未定义的变量时会出现此错误,在这种情况下,这意味着您的代码未输入while循环,这意味着您的查询结果为为空

尝试:

if($sql) {
    while($rows = mysql_fetch_array($sql)){
        $aic[] = $rows['aic'];
        $name[] = $rows['name'];
        $address[] = $rows['address'];
    }
}else {
    $aic     = array('empty','empty','empty','empty');
    $name    = array('empty','empty','empty','empty');
    $address = array('empty','empty','empty','empty');
}

顺便说一下,我认为你想要这个查询并在%之前或之后错过$batchcode,你也可以试试这个:

$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app 
WHERE batchcode LIKE '%".$batchcode."%'");

或者这个:

$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app 
    WHERE batchcode = '".$batchcode."'");

答案 1 :(得分:0)

我看到你正在查看基于批处理代码的表格,所以如果你正在寻找那个确切的批处理代码,请使用=

$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode = '".$batchcode."'");