Hi new user here and i've been browsing this forum about a week now,still new to php(learn about 3 weeks).I use 'search' function to find my related problem but couldnt find one so i decided to create a thread and hopefully i create in correct section.

Now,my problem is the chessboard I want to create couldnt display the pieces image(king,queen,horse etc) and I receive this error: "Notice: Undefined variable: square in C:\Apache2\Apache2\htdocs\PHPCHESS\drawboard.php on line 9"

not only line 9 but also line 22,26,30,34,38...etc basically the images of the chessboard pieces.

I already enabling "php_gd2.dll" to display images of gif.

Try to solve the problem and even google'ed with no luck,its been eating inside me for a week now.Any help to find cause of the error would be really appreciated,cheers.

<?php
include("constants.php");

// open connection to the server and selects the database
$link_id = mysql_connect("localhost",$username, $password);
mysql_select_db($dbase, $link_id);

// Create The Query And Specify the name of database to run query on
$query = "select * from chessboard where 'square = $square'";
$result = mysql_query($query);

if ($result){

  $numOfRows = mysql_num_rows ($result);
  for ($i = 0; $i < $numOfRows; $i++){

  $piece = mysql_result ($result, $i, "piece");

}
if ($piece == "wr"){
echo  "<img src=\"images/wr.gif\">";
}

if ($piece == "wn"){
echo  "<img src=\"images/wn.gif\">";
}

if ($piece == "wb"){
echo  "<img src=\"images/wb.gif\">";
}

if ($piece == "wq"){
echo  "<img src=\"images/wq.gif\">";
}

if ($piece == "wk"){
echo  "<img src=\"images/wk.gif\">";
}

if ($piece == "wp"){
echo  "<img src=\"images/wp.gif\">";
}

if ($piece == ""){
echo  "<img src=\"images/blank.gif\">";
}

if ($piece == "br"){
echo  "<img src=\"images/br.gif\">";
}

if ($piece == "bn"){
echo  "<img src=\"images/bn.gif\">";
}

if ($piece == "bb"){
echo  "<img src=\"images/bb.gif\">";
}

if ($piece == "bq"){
echo  "<img src=\"images/bq.gif\">";
}

if ($piece == "bk"){
echo  "<img src=\"images/bk.gif\">";
}

if ($piece == "bp"){
echo  "<img src=\"images/bp.gif\">";
}

else{
  echo mysql_errno().": ".mysql_error()."<BR>";
}
}
?>

    As the error message states, you're referencing a variable called $square but you didn't define it anywhere.

    If $square is supposed to come from external data (e.g. the query string, a form submission, etc.), then you have to use the appropriate superglobal array ($GET, $POST, etc.). See this manual page for more information: [man]variables.external[/man].

    Also, your SQL query isn't formed correctly either. Your WHERE condition contains a single string; if you instead meant to compare the column "square" to a string value, then the single quotes should be around the string value only. It's the same concept as in PHP... would you write this:

    '$variable = value here;'

    or this:

    $variable = 'value here';

    EDIT: Also note that user-supplied data should never be placed directly into a SQL query, else you'll be vulnerable to SQL injection attacks (and/or just plain SQL errors). Instead, it must first be sanitized with a function such as [man]mysql_real_escape_string/man.

    EDIT2: In addition, you aren't processing the SQL result set correctly. Here is where you retrieve information from all of the rows matched:

      for ($i = 0; $i < $numOfRows; $i++){ 
    
      $piece = mysql_result ($result, $i, "piece"); 
    
    }

    That loop will run, overwriting the value each time until the loop ends, leaving you with the last row. Instead, you should move the code that deals with each row inside the loop.

      This is how drawboard.php is included.
      This file is called chessboard.php

      <table background='images/animknot.gif' border='0' cellspacing='1' cellpadding='1'>
      
      <tr>
      <td width='25' height='25' align='right'><font face='arial'> 8 &nbsp; </font></td>
      <td width='25' height='25' bgcolor='tan'><? $square=a8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='#996633'><? $square=b8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='tan'><? $square=c8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='#996633'><? $square=d8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='tan'><? $square=e8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='#996633'><? $square=f8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='tan'><? $square=g8; include("drawboard.php");?></td>
      <td width='25' height='25' bgcolor='#996633'><? $square=h8; include("drawboard.php");?></td>
      </tr>
      etc
      etc

      This is the MySQL table 'chessboard'

      $tablename = "chessboard";
      $query = "CREATE table $tablename (
        id MEDIUMINT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
        square VARCHAR(2) NOT NULL, 
        piece VARCHAR(2) NOT NULL
      )";

      The PHP Chess script can be downloaded in a small ZIP from
      http://www.redlionwebdesign.com/phpchess.htm
      🙂

        halojoy;10950559 wrote:

        The PHP Chess script can be downloaded in a small ZIP from
        http://www.redlionwebdesign.com/phpchess.htm
        🙂

        Yep i downloaded the php chess script from that website,i had everything work; chessboard appear,chat message work etc,unless there is 'hidden error' but as for now the drawboard.php aka chess pieces gave me the error message as mention.

        Right now i'm trying to understand what bradgrafelman mean(im really new to php scene ")

        @ - do you able to make everything work and play the chessboard?

          Write a Reply...