Dear All,

I am currently working on a web assignment.
But I do not know what is wrong with this ^piece of code.
Can anyone of you find the bug in it please
;

<?
	$con=mysql_connect("localhost","root","");
	if(!$con){die("could not connect".mysql_error());}
	mysql_select_db("idareyou",$con);
	echo("dbconnect is working");

	$fname=a;//$_GET('search_fname');echo $fname;
	$lname=a;//$_GET('search_lname');
	
//	include("dbConnect.php");
	
	echo "blaljfkdjfjdfk";	
	echo $fname;
	
	$sql="SELECT fname,lname,email FROM user WHERE fname=$fname AND lname=$lname";
	$result=mysql_query($sql);
	
	if(mysql_num_rows($result)<1){
		mysql_close($con);
		echo "No result found";
	}
	 header('Content-Type: text/xml');
 $xml_name = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" ;
 $xml_name = $xml_name."<Nom>" ;

	while($rows=mysql_fetch_array($result) ){
		$xml_name=$xml_name."<Name>";
		$xml_name=$xml_name."<fname>".$rows["fname"]."</fname>";
		$xml_name=$xml_name."<lname>".$rows["lname"]."</lname>";
		$xml_name=$xml_name."<email>".$rows['email']."</email>";
		$xml_name=$xml_name."</Name>";
	}
	$xml_name=$xml_name."</Nom>";
	echo $xml_name;
	
	mysql_close($con);
?>
<html>
<body>
<? echo $fname; echo $lname; >
</body>
</html>

Looks like there's a few bugs in there...

1. You have commented out the filename so you're pointing to just 'a'. Not sure what you're trying to do here... This makes no sense! Also, you're trying to write three lines of code on one line... You should concatenate these lines

$fname=a;//$_GET('search_fname');echo $fname;
$lname=a;//$_GET('search_lname');

2. Your query variables should have single quotes around them like this:

$sql="SELECT fname,lname,email FROM user WHERE fname='$fname' AND lname='$lname'";

3. You have used incorrect quotes here:

$xml_name = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" ;

Change it to:

$xml_name = '<? xml version=\"1.0\" encoding=\"ISO-8859-1\";' ?>

4. You've missed the last closing php tag:

<? echo $fname; echo $lname; ?>

Those are the ones I saw straight away... there's probably more in there though

Thanks man.
I've corrected these mistakes but now I am left with this last error.
Can you please work it out.


XML Parsing Error: XML declaration not well-formed
Location: http://localhost/idareyou/searchUser.php
Line Number 1, Column 15:<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><Nom><Name><fname>a</fname><lname>a</lname><email>a@a.c</email></Name></Nom>
--------------^

note the arrow is just underneath the escape charater just after version=\

k, either try removing the double quotes completely from the version/encoding or if that still fails, try removing the escape chars, I'm not sure how necessary they are.

I've removed the escape characters.

thanks a lot .

The php code is working...thanks to you. I am using this php file with some basic ajaxing. But I do not why, it is not working, though it is accessing each method in it. Can you please figure out what is wrong in it. Thanks in advance.

var xmlhttp;

function showUserName(){

    var fname= document.getElementById("search_fname");//alert("fname is " + fname);
    var lname= document.getElementById('search_lname');//alert("lname is "+ lname);
    if(window.XMLHttpRequest){
        //code for ie7+,firefox,chrome,opera,safari
        xmlhttp=new XMLHttpRequest();
    }
    else{
        //code for ie6,5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(xmlhttp==null){
        alert("cannot create XMLHTTP instance");
        return false;
    }

    var url="searchUser.php";
    url=url+"?search_fname="+fname+"&search_lname="+lname;

    xmlhttp.onreadystatechange=theResponse;

    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

function theResponse(){
        if(xmlhttp.readyState==4){

        if(xmlhttp.status==200){
            var tableName="<table><tr><th>First Name</th><th>Last Name</th><th>Email</th></tr>"; 
            var xmlDoc=xmlhttp.responseXML.documentElement;//refers to root position of the xml <Nom>

            var NomNo=xmlDoc.childNodes.length;
                for(var i=0;i<NomNo;i++){
                var namenode=xmlDoc.childNodes[i];//refers to Nom/Name
                tableName=tableName+"<tr>";

                var nameNo=namenode.childNodes.length;//refers to no of element under Name

                for(var j=0;j<nameNo;j++){
                    var Nam=namenode.childNodes[j];//1st object
                    var sibl=Nam.childNodes[0];//its value
                    if(sibl!=null){
                        tableName=tableName+"<td>"+sibl.nodeValue+"</td>";
                    }
                    else{
                        tableName=tableName+"<td>&nbsp</td>";
                    }
                    tableName=tableName+"</tr>";
                }
                tableName=tableName+"</table>";
                document.getElementById("div_searchname").innerHTML=innerHTML+tableName;
            }
        }
    }
        else{
            alert("there was a problem with the request");
        }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.