justlukeyou Posted March 24, 2011 Share Posted March 24, 2011 Hi, I am trying to run two scripts on one page. When I use just one script on the page they work however when I place both scripts on the same page one of them disrupts the other script. This script prevents the other following script from working: ini_set('display_errors', 1); error_reporting(-1); { $query = "SELECT * FROM answers ORDER BY `aid` DESC LIMIT 0, 11"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $answer = $row['answer']; $aid = $row['aid']; echo " <div class='questionboxquestion'> <a href= 'http://www.domain.co.uk/test/easy/answer.php?aid=$aid' class='questionlink'>$answer</a> </div> <div class='questionboxnotes'> </br> </div> <div class='questionboxlinks'> <div class='questionboxcategory'> <div class='questionboxcategorytitle'> Category: </div> <a href= 'http://www.domain.co.uk/test/easy/furniture-category.php' class='questionanswerlink'></a> </div> <div class='questionboxanswerlink'> <a href= 'http://www.domain.co.uk/test/oeasy/index.php' class='questionanswerlink'>Answer</a> </div> </div> "; } <?php if($error) echo "<span style=\"color:#ff0000;\">".$error."</span><br /><br />"; ?> <label for="username">Username: </label> <input type="text" name="username" value="<?php if($_POST['username']) echo $_POST['username']; ?>" /><br /> <label for="password">Password: </label> <input type="password" name="password" value="<?php if($_POST['password']) echo $_POST['password']; ?>" /><br /> <label for="password2">Retype Password: </label> <input type="password" name="password2" value="<?php if($_POST['password2']) echo $_POST['password2']; ?>" /><br /> <label for="email">Email: </label> <input type="text" name="email" value="<?php if($_POST['email']) echo $_POST['email']; ?>" /><br /><br /> <input type="submit" name="submit" value="Register" /> Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/ Share on other sites More sharing options...
btherl Posted March 24, 2011 Share Posted March 24, 2011 Please post the script you get when you put them both together, and tell us what happens when you try to use that script. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191954 Share on other sites More sharing options...
justlukeyou Posted March 24, 2011 Author Share Posted March 24, 2011 Hi, The error is: Notice: Undefined variable: error Which refers to the first line on code in the second set: <?php if($error) echo "<span style=\"color:#ff0000;\">".$error."</span><br /><br />"; ?> The first set of code stops the first variable from the second set of code from working. Are there any processes to have two scripts working on the same page? Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191956 Share on other sites More sharing options...
DavidAM Posted March 24, 2011 Share Posted March 24, 2011 Actually, the first two lines of your "first set of code" turns on error reporting. So now you see an error in your second set of code. That error was there before you combined the two sets of code, you just did not have error reporting on and did not see the error. P.S. The solution is NOT to remove the error reporting. The solution is to fix the error. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191958 Share on other sites More sharing options...
justlukeyou Posted March 24, 2011 Author Share Posted March 24, 2011 I dont understand what you mean. If I split the two pieces of code between two pages they both work. When I combine them on one page they clash and the first set of code prevents the second set of code from working. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191963 Share on other sites More sharing options...
nethnet Posted March 24, 2011 Share Posted March 24, 2011 What DavidAM is saying is true. The second set of code always produces an error, because nowhere is the variable $error defined (at least, not in the scope of the code you have posted). The only reason it is showing an error when the two sources are placed in the same script is because the first set of code essentially says "Hey, if there is an error, let's go ahead and print it to the browser." Post the full code of the script so we can see where/why your $error variable isn't being set. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191964 Share on other sites More sharing options...
justlukeyou Posted March 24, 2011 Author Share Posted March 24, 2011 The other code is a header piece of code but what I dont understand is that when I use this code on its own it works. So why does it work when I dont include the first set of code? I thought I was using process of elimination to see where the problem is. And the second set of code stops working when I place the first set of code with it. The first set of code continues to work. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191966 Share on other sites More sharing options...
nethnet Posted March 24, 2011 Share Posted March 24, 2011 You aren't listening to what we are saying. The error is happening because the $error variable is not registered as having any value. Therefore, when you attempt to use it, PHP throws a notice to the browser (the lowest level of error). This notice is NOT displayed when you only run the second set of code by itself because, since it is only a notice, it is not displayed by default in PHP's settings. When you put the two sets of code together, it is displayed, because you have the lines: ini_set('display_errors', 1); error_reporting(-1); Which is essentially saying "If there are any errors at all, including miniscule notices, print them to the browser for the user to see." The first set of code isn't preventing the second set from working, it is merely telling the second set to print this notice of an undeclared variable to the browser. The important part of this is that the error is happening whether or not you include the first set of code. Like I said earlier, you are trying to use the $error variable without previously defining it in your script. Try adding this to your code after your error reporting statements: $error = FALSE; This will declare the variable for your script's sake, but it will still prevent your if clause from returning any error present. Edit: Place that boolean declaration as high in your script as possible, so that any code that may give $error a value later in the script will still do so without it being reverted back to FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191973 Share on other sites More sharing options...
justlukeyou Posted March 24, 2011 Author Share Posted March 24, 2011 Hi, I was listening I just wasn't understanding what you were saying. What I did whilst you were replying was to remove the error reporting. ini_set('display_errors', 1); error_reporting(-1); And now both pieces of code work. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191976 Share on other sites More sharing options...
nethnet Posted March 24, 2011 Share Posted March 24, 2011 Well, for sake of clarification, the scripts do not "work" anymore now than they did before. All you did by removing the error reporting was to tell PHP to not notify you of any notices. It is still happening in the background, you just can't see it. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191978 Share on other sites More sharing options...
justlukeyou Posted March 24, 2011 Author Share Posted March 24, 2011 Thanks, So how can I pinpoint the error if the code works? I have tried adding the error reporting to the second set of script but it still comes up with no errors. I didn't know a script could work but actually have errors! Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191980 Share on other sites More sharing options...
nethnet Posted March 25, 2011 Share Posted March 25, 2011 Put the error reporting lines back at the top of the script (and for future reference, it is helpful to always have these lines at the top of your scripts during development, and then remove them when you make your scripts public, or set their values to 0). Error reporting is a programmers best method of debugging. This will show you your original problem, an undeclared variable. Something like this is a rather unimportant matter when compared to things like parse errors and stuff like that, but nevertheless, if PHP is pointing it out, it's a good idea to fix it. If you put this at the top of your script, after your error reporting lines, the problem should be fixed. $error = false; Basically, the problem was that you were attempting to use your $error variable before you declared it and gave it a value. By adding that line, you declare it, so it will no longer produce the undeclared variable notice when you try to use it later. Additionally, it will still return false when you do use it in your if statement, which is what you want. Later, you will be able to add your own error checking code to the script, which should give $error a value such as "Invalid username" or whatever errors you need for your form (I'm assuming it is a registration script?) That way, $error will initially have a value of false, but this value will be changed to whatever error your script finds (if there is one). Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191983 Share on other sites More sharing options...
justlukeyou Posted March 25, 2011 Author Share Posted March 25, 2011 Hi, I do have this in the header PHP but I dont know why it isn't reading this. } else { $error = "Invalid username or password."; } Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191986 Share on other sites More sharing options...
nethnet Posted March 25, 2011 Share Posted March 25, 2011 It would only read that if there was an error in the username or password. If that 'else' statement isn't executed, then $error will never be declared. You can put $error = false in the 'if' part of that statement, so that no matter which way it goes, $error is being declared, and the notice will never be generated. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191987 Share on other sites More sharing options...
justlukeyou Posted March 25, 2011 Author Share Posted March 25, 2011 The problem is that its not reading any of the header script. Slowly but surely! Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191990 Share on other sites More sharing options...
nethnet Posted March 25, 2011 Share Posted March 25, 2011 Include the entire file here so we can see why there's a problem. Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191991 Share on other sites More sharing options...
justlukeyou Posted March 25, 2011 Author Share Posted March 25, 2011 I have checked this over again and there isn't an error. The first script is interfering with the second script. Is there anyway in which one script can interfere with another script? For examply, should I rename each query so it has unique queries of unique field names? Quote Link to comment https://forums.phpfreaks.com/topic/231642-one-script-disrupting-another-script/#findComment-1191995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.