Jump to content
The Uniform Server Community

Undefined index:i on line 2

Heinz Stapff

Recommended Posts

I'm using ul element onclick javascript function to change div innerHTML with get mysql query but the innerHTML returns 'You have an error in your SQL syntax "undefined" at line 2'.


Onclick external.js Ajax

function getproduct(i){
if(i=' '){
document.getElementById('f1').innerHTML=' ';
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("f1").innerHTML=xmlhttp.responseText; //errors declared here

var jewelry= document.getElementById('jewelry').childNodes;   
for (i = 0; i < jewelry.length; i++) {   
if (jewelry[i].className == 'item') {  // excludes class="item disabled" 
jewelry[i].onclick = function() {   
alert(this.id+': Is sending your request.');  // this actually gets the right id
var i=this.id; // declared variable?
getproduct(i);//used variable returned undefined index?




$q=$_GET["i"];//undefined index i at line 2
$link=mysql_connect("localhost","root","root")or die(mysql_error( ));
mysql_select_db("bwi",$link) or die(mysql_error( ));
$query=mysql_query("select FROM* [B]'".$i."'[/B]")or die(mysql_error( ));
//undefined variable i line 7 syntax error on quotes?


As I'm new to Ajax and PHP I have to know how to define i and the corect syntax in the select FROM * statement. Any thoughts greatly appreciated :) :rolleyes:

Link to comment
Share on other sites

really look at what the error code says

You have an error in your SQL syntax


so it is not the ajax or the php it is the SQL statement you need to be looking at


in your sql you currently have "select FROM *" see anything wrong about that?

SQL is very logical - you are wanting to say select everything from the table or "select * FROM"

Link to comment
Share on other sites

megan Thanks, you're right. The 'i' still returns 'undefined index: i on line 2 and syntax error on line 7 ie

select * From '".i."'"

I'd guess that the ajax call is not getting the id of the li item clicked for the php $q=$_GET["i"]; as well as the wrong syntax for the quote double quotes shown above. Will have to get back and try again on the pc that has the server. Thank again for pointing out that simple syntax error.

Link to comment
Share on other sites


The new errors produced after corecting the select*FROM statement are as follows

Notice: Undefined index: i in D:\use_ide_1\UniServer\www\getproduct.php on line 2


Notice: Undefined variable: i in D:\use_ide_1\UniServer\www\getproduct.php on line 7

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''..'' at line 1


So I'm guessing the 'i' is not being defined which could be the javascript ajax call function or the onclick itself is not passing var i=this.id to getproduct() corectly. I'll have to post in javascript forum. :rolleyes:

Link to comment
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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