1

I have this code. It's just for testing purposes, so you don't need to tell me to use parameter binding and prepared statements and PDO to avoid SQL Injection.

foreach($dd->getElementsByTagName("ReportItem") as $elmt){
    foreach ($elmt->childNodes as $node){
        if($node->nodeName==="ModuleName")
            $name = $node->nodeValue;

            if($result=mysqli_query($conn,"select * from technology_info where name = $name")){
                if(mysqli_num_rows($result)==0){

                    mysqli_query($conn,"insert into technology_info(id,name,tool_id) values(null,$name,'2')");
                    //ERROR: Undefined variable: name 

                }

            }
        }
}

This is what the code is meant to accomplish: if variable $name is a value that is already in the database, do nothing. Otherwise, add it to the database.

However, I'm getting an error message: Notice: Undefined variable: name in /var/www/teste/index5.php

I mean, the variable is there. Any idea what might be happening?

2
  • echo the variable after the $name = $node->nodeValue; to see what the output is Commented Mar 14, 2012 at 19:30
  • well as the if says $name is not always set. Commented Mar 14, 2012 at 19:31

5 Answers 5

3

Because $name is only being set if($node->nodeName==="ModuleName"). That if statement only applies to that one line, yet the code below it (the mysql statements) will continue to run regardless.

Sign up to request clarification or add additional context in comments.

Comments

1

$node->nodeValue is probably null, which sets $name to null. PHP would render that as undefined.

5 Comments

The if statement right above the assignment would prevent this.
Also, null is certainly not the same as "undefined"
@thetaiko that checks nodeName, not nodeValue, and PHPs looseness sometimes renders null as undefined.
my bad on the nodeName/nodeValue. You're correct there. But I will stand by my assertion that PHP will never render null as undefined.
@thetaiko I have had a couple cases where this happened. It's rare and depends on whether the variable is an object or not (and more stuff, I don't remember my exact case).
1

You left out a set of squiggly brackets

foreach($dd->getElementsByTagName("ReportItem") as $elmt){
foreach ($elmt->childNodes as $node){
    if($node->nodeName==="ModuleName")

    { // *** You left this out 

        $name = $node->nodeValue;

        if($result=mysqli_query($conn,"select * from technology_info where name = $name")){
            if(mysqli_num_rows($result)==0){

                mysqli_query($conn,"insert into technology_info(id,name,tool_id) values(null,$name,'2')");
                //ERROR: Undefined variable: name 

            }

        }
    }
}
}

Also going to need some quotes around $name in your query.

Comments

0

Well not always if $node->nodeName doesn't equal "ModuleName" then the $name variable never gets created. Try defining your $name variable outside your loops or even just outside your if statement.

Comments

0

$name is out of scope.

Try

foreach($dd->getElementsByTagName("ReportItem") as $elmt){
    foreach ($elmt->childNodes as $node){
        if($node->nodeName==="ModuleName"){
            $name = $node->nodeValue;
            if($result=mysqli_query($conn,"select * from technology_info where name = $name")){
                if(mysqli_num_rows($result)==0){
                    mysqli_query($conn,"insert into technology_info(id,name,tool_id) values(null,$name,'2')");
                    //ERROR: Undefined variable: name 
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.