Jump to content

[SOLVED] Issue with PHP file


pluto

Recommended Posts

Good afternoon all,

 

I'm having something of a problem with a PHP page I'm developing.

I've managed to keep the HTML and the PHP seperate so it's a bit easier to manage, however, I'm trying to add a record to a data base and most of the fields are getting in to the DB excpet EnglishWord and EnglishDefinition, and I can't figure out what's wrong due to my lack of experience in PHP. 

 

The word table is structured thus:

 

wordId, GreekWord, GreekDefinition, EnglishWord, EnglishDefinition, Chapter,Section

 

The code is posted below for your perusal.  any help would be awesome.  Thanks.

 

addWord.php

<?php
/** PHP code inserted here */
$greekWord = $_POST['GreekWord'];
$greekDef = $_POST['GreekDefinition'];
$englishWord = $_POST['EnglishWord'];
$englishDef = $_POST['EnglishDefinition'];
$chapter = $_POST['chapter'];
$section = $_POST['section'];

echo "connection to the DB section of the code";
$conn = @mysql_connect("localhost", "root") or die("Could not connect to the data base");
$db = @mysql_select_db("test",$conn) or die("Error in selecting the data base.");

echo "setting the SQL variables up";
// This is so the SQL statement is correctly formatted.
$grkWrd = "'".$greekWord."'";
$grkDef = "'".$greekDef."'";
$engWrd = "'".$englishWord."'";
$engDef = "'".$englishDef."'";
$chptr = "'".$chapter."'";
$sctn = "'".$section."'";

echo "the query is here";
$query = "INSERT INTO word (GreekWord, GreekDefinition, EnglishWord, EnglishDefinition, Chapter, Sections) values ($grkWrd,$grkDef,$engWrd,$engDef,$chptr,$sctn)";

echo "exuctuing the query";
$exec = @mysql_query($query, $conn) or die ("Cannot add the word to the database");			

echo "query execution successful";
if($exec)
{
	//echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
	echo "Word added";
	//header("Location:http://localhost/ninjatuna/wordAdded.html");
}
else
{
	if($errmsg!="")
	{
		echo($errmsg);
		echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
		exit;
	}
}
?>

 

addword.html

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<link rel="stylesheet" type="text/css" href="layout.css">
	<script type="text/javascript" src="translator.js"></script>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<title>Welcome to LearnGreek.com (Work title)</title>
</head>

<body>
	<div id="header">
		<h1>Welcome to LearnGreek.com</h1>	
	</div>	


	<div id="nav">
		<ul>
			<li><a href="index.html">Home</a></li>				
			<li><a href="Login.html">Login</a></li>
			<li><a href="Register.html">Register</a></li>
			<li><a href="Links.html">Links</a></li>
		</ul>
	</div>
	<div id="main">
	<p>			
		<form name="addwrd" action="addWord.php" method="post">				
		 	Greek Word: <input type="text" onkeyup="trans(this.id)" id="greek_word" name="GreekWord" size=20><br><br>
		 	Greek Definition:  <input type="text" onkeyup="trans(this.id)" id="greek_definition" name="GreekDefinition" size=20><br><br>
		 	English Word: <input type="text" name="EnglishWord" size=30><br><br>
		 	English Definition: <input type="text" name="EnglishDefinition" size=20><br><br>
		 	Chapter: <input type="text" name="chapter" size=20><br><br>
			Section: <input type="text" name="section" size=20><br><br>
		 	<input type="submit" name="addword" value="Submit">
		</form>
	</p>
	</div>

</body>
</html>

Once again, many many thanks for taking the time to look at this.  I'm aware it's not secure code as of yet, but i'm focussing on getting it runnning before I start to lock it all down.

Link to comment
Share on other sites

try echoing $_POST['EnglishWord'] and $_POST['EnglishDefinition']..

and also echo the query to check its all correct

i.e.

<?php
   /** PHP code inserted here */
   $greekWord = $_POST['GreekWord'];
   $greekDef = $_POST['GreekDefinition'];
   $englishWord = $_POST['EnglishWord'];
   $englishDef = $_POST['EnglishDefinition'];
   $chapter = $_POST['chapter'];
   $section = $_POST['section'];

