2012-04-07 71 views
1

我想在表單中根據從數據庫查詢返回的行數回顯一個值。繼續收到錯誤解析錯誤:語法錯誤,意外的T_ECHO,期待','或';'html格式的回聲變量

正如你可能會告訴我,我很新。任何人都可以幫助我回應變量?我知道$ num_rows返回的值是使用var_dump顯示的。由於

<? 

if($num_rows <= 10) { 

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/> 
</form>'; 
} 
if($num_rows >10) { 
echo '</br></br><form id="h2" class="rounded" action="4.php"  
target="_blank" method="post"/> 
<input type="submit" name="submit" class="button" value="11"/><BR> 
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/> 

</form>'; 
}?> 
+2

每當你得到你不懂語法錯誤,要做的第一件事就是開始刪除(或註釋掉)位的代碼,直到消息消失。您刪除的最後一件事可能是問題的原因。 – 2012-04-07 18:39:34

+0

另外,解析器會告訴你哪裏遇到了語法錯誤(源文件,行號和錯誤描述)。 – Raffaele 2012-04-07 18:48:22

+0

解析器識別第40行的錯誤,這是我的echo $ num_rows。仍然不理解爲什麼value =「'echo $ num_rows;'」在回顯的表單中不起作用...? – user1022772 2012-04-07 18:54:50

回答

2

在這兩個代碼塊中,您重複命令echo而不是連接輸出或使用兩個語句。你已經這樣做了:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/> 
</form>'; 

這是一個語法錯誤。相反,你可以這樣做:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="' . $num_rows . '"/> 
</form>'; 

或本:

echo '</br></br><form id="h1" class="rounded" action="4.php" target="" 
method="post"/> 
<input type="submit" name="submit" class="button" value="10" /><br> 
<input type="text" name="number_of_tests" value="'; 
echo $num_rows . '"/>'; 
echo '</form>'; 
+0

工作。謝謝。一直把我逼瘋。仍然不確定如果我誠實,我明白它是如何工作的! – user1022772 2012-04-07 19:00:23

+0

echo some_string。 some_string。 some_string;被允許。什麼不是增加另一個回聲結束。一個回聲就是你所需要的。 – 2012-04-07 19:02:09

1

這是你應該使用連接字符串代碼並輸出結果

echo ' some value ' . $variable . ' other text '; 

echo函數輸出一個字符串,而點(。)運算符連接字符串。這是怎樣的錯誤代碼

echo 'value="'echo $num_rows;'"/>'; 

當你想插入這個變量的值的方式

$a_string = 'I\'m a string'; 
echo "I'm a double quoted string and can contain a variable: $a_string"; 

這適用於數組太

$an_array = array('one', 'two', 'three'); 
echo "The first element of the array is {$an_array[0]}" 

the PHP manual