Trouble with a really simple project

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Trouble with a really simple project

Post by andysm849 »

Hi I'm a real newbie to php and have to make a sort of dictionary for a school project. 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 about "$query". So when I just put in one of the words 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 the "$wordname" function that I mentioned earlier. What do I have to make that equal so that it will display the word that is searched for in the searchbox? I know my php might not be the best for the task, but please remember that I'm a total noob...

Code: Select all

<html>
<head><title>Definitions</title></head>
<body>
<?php
  $user="--------";
  $host="-------";
  $password="------";
  $database = "dictionary";
  $cxn = mysqli_connect($host,$user,$password,$database)
         or die ("couldn't connect to server");
  $wordname = 'name'
  $query = "SELECT * FROM word WHERE wordname='$wordname'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
 
  /* Display results in a table */
  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 html form searchbox's code is:

Code: Select all

 
<head>
<title>Search</title>
</head><body>
<h3>Search Below</h3>      
<form name="wordname" action="wordDisplay.php"
method="post">
Search: 
<input type="text" name="name">
<input type="submit" value="Search">
</form>         
</body>
</html>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trouble with a really simple project

Post by John Cartwright »

Assuming you meant to exclude the action in your form, and you want to use $_GET (passing variables in the url), then your word should be accessible through $_GET['name'].

Code: Select all

 
if (isset($_GET['name'])) {
   $wordname = mysql_real_escape_string($_GET['name']);
} else {
   //handle error.. missing word
}
Notice I threw in the mysql_real_escape_string(). This should be applied to all user input going into the query to avoid miscellaneous characters botching the query, i.e. quotes.
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

Great thanks. So that should just replace all of line 11?
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

I tried this and it didn't work... Ugh it seems so simple but there has to be like one little mistake.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trouble with a really simple project

Post by John Cartwright »

What have you tried? I'm not going to write anything furthur than that since this is a school assignment.

EDIT | Okay so you were using the $_POST method afterall, somehow it eludated me. Change all the $_GET to $_POST.
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

Its not really a school project, I'm just helping a teacher out...
Okay I tried changing the post to get to keep it consistent but its still not working... here's the code as it is now:

Code: Select all

 
<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($_GET['name'])) {
    $wordname = mysql_real_escape_string($_GET['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>
 
and the searchbox:

Code: Select all

 
<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>
 
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Trouble with a really simple project

Post by WebbieDave »

andysm849 wrote:

Code: Select all

$query = "SELECT * FROM word WHERE wordname='name'";
What the? You definitely need to stare at that line for a little while :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trouble with a really simple project

Post by John Cartwright »

You also need to read my EDIT | in the last post.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Trouble with a really simple project

Post by WebbieDave »

Jcart wrote:You also need to read my EDIT | in the last post.
He changed the form method to get.
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

WebbieDave wrote:
andysm849 wrote:

Code: Select all

$query = "SELECT * FROM word WHERE wordname='name'";
What the? You definitely need to stare at that line for a little while :)
should it be WHERE wordname='$wordname'"; ?
Sorry I'm new at this...

I changed all the posts to gets... shouldn't that work the same, as long as they are all gets?
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

When I did that, and changed 'name' to $wordname, the resulting url, instead of being just "http://....worddisplay.php" it changed to "http://...wordDisplay.php?name=(the word I searched for)". This is a step in the right direction right?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trouble with a really simple project

Post by John Cartwright »

$_GET takes the variables out of the url, $_POST takes variables from form submissions done with the post method. Either way is fine here.
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

okay... so what should I change now? Thanks for your help so far...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trouble with a really simple project

Post by John Cartwright »

Give it a shot, and post your code ;)
andysm849
Forum Newbie
Posts: 14
Joined: Tue Jun 17, 2008 12:26 pm

Re: Trouble with a really simple project

Post by andysm849 »

Sorry, I was trying to say that I changed all the stuff you said...ive tried both gets and posts, and neither is working. Everything else is the same as it was with the last time I posted code.
Post Reply