//test to see if values being posted...
echo $_POST['EnglishWord'];
echo '<br />';
echo $_POST['EnglishDefinition'];
            
   echo "connection to the DB section of the code";
   $conn = @mysql_connect("localhost", "root") or die("Could not connect to the data base");
   $db = @mysql_select_db("test",$conn) or die("Error in selecting the data base.");
   
   echo "setting the SQL variables up";
   // This is so the SQL statement is correctly formatted.
   $grkWrd = "'".$greekWord."'";
   $grkDef = "'".$greekDef."'";
   $engWrd = "'".$englishWord."'";
   $engDef = "'".$englishDef."'";
   $chptr = "'".$chapter."'";
   $sctn = "'".$section."'";
   
   echo "the query is here";
   $query = "INSERT INTO word (GreekWord, GreekDefinition, EnglishWord, EnglishDefinition, Chapter, Sections) values ($grkWrd,$grkDef,$engWrd,$engDef,$chptr,$sctn)";

// echo query to validate
echo '<br />';
echo $query;
   
   echo "exuctuing the query";
   $exec = @mysql_query($query, $conn) or die ("Cannot add the word to the database");         
   
   echo "query execution successful";
   if($exec)
   {
      //echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
      echo "Word added";
      //header("Location:http://localhost/ninjatuna/wordAdded.html");
   }
   else
   {
      if($errmsg!="")
      {
         echo($errmsg);
         echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
         exit;
      }
   }
?>

Link to comment
Share on other sites

Ok,

 

I tried that and it didn't echo anything back, which would lead me to suggest that somehow it's not reading what's been put in the text boxes.  But I'm not sure why, I've checked everything and it all looks ok...

 

Pluto

Link to comment
Share on other sites

You should turn on error reporting for your script and displaying SQL errors. And removing the '@' before the query to not prevent it from displaying errors if there are any.

 

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

   /** PHP code inserted here */
   $greekWord = $_POST['GreekWord'];
   $greekDef = $_POST['GreekDefinition'];
   $englishWord = $_POST['EnglishWord'];
   $englishDef = $_POST['EnglishDefinition'];
   $chapter = $_POST['chapter'];
   $section = $_POST['section'];

//test to see if values being posted...
echo $_POST['EnglishWord'];
echo '<br />';
echo $_POST['EnglishDefinition'];
            
   echo "connection to the DB section of the code";
   $conn = mysql_connect("localhost", "root") or die(mysql_error());
   $db = mysql_select_db("test",$conn) or die(mysql_error());
   
   echo "setting the SQL variables up";
   // This is so the SQL statement is correctly formatted.
   $grkWrd = "'".$greekWord."'";
   $grkDef = "'".$greekDef."'";
   $engWrd = "'".$englishWord."'";
   $engDef = "'".$englishDef."'";
   $chptr = "'".$chapter."'";
   $sctn = "'".$section."'";
   
   echo "the query is here";
   $query = "INSERT INTO word (GreekWord, GreekDefinition, EnglishWord, EnglishDefinition, Chapter, Sections) values ($grkWrd,$grkDef,$engWrd,$engDef,$chptr,$sctn)";

// echo query to validate
echo '<br />';
echo $query;
   
   echo "exuctuing the query";
   $exec = mysql_query($query, $conn) or die(mysql_error());         
   
   echo "query execution successful";
   if($exec)
   {
      //echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
      echo "Word added";
      //header("Location:http://localhost/ninjatuna/wordAdded.html");
   }
   else
   {
      if($errmsg!="")
      {
         echo($errmsg);
         echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
         exit;
      }
   }
?>

 

Try adding again to see if any errors come up.

Hope I didn't make any mistakes and helped.

Link to comment
Share on other sites

Hi DarkLink, thanks for that,

 

i did that, and got the following errors:

 

Notice: Undefined index: EnglishWord in C:\xampp\htdocs\NinjaTuna\addWord.php on line 9

 

Notice: Undefined index: EnglishDefinition in C:\xampp\htdocs\NinjaTuna\addWord.php on line 10

 

Notice: Undefined index: EnglishWord in C:\xampp\htdocs\NinjaTuna\addWord.php on line 15

 

 

Notice: Undefined index: EnglishDefinition in C:\xampp\htdocs\NinjaTuna\addWord.php on line 17

Word added

 

Thing is though, that they are in the HTML (see first post) and they are spelt correctly, or am I missing something really obvious here?  I've spent the last 40 minutes trying to figure out what's going on.

 

Thanks

 

Pluto

Link to comment
Share on other sites

Those notices are warnings that are telling you to define the variables before it is being used.

 

Such as defining $_POST['something'] before you use it, it is a change made to PHP 5, to my knowledge, it is not that important and can be ignored.

 

For instance, I have made an attempt on remedying your code, I have made it all into one single PHP file and when you go on addWord.php, it should display the form and process the form when submitted.

 

Here it is:

