Jump to content

Please help I am going to pull my hair out part two!!!!!


jackie11

Recommended Posts

Hi everyone,

Sorry its me again, just got help earlier with displaying my query (code below) and everything works great now apart from the picture which is not displaying, I've tried a number of things but I just can't manage to get it to display any advise? as I'm sure you can tell I am a beginner and just trying to get started with Mysql and php so any help would be great, I'm also looking to put an IF line in that if the surname search does not come up with a match it will bring up an error message

[code]

<html>
<head><title>Employee Index</title></head>
<body>

<h1><img src="etc/Logo_pic_2.JPG"></h1>


<?php


$search = "%" . $_POST["search"] . "%";
 

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("employee_index") or die(mysql_error());


  $query = "SELECT * FROM employee WHERE Surname LIKE '$search'";
  $result = mysql_query ($query)or die(mysql_error());
 

    if ($result) {
    while ($row = mysql_fetch_array ($result,MYSQL_ASSOC)) {
 

$Employee_ID = $row['Employee_ID'];
$Surname = $row['Surname'];
$Forename = $row['Forename'];
$Job_title = $row['Job_title'];
$Office_location = $row['Office_location'];
$Telephone = $row['Telephone'];
$Email = $row['Email'];
$Expertise = $row['Expertise'];
$Hobbies = $row['Hobbies'];
$DOB = $row['DOB'];
$Picture = $row['Picture'];

}
}
echo "<table cellspacing='15'>";
  echo "<tr><td colspan='20'><hr></td></tr>";
  echo "<th>ID</th><th>Picture</th><th></TD><th>Surname</th><th>Forename</th><th>Job Title</th><th>Office Location</th><th>Telephone</th>
  <th>Email</th><th>Expertise</th><th>Hobbies</th><th>DOB</th>";
  echo "<tr><td colspan='20'><hr></td></tr>";

      echo "
          <td>$Employee_ID</td>\n
          <td><td><a href='../Database/pics/{$row['Picture']}' border='0'>
            <img src='../Database/pics/{$row['Picture']}' border='0'
            width='90' height='100'></a></td>\n
          <td>$Surname</td>\n
            <td>$Forename</td>\n
            <td>$Job_title</td>\n
              <td>$Office_location</td>\n
              <td>$Telephone</td>\n
                <td>$Email</td>\n
                <td>$Expertise</td>\n
                  <td>$Hobbies</td>\n
                  <td>$DOB</td>\n
                   
          </tr>\n";
    echo "<tr><td colspan='20'><hr></td></tr>\n";

  echo "</table>\n";
 
?>

</body>
</html>


[/code]


I promise when I have ghot a handle on mysql etc I will try and help someone else!

Thanks again

Jackie
[quote author=jackie11 link=topic=123014.msg508009#msg508009 date=1169146665]
Hi 

Thanks for the reply, I tried that no difference!

any other suggestions  :-\

Jackie
[/quote]
i [i]think[/i] this is what you want to do
[code]
<html>
<head><title>Employee Index</title></head>
<body>

<h1><img src="etc/Logo_pic_2.JPG"></h1>


<?php
        /*put single quotes here outside your % symbols*/
        search = "'%" . $_POST["search"] . "%'";

        mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("employee_index") or die(mysql_error());

        $query = "SELECT * FROM employee WHERE Surname LIKE ". $search ."";
        $result = mysql_query ($query)or die(mysql_error());

        if(isset($result)){
                echo "<table cellspacing=\"15\"\n>".
                    "\t<tr>\n".
                    "\t\t<td colspan=\"20\"><hr></td>\n".
                    "\t</tr>\n".
                    "\t<tr>\n".
                    "\t\t<th>ID</th><th>Picture</th><th></TD><th>Surname</th>\n".
                    "\t\t<th>Forename</th><th>Job Title</th><th>Office Location</th>\n".
                    "\t\t<th>Telephone</th><th>Email</th><th>Expertise</th><th>Hobbies</th><th>DOB</th>\n".
                    "\t<tr>\n".
                    "\t\t<td colspan=\"20\"><hr></td>\n".
                    "\t</tr>\n";

                while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
                        echo "\t<tr>\n".
                            "\t\t<td>". $row['Employee_ID'] ."</td>\n".
                            "\t\t<td><a href=\"../Database/pics/". $row['Picture'] ."\" border=\"0\">\n".
                            "\t\t<img src=\"../Database/pics/". $row['Picture'] ."\" border=\"0\" width=\"90\" height=\"100\"></a></td>\n".
                            "\t\t<td>". $row['Surname'] ."</td>\n".
                            "\t\t<td>". $row['Forename'] ."</td>\n".
                            "\t\t<td>". $row['Job_title'] ."</td>\n".
                            "\t\t<td>". $row['Office_location'] ."</td>\n".
                            "\t\t<td>". $row['Telephone'] ."</td>\n".
                            "\t\t<td>". $row['Email'] ."</td>\n".
                            "\t\t<td>". $row['Expertise'] ."</td>\n".
                            "\t\t<td>". $row['Hobbies'] ."</td>\n".
                            "\t\t<td>". $row['DOB'] ."</td>\n".
                            "\t</tr>\n".
                            "\t<tr>".
                            "\t\t<td colspan=\"20\"><hr></td>\n".
                            "</tr>\n";
                }
                echo "</table>";
        }
?>

</body>
</html>
[/code]

you should always escape strings like
[code]echo "Var: ". $var ."";[/code]

and always escape double quotes:
[code]echo "<font color=\"red\">red font</font>";

/*NOT*/
echo "<font color='red'>red font</font>";
[/code]

