Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,153,813 members, 7,820,876 topics. Date: Tuesday, 07 May 2024 at 11:59 PM |
Nairaland Forum / Science/Technology / Programming / Variable Undefine (794 Views)
Declare And Initialize A Variable In Vb 6.0 / What Is A Typeless Variable? (2) (3) (4)
Variable Undefine by ajaxphp7: 5:20pm On Jun 26, 2010 |
Please Php Guru I need your help, I am working on a website and I am have this error.Undefined index: matricno. I don't know how to debug my coding <?php. pls help me |
Re: Variable Undefine by Beaf: 6:11pm On Jun 26, 2010 |
1. Check that $_POST['matricno'] is not null. Do something like echo $_POST['matricno']; Better still (recommended practise); extract($_POST); $matricno=$_POST['matricno']; $level =$_POST['level']; $semester =$_POST['semester']; //check that form variables are not returning null, otherwise don't even bother with the database if (isset($_POST['matricno']) && isset($_POST['level']) && isset($_POST['semester']) ) { //build select query $query= "SELECT * FROM student WHERE matricno LIKE '$matricno' AND level LIKE '$level' AND semester LIKE '$semester'"; //open database mysql_select_db($database_dj, $dj); $result = mysql_query($query, $dj) or die(mysql_error()); //query students affair database if(!($result=mysql_query($query, $dj))) { print("could not execute query!" } // end if mysql_close($dj); } else { echo “A required form variable isn’t set”; } 2. Check your connection string, it is failing, if your database isn't connected, which will also result in undefined variable messages. |
Re: Variable Undefine by ajaxphp7: 2:08pm On Jun 28, 2010 |
Thanks very much 4 your great contribution. I seem not to be having problem undefine variable again but, the coding only echo the form search field but not the whole fields I have in the Database I use echo in the body section of my html tags to call all values in the field that matches with the search fields; matricno, level, semester. It is displaying output input value in the search form not the value in the database and other fields are not retrieved. How do I write the echo statement to fetch other fields and their repective values in my database <?php thanks as I am waiting for your reply |
Re: Variable Undefine by Beaf: 5:58pm On Jun 28, 2010 |
I suspect there might be a mispelling issue or some form fields might be outside of the <form></form> area. For text fields and textareas it is actually more accurate to check for null values with empty(), rather than isset() I gave you earlier. To eliminate issues with your fields returning empty even before yo call the databse, debug your fields like so, add the code below just above your databse code; function errorMessage($elementName) { return "form variable, " . $elementName . " has not been set or is spelled wrongly "; } if(!empty($matricno)) { echo $matricno; } else { echo errorMessage($matriano); } if(!empty($semester)) { echo $semester; } else { echo errorMessage($semester); } if(!empty($level)) { echo $level; } else { echo errorMessage($level); } |
Re: Variable Undefine by ajaxphp7: 6:04pm On Jun 28, 2010 |
I want to thank beaf alot for the contribution. But what if the variable error is showing null |
Re: Variable Undefine by ajaxphp7: 6:19pm On Jun 28, 2010 |
PLEASE, BEAF WHAT DO U MEAN BY THE form variables are not returning null, otherwise don't even bother with the database |
Re: Variable Undefine by Beaf: 6:42pm On Jun 28, 2010 |
ajaxphp7: That would mean the variable has not been set. It would likely lead to issues with your database query result. ajaxphp7: Take your sql statement below; $query= "SELECT * FROM student WHERE matricno LIKE '$matricno' AND level LIKE '$level' AND semester LIKE '$semester'"; Now lets see what happens to it. . . I have assumed that each student must have a matric number, semester, level and name. Therefore, if all your form variables are empty ("", then the translated query becomes; SELECT * FROM student WHERE matricno LIKE '' AND level LIKE '' AND semester LIKE '' Obviously, the above will not be useful in the context you are trying to use it in. It should return exactly zero results, in which case going to the database was a waste and unecessary. The same applies to most other problems. In fact, you can avoid returning error messages in certain situations, say you were passing in a database table name into your sql (not adviced), for instance. |
Re: Variable Undefine by Beaf: 6:46pm On Jun 28, 2010 |
One thing that strikes me, is that you are using the LIKE clause in your sql without any wildcards. Is there any reason why you aren't using " = " instead? |
Re: Variable Undefine by ajaxphp7: 11:03am On Jun 29, 2010 |
Thanks for the contribution beaf. for the question for why I am using like and not "=" it is what I actually have in mind initially to achieve with it. but for now my query has changed bcause of the advice I have gotten from guru's like you and others. The major problem I am having now is on the last </html> close tag. The error reads, Parse error: parse error in line 50 [color=#990000][/color] which is pointing to the last html close tag. What are the likely cause of such error message. |
Re: Variable Undefine by Beaf: 3:33pm On Jun 29, 2010 |
^ No probs, but I am far from a guru. You are likely to have a typo, that is confusing the PHP parser engine. Make sure; 1. Your code is between <?php and ?> tags. Check those tags very carefully for typo's. 2. All code is between curly braces, ensure every { tag has a corresponding }. 3. If you are using short opening tags, make sure they are enabled on your server. It is best to avoid them totally <? might not be recognised, but <?php will always be. Hopefully the above should help. |
(1) (Reply)
King Of Cms...learn To Earn / This NOT CLEAR To Me! / Programming Shopping Cart Issue Wondering How They Do It
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23 |