当所选数据不存在于数据库中时,PHP未定义变量 [英] PHP undefined variable when selected data is not present in the database
本文介绍了当所选数据不存在于数据库中时,PHP未定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是脚本:
我正在显示输入类型文本..
<tr><td>目标</td></tr><tr><td><input ID="txtobjective" name="objective" value="<?php echo "" .$objective.""; ?>"type="text"/><br></td></tr>
我认为这是一个常见问题,我什至无法解决.我刚从使用 PHP 编码回来.有人可以建议我处理此类问题的好方法.提前致谢.
解决方案
将 $objective='' 放在代码之前.您的变量尚未设置.
Well, my code works when the selected row exists in the database, it works perfect. But the problem is when I am selecting a data that doesn't exist at all. It gives me an error that my variables are undefined.
This is the scripting:
<?php
include('Nethost.php');
$patient = $_POST['patientid'];
$diagnosisid = $_POST['diagnosis'];
$pname = $_POST['patientname'];
$d = $_POST['diagnosis'];
$query = mysql_query("select objective from soapie WHERE patientid='$patient' AND subjective='$diagnosisid' LIMIT 1");
while($rows = mysql_fetch_assoc($query))
{
if($rows==NULL)
{
echo "No data";
}
else
{
$objective = $rows['objective'];
}
}
?>
I am displaying it input type text..
<table style="margin-left:auto; margin-right:auto">
<tr>
<td>Objective</td>
</tr>
<tr>
<td><input ID="txtobjective" name="objective" value="<?php echo "" .$objective. ""; ?>" type="text" /><br></td>
</tr>
</table>
I think this is a common problem and I can't even solve it. I just got back from coding using PHP. Can someone suggest me a good practice to handle this kind of problems. Thanks in advance.
解决方案
Put $objective='' before code. Your variable has not been set.
<?php
include('Nethost.php');
$patient = $_POST['patientid'];
$diagnosisid = $_POST['diagnosis'];
$pname = $_POST['patientname'];
$d = $_POST['diagnosis'];
**$objective='';**
$query = mysql_query("select objective from soapie WHERE patientid='$patient' AND subjective='$diagnosisid' LIMIT 1");
while($rows = mysql_fetch_assoc($query))
{
if($rows==NULL)
{
echo "No data";
}
else
{
$objective = $rows['objective'];
}
}
?>
这篇关于当所选数据不存在于数据库中时,PHP未定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文