also, you really don't need to redefine your mysql_fetch_array variables.
[code]
$Employee_ID = $row['Employee_ID'];
$Surname = $row['Surname'];
$Forename = $row['Forename'];
$Job_title = $row['Job_title'];
$Office_location = $row['Office_location'];
$Telephone = $row['Telephone'];
$Email = $row['Email'];
$Expertise = $row['Expertise'];
$Hobbies = $row['Hobbies'];
$DOB = $row['DOB'];
$Picture = $row['Picture'];
[/code]

this [i]will[/i] get confusing when you're creating pages with a thousand lines of code. it's a good habit to not redefine them. especially if you're just calling them for a single while loop. it's not necessary. plus, if you reach an error, or something isn't showing up (like your image), you can be sure it isn't a misspelling of the redefined variable. if it is a spelling error, it will only be found in one place... you won't have to go back and find where you redefined it, and see if you misspelled it there too. hope this helps.
:)Hi boo_looly

Thanks for reply, I have made the changes you have sugested but it brought up an error on line 10 first so I put iline 10 back to the way I had it. I am now getting the message

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 '%finlay%' at line 1

not to sure what this means?

Jackie
[quote author=jackie11 link=topic=123014.msg508079#msg508079 date=1169151756]
:)Hi boo_looly

Thanks for reply, I have made the changes you have sugested but it brought up an error on line 10 first so I put iline 10 back to the way I had it. I am now getting the message

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 '%finlay%' at line 1

not to sure what this means?

Jackie
[/quote]

can you post line 10, along with a few lines of code before it and a few lines after it?
hmmmm, i know what the mysql error means, but i'm not sure why it's returning erroneous. you can diagnose these mysql issues by replacing $search with your own value. like this:
[code]
<?php
        /*instead of*/
        $query = "SELECT * FROM employee WHERE Surname LIKE ". $search ."";

        /*type in a known value in your database*/
        $query = "SELECT * FROM employee WHERE Surname LIKE valueinyourdatabase";
?>
[/code]

and see what happens. if it still brings up an error, then something else is wrong. if it doesn't bring up an error, then the cause of the error is the $search variable. try that and post what happens.
Hi

When I changed it to

[code]search = "'%" . $_POST["search"] . "%'";[/code]

I get the message

Parse error: parse error, unexpected '=' in C:\Program Files\xampp\htdocs\test_3\Search.php on line 10

So I changed it back


When I changed it to:
[code]/*type in a known value in your database*/
        $query = "SELECT * FROM employee WHERE Surname LIKE Finlay";
       
[/code]

I just get a blank screen with the image at the top of the page, I have pasted in my code again just to clarify

Thanks again for looking at this for me!


[code]<html>
<head><title>Employee Index</title></head>
<body>

<h1><img src="etc/Logo_pic_2.JPG"></h1>


<?php

      $search = "%" . $_POST["search"] . "%";

        mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("employee_index") or die(mysql_error());
       
       
        /*type in a known value in your database*/
        $query = "SELECT * FROM employee WHERE Surname LIKE Finlay";
       

        if(isset($result)){
                echo "<table cellspacing=\"15\"\n>".
                    "\t<tr>\n".
                    "\t\t<td colspan=\"20\"><hr></td>\n".
                    "\t</tr>\n".
                    "\t<tr>\n".
                    "\t\t<th>ID</th><th>Picture</th><th></TD><th>Surname</th>\n".
                    "\t\t<th>Forename</th><th>Job Title</th><th>Office Location</th>\n".
                    "\t\t<th>Telephone</th><th>Email</th><th>Expertise</th><th>Hobbies</th><th>DOB</th>\n".
                    "\t<tr>\n".
                    "\t\t<td colspan=\"20\"><hr></td>\n".
                    "\t</tr>\n";

                while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
                        echo "\t<tr>\n".
                            "\t\t<td>". $row['Employee_ID'] ."</td>\n".
                            "\t\t<td><a href=\"../Database/pics/". $row['Picture'] ."\" border=\"0\">\n".
                            "\t\t<img src=\"../Database/pics/". $row['Picture'] ."\" border=\"0\" width=\"90\" height=\"100\"></a></td>\n".
                            "\t\t<td>". $row['Surname'] ."</td>\n".
                            "\t\t<td>". $row['Forename'] ."</td>\n".
                            "\t\t<td>". $row['Job_title'] ."</td>\n".
                            "\t\t<td>". $row['Office_location'] ."</td>\n".
                            "\t\t<td>". $row['Telephone'] ."</td>\n".
                            "\t\t<td>". $row['Email'] ."</td>\n".
                            "\t\t<td>". $row['Expertise'] ."</td>\n".
                            "\t\t<td>". $row['Hobbies'] ."</td>\n".
                            "\t\t<td>". $row['DOB'] ."</td>\n".
                            "\t</tr>\n".
                            "\t<tr>".
                            "\t\t<td colspan=\"20\"><hr></td>\n".
                            "</tr>\n";
                }
                echo "</table>";
        }
?>

</body>
</html>
[/code]
change [code]$search = "%" . $_POST["search"] . "%";[/code] to [code]$search = "'%". $_POST['search'] ."%'";[/code]

pay close attention to the single quotes on the outsides of the %, and the singlequotes inside ['search']. not double quotes. try that and change this:
[code]$query = "SELECT * FROM employee WHERE Surname LIKE Finlay";[/code]

to this:
[code]$sql = "SELECT * FROM employee WHERE Surname LIKE ". $search ."";
$query = mysql_query($sql);
[/code]

now you can use:
[code]
while($row = mysql_fetch_array($query, MYSQL_ASSOC)){
/*yada yada yada */
}
[/code]
try that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.