Jump to content

Help with a really noobish problem please


andysm849

Recommended Posts

Hi I'm a real newbie to php and have to make a sort of dictionary to help a teacher out at school. Basically what I'm trying to do is have a database of words, their definitions, and sentences, and make it searchable. So I created the database, which is called "dictionary", with the table "word". The words are under the column "wordname" the definitions under "worddef" and their sentences are under "wordsent". Since I am such a beginner I got a php for dummies book, and used example which I kind of played around with to work for my database. My problem is with "$wordname =" right above "$query". So when I just put in "$wordname= one of the words in the database" for this, it displays the word and definition and sentence as it should. However since I am obviously not looking to display just one word, I created an html search box. So now I tell the searchbox to run "worddisplay.php" (the name of this program) and nothing comes up when I type a word that I know is in the database. So Ive deduced that it has to be something with the "$wordname" function that I mentioned earlier. Any idea whats wrong with my code, because its just coming up blank after I click search (the url shows that it went to worddisplay.php though), no error message or anything.

 

 <html>
<head><title>Definition</title></head>
<body>
<?php
   $user="-------";
   $host="localhost";
   $password="-------";
   $database = "dictionary";
   $cxn = mysqli_connect($host,$user,$password,$database)
          or die ("couldn't connect to server");
  if (isset($_POST['name'])) {
     $wordname = mysql_real_escape_string($_POST['name']);
  } else {
    //handle error.. missing word
  }
   $query = "SELECT * FROM word WHERE wordname='name'";
   $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
  
   /* Display results in a table */
   $wordname = ucfirst($wordname);
   echo "<h1>$wordname</h1>";
   echo "<table cellspacing='15'>";
   echo "<tr><td colspan='3'><hr /></td></tr>";
   while($row = mysqli_fetch_assoc($result))
   {
      extract($row);
      echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
      echo "<tr><td colspan='3'><hr /></td></tr>\n";
   }
   echo "</table>\n";
?>
</body></html>
  

 

The searchbox's code is:

 

 <head>
<title>Search</title>
</head><body>
<h3>Search Below</h3>      
<form name="name" action="wordDisplay.php"
method="get">
Search:
<input type="text" name="name">
<input type="submit" value="Search">
</form>        
</body>
</html>

 

I know that this is a really simple program and the answer to my problem is probably really obvious, but im a total noob like I said and any help would be greatly appreciated...

Link to comment
Share on other sites

in your code mate you get $_POST['name']

that is fine but your form says

<form name="name" action="wordDisplay.php" method="get">

the method="get" part tells your form to send get not post so you have to options you can change your form so it reads

<form name="name" action="wordDisplay.php" method="post">

or alter these lines to they will take a get var

  if (isset($_GET['name'])) {
     $wordname = mysql_real_escape_string($_GET['name']);

if you want your users to be able to bookmark there searches you should change the php code.

but if you want security you should change the form line.

because when using get the form results are in the url like

wordDisplay.php?name=word

this type of query is bookmark able but if you use post the url looks like this

wordDisplay.php

and the name=word will be sent anouter way if this is bookmarked it wont work.

 

Scott.

 

 

Link to comment
Share on other sites

Thanks for all the help so far, but unfortunatly it still refuses to work. Here's the code now:

<?php
/* Program: wordDisplay.php
*/
?>
<html>
<head><title>Definition</title></head>
<body>
<?php
  $user="------";
  $host="localhost";
  $password="--------";
  $database = "dictionary";
  $cxn = mysqli_connect($host,$user,$password,$database)
         or die ("couldn't connect to server");
if (isset($_POST['name'])) {
    $wordname = mysql_real_escape_string($_POST['name']);
} else {
   //handle error.. missing word
}
  $query = "SELECT * FROM word WHERE wordname='$wordname'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");

  /* Display results in a table */
  $wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='3'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='3'><hr /></td></tr>\n";
  }
  echo "</table>\n";
?>
</body></html>

 

and the search box:

<head>
<title>Search</title>
</head><body>
<h3>Search Below</h3>
<form name="inputword" action="wordDisplay.php"
method="post">
Search:
<input type="text" name="name">
<input type="submit" value="Search">
</form>
</body>
</html>

Link to comment
Share on other sites

what r u trying to do with these lines ???

while($row = mysqli_fetch_assoc($result))
  {
     extract($row);
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='3'><hr /></td></tr>\n";
  }


Link to comment
Share on other sites

That just displayed the data in separate collumns. My problem though is that its not displaying anything at all. When I just made $wordname = a single word it would display it properly so that's not the problem. I want to just get it working before I fix alignment issues...

Link to comment
Share on other sites

you have to have a php.ini file is the core configuration file for php.. normally its in one of two spots.. either in

 

c:\windows\php.ini

or

c:\windows\system32\php.ini

 

or its in

 

root\PHP\php.ini

 

 

once your in there find this "error_reporting" make that = to E_ALL

 

then find this "display_errors" make that = to On

Link to comment
Share on other sites

Okay so Ive had a change of plans. I realized that that program really didn't suit my needs and that I should use a program that was actually made to search. Anyways I found one online, and it seems to be working a heck of a lot better than my previous misguided program. It seems for the most part to be displaying results. But I just have one final problem; it only displays data from one field in the database, when i need to display the word, the definition, and it used in a sentence, aka 3 fields. The code is:

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","------","--------"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("dictionary") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from word where wordname like \"%$trimmed%\"  
  order by wordname"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["wordsent"];

  echo "$count.) $title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>

 

So I want to display the results in a table like form so something along the lines of my previous database:

$wordname = ucfirst($wordname);
  echo "<h1>$wordname</h1>";
  echo "<table cellspacing='15'>";
  echo "<tr><td colspan='2'><hr /></td></tr>";
  while($row = mysqli_fetch_assoc($result))
  {
     echo "<tr>\n
            <td>$worddef</td>\n
            <td>$wordsent</td>\n
           </tr>\n";
     echo "<tr><td colspan='2'><hr /></td></tr>\n";
  }
  echo "</table>\n";

When I try to interchange the stuff after "while($row = mysqli_fetch..." It just comes up blank and doesnt work at all. I know there is something wrong with this result statement but could someone tell me how to fix it? I feel I am soo close now.

 

Oh and about the php.ini: I searched my web server and couldn't find it, the directory where a phpinfo command said it existed doesnt exist, probably because its a shared server. Anyways I sent an email to the tech support, so hopefully Ill be able to access it tmrw to figure out my php errors.

Link to comment
Share on other sites

yep thats what I did. and it sent me to /usr/local/Zend/etc/php.ini which I couldnt access. The usr didnt even have a local folder in it. Thats why I think it was something to do with the shared server thing. I really do need to get a private server, its been a real hassle...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

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.