我制作了一个语音词典。用户输入一个单词并从我的sql数据库中获取相应的含义,它显示在屏幕上然后转换为语音。它工作正常。
我只想添加一个条件:如果用户输入了我的数据库中不存在的单词,那么它会输出消息"抱歉的单词找不到",并且音频中不应该播放任何内容。 目前,如果我输入一个不存在的单词,我将获得未定义索引的音频...等等...'
请告诉我在哪里添加if条件和添加内容 这是我的代码
<html>
<head>
<title>Word meanings</title>
<?php
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
if(isset($_POST['Submit1']))
{
$req=$_REQUEST['word'];
$strSQL = "SELECT * FROM dict WHERE word='$req'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
}
mysql_close();
?>
</head>
<body>
<form name="form1" action="lastry.php" method="POST">
<input type="text" name="word" value="<?php echo isset($_POST['word'])?$_POST['word']:''?>"x-webkit-speech/>
<Input Type ="Submit" Name ="Submit1" Value ="submit">
</form>
<?php
if($_POST)
{
?>
<p>Listen word
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($x);?>'" &type="audio/mp3" />
</audio>
</p>
<br><br>
<p>Listen meaning
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($y);?>" &type="audio/mp3" />
</audio>
</p>
<br><br>
<p>Listen synonym
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($z);?>'" &type="audio/mp3" />
</audio>
</p>
<?php
}
?>
</body>
</html>
答案 0 :(得分:0)
也许检查它是否有价值
if( isset($_POST) && !empty($x) )
答案 1 :(得分:0)
在此处添加计数器:
$foundwords = 0;
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
$foundwords++; // increase counter after each found word
}
然后在mysql_close()
之后检查该变量(身体内某处)
if($foundwords==0) {
echo 'sorry word not found';
... finish html etc ...
die();
}
答案 2 :(得分:0)
在$rs = mysql_query($strSQL);
之后,立即添加第$num_rows = mysql_num_rows($rs);
行,然后您可以:
if($num_rows > 0) {
/*Run while loop which you already have*/
}
else {
echo 'Sorry word not found';
}
答案 3 :(得分:0)
你应该这样做:
<?php
if (isset($_POST['Submit1']) {
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
$req=$_REQUEST['word'];
$rs = mysql_query("SELECT * FROM dict WHERE word='$req'");
while($row = mysql_fetch_array($rs)){
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
if (!isset($x)) { $All_Find = $req; } else { $All_Find = true; }
}
?>
<?php if ($All_Find == true) { ?>
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($y);?>" &type="audio/mp3" />
</audio>
<?php } else { echo "Sorry, the world $All_Find cannot be found :(";}?>
答案 4 :(得分:0)
未定义的索引消息主要是php.ini设置的结果。如果您关闭通知是警告错误级别。 php中不存在的关联数组成员的值始终为NULL(= false =&#39;&#39;)。
但是如果你想要确切,你可以像这样检查
// the condition evaluates to false if $row has no 'meaning' member
if($row['meaning'])
$z = $row['meaning'];
else
$z = "sorry word not found";