MySQL注意:未定义的索引:如何解决[重复]
This question already has an answer here:
I am creating two level dropdown select box with PHP, MYSQL and JQuery. Everything seems fine but when my parent category doesn't have child or if I doesn't select child it is giving me error and I can't understand what this error means and how to resolve. I need you guyz help.
Error:
Notice: Undefined index: f_child_cat in C:\xampp\htdocs\pathtofile\index.php on line 58
You selected 12 & You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Here is my front end code
<?php if(isset($_POST['submit'])){
$drop = $_POST['first_child_cat'];
$f_child_cat = $_POST['f_child_cat'];
echo "You selected ";
echo $drop." & ".$f_child_cat;
$query = mysql_query("SELECT * FROM qa_categories WHERE parentid = $f_child_cat")
or die(mysql_error());
echo '<ul>';
while($cats = mysql_fetch_array( $query ))
echo '<li>',$cats['title'],'</li>';
echo '</ul>';
}
?>
</div>
此问题已经存在 这里有一个答案: p>
-
”注意:未定义的变量“,”注意:未定义的索引“和”通知:未定义的偏移“使用PHP
28 answers
span>
li>
ul>
div>
我正在使用PHP,MYSQL和JQuery创建两级下拉选择框。 一切似乎都很好,但是当我的父类别没有孩子或者我没有选择孩子时它会给我错误,我无法理解这个错误意味着什么以及如何解决。 我需要你的帮助。 p>
错误:
注意:未定义的索引:第58行的C:\ xampp \ htdocs \ pathtofile \ index.php中的f_child_cat 您选择了12 &安培; 您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在第1行的''附近使用正确的语法 p> p>
这是我的前端代码 p> \ n
&lt;?php if(isset($ _ POST ['submit'])){ $ drop = $ _POST ['first_child_cat']; $ f_child_cat = $ _POST ['f_child_cat'] ; echo“你选择”; echo $ drop。“&amp;”。$ f_child_cat; $ query = mysql_query(“SELECT * FROM qa_categories WHERE parentid = $ f_child_cat”) 或者死(mysql_error) ()); echo'&lt; ul&gt;'; while($ cats = mysql_fetch_array($ query)) echo'&lt; li&gt;',$ cats ['title'],'&lt; / li&gt;'; echo'&lt; / ul&gt;'; } ?&gt; code> pre> div>
It means that $_POST['f_child_cat']
is not defined (doesn't exist). You should be using binded parameters to prevent against SQL injection and use mysqli_
or PDO
functions.
Since this is part of your query, I would recommend that you wrap your query like so:
if( isset($_POST['f_child_cat']) && !empty($_POST['f_child_cat']) ) {
... call db
}
else {
echo "You didn't select this"
}
$_POST['f_child_cat']
is not defined check if the value is present first before using it.
$f_child_cat = isset($_POST['f_child_cat'])?$_POST['f_child_cat']:false;
if ($f_child_cat){
$f_child_cat = mysql_real_escape_string($f_child_cat); // always escape
/// your query.
}
Note: mysql_* functions are depricated, use mysqli or pdo as an alternative.