<?php
ini_set('display_errors',1);
error_reporting (E_ALL ^ E_NOTICE);

//if receive form submission
if ($_POST['submit']) {
   /** PHP code inserted here */
   $greekWord = $_POST['GreekWord'];
   $greekDef = $_POST['GreekDefinition'];
   $englishWord = $_POST['EnglishWord'];
   $englishDef = $_POST['EnglishDefinition'];
   $chapter = $_POST['chapter'];
   $section = $_POST['section'];

//test to see if values being posted...
echo $_POST['EnglishWord'];
echo '<br />';
echo $_POST['EnglishDefinition'];
            
   echo "connection to the DB section of the code";
   $conn = mysql_connect("localhost", "root") or die(mysql_error());
   $db = mysql_select_db("test",$conn) or die(mysql_error());
   
   echo "setting the SQL variables up";
   
   echo "the query is here";
   $query = "INSERT INTO word (GreekWord, GreekDefinition, EnglishWord, EnglishDefinition, Chapter, Sections) VALUES ('{$greekWord}','{$greekDef}','{$englishWord}','{$englishDef}','{$chapter}','{$section}')";

// echo query to validate
echo '<br />';
echo $query;
   
   echo "exuctuing the query";
   $exec = mysql_query($query, $conn) or die(mysql_error());         
   

   if($exec)
   {  echo "query execution successful";
      //echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
      echo "Word added";
      //header("Location:http://localhost/ninjatuna/wordAdded.html");
   }
   else
   {  
   echo "Failed to add word";
      if($errmsg!="")
      {
         echo $errmsg;
         echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
         exit;
      }
   }
}
else
{
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <link rel="stylesheet" type="text/css" href="layout.css">
      <script type="text/javascript" src="translator.js"></script>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <title>Welcome to LearnGreek.com (Work title)</title>
   </head>
   
   <body>
      <div id="header">
         <h1>Welcome to LearnGreek.com</h1>   
      </div>   

   
      <div id="nav">
         <ul>
            <li><a href="index.html">Home</a></li>            
            <li><a href="Login.html">Login</a></li>
            <li><a href="Register.html">Register</a></li>
            <li><a href="Links.html">Links</a></li>
         </ul>
      </div>
      <div id="main">
      <p>         
         <form name="addwrd" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">            
             Greek Word: <input type="text" onkeyup="trans(this.id)" id="greek_word" name="GreekWord" size="20"><br><br>
             Greek Definition:  <input type="text" onkeyup="trans(this.id)" id="greek_definition" name="GreekDefinition" size="20"><br><br>
             English Word: <input type="text" name="EnglishWord" size="30"><br><br>
             English Definition: <input type="text" name="EnglishDefinition" size="20"><br><br>
             Chapter: <input type="text" name="chapter" size="20"><br><br>
            Section: <input type="text" name="section" size="20"><br><br>
             <input type="submit" name="submit" value="Submit">
         </form>
      </p>
      </div>
      
   </body>
</html>

<?php
}// end else statement
?>

 

If it still does not insert the two fields that you said, I would really love to see your SQL table structure, if you do not have the SQL code for your table, you should do a backup of your database with PHPMyAdmin and it should give you the code for the structure.

 

Hope I didn't make any mistakes and helped.

Link to comment
Share on other sites

Hi DarkLink,

 

Thank you so much for taking the time to look at my code,

 

I've not had a chance to test it as of yet, but here's the code I used to create the database table.

 

CREATE TABLE `test`.`word` (
  `idword` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `GreekWord` VARCHAR(45) CHARACTER SET greek COLLATE greek_general_ci NOT NULL,
  `GreekDefinition` VARCHAR(45) CHARACTER SET greek COLLATE greek_general_ci NOT NULL,
  `EnglishWord` VARCHAR(45) NOT NULL,
  `EnglishDefinition` VARCHAR(45) NOT NULL,
  `Chapter` VARCHAR(45) NOT NULL,
  `Sections` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`idword`)
);

 

Thanks once again for your time

 

Pluto :)

Link to comment
Share on other sites

Hmm, I'm almost out of ideas. =/ But anyway, can you tell me what does that piece of JavaScript exactly do? I can see that "translator.js" judging by it's name, it translates the word to Greek, but how exactly does it do it?

 

I can also see an "onkeyup" event in the Greek text areas.

Link to comment
Share on other sites

Hi there,

 

believe it or not, it finally worked.  i'm not sure what I did, but I kept testing the code changing bits and bobs, and tried various things, and all of a sudden it worked.  Not sure why.

 

Just wanted to say thanks for all your help.

 

Pluto

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.