Felipe
Felipe

Reputation: 11887

Why is PHP giving off a undefined variable message for this code?

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?

Upvotes: 1

Views: 1139

Answers (5)

Cerad
Cerad

Reputation: 48865

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.

Upvotes: 1

user1248752
user1248752

Reputation:

$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 
                }
            }
        }
    }
}

Upvotes: 0

travega
travega

Reputation: 8415

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.

Upvotes: 0

Jon Egeland
Jon Egeland

Reputation: 12613

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

Upvotes: 1

danielrsmith
danielrsmith

Reputation: 4060

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.

Upvotes: 3

Related Questions