Jump to content

One Script Disrupting Another Script


justlukeyou

Recommended Posts

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" />

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.