Jump to content

[SOLVED] 2 Variables not defined == undefined index


Recommended Posts

 

Hi I have a problem with my website.

 

The original programmer has left out a default value for 2 variables. This results in 2 "undefined index" errors only when I directly link to the page without having visit the page where the variables are being created first.

The website has been made for me without any comments in the script which makes it more difficult for me to analyze. I do know what values I need as a default value.

 

In the page where I need to fix my problem I see:

if (!isset($_SESSION["CURRENCY"]))
{
	$_SESSION["CURRENCY"]=$_POST["currency"];
}

 

In the cookie I have on my computer I see the value 318 this is the value that I also want to have a s default.

 

I tried the code:

else (isset($_SESSION["CURRENCY"]))
{
	$_SESSION["CURRENCY"]= '318';
}

But I than get a parse error on the '{'.

 

Than there is another "undefined index" error which comes from an @require. In this code it says:

$sql="SELECT id,text FROM text_list WHERE id='".$text."' AND (language='".$_SESSION["LANGUAGE"]."' OR language=0) ORDER BY language DESC";
$qry=@mysql_query($sql);
$row=@mysql_fetch_array($qry);
$sql2="SELECT phrase FROM phrases WHERE id='".$text."' AND language='".$_SESSION["LANGUAGE"]."'";
$qry2=@mysql_query($sql2);
$row2=@mysql_fetch_array($qry2);
        return $row2["phrase"];

 

In this case I'm trying to specify a default value '1'.

 

If someone could point me out I would be very grateful.

 

Kind regards,

Sebstiaan

Link to comment
Share on other sites

@ signs can be good if you don't want the whole world knowing about your table name and parts of the SQL syntax.

Although its not good coding practice to do so, there are exceptions I feel.

 

You know how you solve that problem? Implement a database class with error handling functions, where you can choose to log the error, email the error and display to the user a friendly message of "we are sorry the database is having some issues, administration has been alerted."

 

Using it this way, you could have an error in the middle of the page and it would take you 5 days to figure out, shit I've been hiding the error and that is why no user was able to register on my site!

 

Just bad coding practice and will cause some massive head injuries.

Link to comment
Share on other sites

Yeah. I agreed. But usually that just happens to thick people. I personally think it is quite simple to spot a @ sign.

I would only ever put them in, in certain circumstances. Not in regular coding. Nevermind.

 

I will put it this way, there is a certain way to handle the @ signs and yes they do have their uses. But if he is going to use them on every query like that and not do an or die('error in mysq'); it is improper usage =)

 

To seb:

 

If you are going to continue using the @ signs like that I would suggest creating a function page that houses functions for query and fetch_array. Something like this

<?php
function query($sql, $debug=false) {
     if (!empty(trim($sql)) {
          $result = @mysql_query($sql);
          if (mysql_error()) {
                 if ($debug) 
                       echo 'SQL: ' . $sql . '<br />Error: ' . mysql_error();

                 return false;
         }

         return $result;
    }
    return false;
}
?>

 

Same with mysql_fetch_array. This way your code would look like:

<?php
$query = "SELECT * FROM table_name";
$result = query($query);
if (!$result) {
      echo 'We are experiencing a problem with the database, please notify and administrator.';
}
?>

 

Or if you are in testing phase:

<?php
$query = "SELECT * FROM table_name";
$result = query($query, true); // will no echo out the error if so.
if (!$result) {
      die('Please see the error above!');
}
?>

 

Anyhow, just some suggestions that will help out in the long run.

Link to comment
Share on other sites

Thank you all for your input and such quick response!

 

I did not know the @ was that bad, I can only say that I bought my website for more than 8000 USD and the quality and support that I got from that company sucks so badly that I wish I had bought a ready made solution. At the time I bought it I had no knowledge of php and that sucks because later I saw a much better solution ready made with a lot of comments in source etc, for just 99 USD (which only required some small changes in order to have it the way I wanted) it made me feel sick knowing that I spend a 100 times more for a bad solution. Now I just try to make the best of it now, thats all I can do :)

 

I'm grateful that there are people here on this forum willing to help people!

 

I would never recommend anyone to use the service of Hyperskins AB in Sweden.

 

Well that said I feel a little better now ;)

 

I will try the code I received from you and experiment a little more after diner (17:09 local time Sweden)

 

Kind regards,

Sebas.

 

Link to comment
Share on other sites

8,000USD? Shit you should of gotten a lot more for that type of money, wow. I've never charged more than $2,000 for a project....wow. Crazy man.

 

Well hope you can salvage most of that 8000 and get it working properly. Just glad it wasn't your code that was flawed. Wow.

Link to comment
Share on other sites

Yes its amazing, I had to spend like 2 weeks telling them which mistakes they had to correct, If you would see how bad they mad my administrator panel....

 

I asked them if dynamic links would be found by Google, no problem they said. Than I read an article from Google that they have problems with pages containing the & and ? symbols. There response was "Interesting article" did they implement the mod_rewrite? No I had to figure it out myself and with the help of forums of course. It was amazing! Even after reading the first chapter in a book about php for beginners I recognized the mistakes they made regarding security, well they fixed that part for me but I realy had to mail them a lot to get things done.

 

The only good thing out of this is that I have goatherd some basic php experience and have been able to improve some parts :)

 

The accomplishments of the mod_rewrite took me 6 months but now I know how to deal with it.

 

Yeah it sucks.... If I was rich it would not matter but I'm not :)

 

Thanks for your support!

 

Link to comment
Share on other sites

Yea, I love mod_rewrite. I use it for every site as I like to have all my non-static pages go through a central "processing" file. Just makes life a ton easier. It also makes the site a lot less scarier =)

 

Anyhow, if you have questions about the code I posted let me know, have fun with your PHP coding ventures.

Link to comment
Share on other sites

I used the code from papaface but still get the same error.

That probably means I need to change something else.

 

Instead of 318 I tried the value EUR (stands for Euro) but still the same code referring to the line with the code:

if (!isset($_SESSION["CURRENCY"]))
{
	$_SESSION["CURRENCY"]=$_POST["currency"];
}

 

The problem is that because I created a direct (static) link no post data is being received.

The two parameters that I miss is the currency and language selection.

There are 3 @ requires in this script but the first error message happens outside the require and happens I think because it expects to find the currency. The second error happens in one of the @requires and involves the language.

 

O' pasta is ready so I take a break :D

 

I will try to fix the problem in the @require with the elseif and see what happens :)

 

I will have

 

Link to comment
Share on other sites

The solution from Dough boy did not help for error number 2, however, i was able to solve it myself :)

 

The old code was:

$sql="SELECT id,text FROM text_list WHERE id='".$text."' AND (language='".$_SESSION["LANGUAGE"]."' OR language=0)

 

And the new code is:

$sql="SELECT id,text FROM text_list WHERE id='".$text."' AND (language='".$_SESSION["LANGUAGE"]."' OR language=1)

 

That solved my problem!

Thank you all for helping!

 

Kind regards,

Sebastiaan

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.