2012-03-03 55 views
1

我使用的是PHP和MySQL。下面是代码...当我选择'国家'时,我应该得到该州的城市列表......但是,它并没有拉动任何城市......看起来'国家'值并没有推到查询城市......我做错了什么?如何修复它,如果你的变量不通过

谢谢!

</head> 

<body> 
<div id="wrapper"> 

<div id="header"> 

<h1>Tomorrow's Toys Today</h1> 
</div> 
<div id="subheader"> 
</div> 

<div id="leftmenu"> 

<?php 

    include("misc.inc");           // 11 


    $connection = mysql_connect($host,$user,$password)    // 13 
     or die ("couldn't connect to server"); 
    $db = mysql_select_db($database,$connection)     // 15 
     or die ("Couldn't select database"); 


[email protected]$_GET['state']; 

[email protected]$_GET['city']; 


@$quer=mysql_query("SELECT state FROM states"); 


echo '************' , $state; 

if(isset($state) and strlen($state) > 0){ 

@$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = $state "); 

}else{$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = $state"); }; 



echo "<form method=post name=f1 action='dd-check.php'>"; 
echo "<table> <tr><td>"; 



echo "State"; 

echo"</td><td>"; 

echo "<select name='state' onchange=\"reload(this.form) \"><option value=''>Select one</option>"; 

while($noticia = mysql_fetch_array($quer)) { 

if($noticia['state=']==$state){ 

     echo "<option value='$noticia[state]'>$noticia[state]</option>"."<BR>";} 

    else{ 

     echo "<option value='$noticia[state]'>$noticia[state]</option><br>";} 

} 

echo "</select></td></tr><tr><td>"; 


echo "City"; 

echo "</td><td>"; 

echo "<select name='city' onchange=\"reload(this.form) \"><option value=''>Select one</option>"; 

while($noticia2 = mysql_fetch_array($quer2)) { 

if($noticia2['city=']==$city){ 

     echo "<option value='$noticia2[city]'>$noticia2[city]</option>"."<BR>";} 

    else{ 

     echo "<option value='$noticia2[city]'>$noticia2[city]</option>";} 

} 


echo "</select></td></tr><tr><td>"; 

echo "<input type=submit value=Show></td></tr></table>"; 
echo "<input type=text name=state value=$state>"; 
echo "</form>"; 
?> 


</div> 

<div id="main"> 



</div> 


</div> 
+0

不要使用'无法抑制的错误@'。这只会使调试无法进行。 – PeeHaa 2012-03-03 01:15:51

回答

3

是$ _GET ['state']是一个字符串吗?如果是这样,你应该这样做:

$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = '".mysql_real_escape_string($state)."'"); 

字符串需要引号。诠释不。其他方式您应该清理输入。

另外,还有一些在抑制比使用@上的一切错误的更好的方法...... http://php.net/manual/en/function.error-reporting.php

+0

如果设置了,是不是$ _GET ['state']总是一个字符串? – Shoe 2012-03-03 01:13:30

+0

@JeffPigarelli请求超级全局变量总是字符串 – PeeHaa 2012-03-03 01:14:37

+0

@PeeHaa,谢谢。 – Shoe 2012-03-03 01:15:43

0
@$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = $state "); 

}else{$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = $state"); }; 

回声 “$状态”;会给你输出$ state 使用echo $ state;或回显“{$ state}”;

你的情况

@$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = ".$state." "); 
}else{$quer2=mysql_query("SELECT city FROM cities c, states s where c.state_id = s.state_id and s.state = ".$state.""); }; 
+0

感谢您的回答..我试过:echo“$ state”; echo“{$ state}”;没有向我显示状态..我也尝试了Karol和Qing提供的@ query2语句,并且都没有返回结果!还有其他建议吗?谢谢! – 2012-03-03 02:04:51

+0

当我选择状态后,我做了一个查看源并获取以下消息。我该如何解决它?我是PHP新手!谢谢.. – 2012-03-03 19:01:40

0
</head> 

<body> 
<div id="wrapper"> 

<div id="header"> `enter code here` 

<h1>Tomorrow's Toys Today</h1> 
</div> 
<div id="subheader"> 
</div> 

<div id="leftmenu"> 

<form method=post name=f1 action='dd-check.php'><table> <tr><td>State</td><td><select name='state' onchange="reload(this.form) "><option value=''>Select one</option><br /> 
<b>Notice</b>: Undefined index: state= in <b>C:\xampp\htdocs\Dummies\index.php</b> on line <b>62</b><br /> 
<option value='Virginia'>Virginia</option><BR><br /> 
<b>Notice</b>: Undefined index: state= in <b>C:\xampp\htdocs\Dummies\index.php</b> on line <b>62</b><br /> 
<option value='Maryland'>Maryland</option><BR><br /> 
<b>Notice</b>: Undefined index: state= in <b>C:\xampp\htdocs\Dummies\index.php</b> on line <b>62</b><br /> 
<option value='Washington DC'>Washington DC</option><BR><br /> 
<b>Notice</b>: Undefined index: state= in <b>C:\xampp\htdocs\Dummies\index.php</b> on line <b>62</b><br /> 
<option value='Colorado'>Colorado</option><BR></select></td></tr><tr><td>City</td><td><select name='city' onchange="reload(this.form) "><option value=''>Select two</option><br /> 
<b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\Dummies\index.php</b> on line <b>81</b><br /> 
</select></td></tr><tr><td><input type=submit value=Show></td></tr></table><input type=text name=state value=></form> 
相关问题