Jump to content

new to php and was wandering if someone could have a look at my code!!!


glens1234

Recommended Posts

Hi there. I have just downloaded mamp and have written a small php script.

The script bascially inserts data from a form into a database.

 

When i fill out the form i get the msg "Error, insert query failed", which i defined in the script.

So can someone please help me understand why my insert statement is failing?

 

The code for processing the form is...

<?php
include 'config.php';
include 'opendb.php';
   
   $name = stripslashes($_POST['Name']);
   //$a_file = $_POST['a_file'];dont need this code yet
   $languages_spoken = $_POST['languages_spoken'];


$query = "INSERT INTO employees (name,lang_spoken) VALUES ('$name', ‘$languages_spoken') "; 
mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');

echo "Thank you ". $name . ". We will be in contact with you shortly";
include 'closedb.php';
?>

 

The code of the html form is...

<!--- onyl display form if it has not already been submited --->
<?php
if (!isset($_POST['submit'])) {
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Application form</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" href="stylesheet.css" type="text/css" media="screen" />
</head>
<body>
   <form method="post" action="process_app_form.php">
      <table border="0" cellspacing="0" cellpadding="2">
           <tr>
            <td>Name:</td><td><input type="text" name="Name" maxlength="30" size="26" /></td>
         </tr>

	<tr><td>Languages Spoken</td>
	<td><select name="languages_spoken" size="5">
	<option value="English">English</option>
	<option value="French">French</option>
	<option value="Spanish">Spanish</option>
	<option value="German">German</option>
	</select></td>
                  </tr>
	   <tr>
	    <td><input type="submit" name="Submit"
               value="Submit"></td>
         </tr>
      </table>
   </form>
</body>
</html>
   <?php
   }
   ?>

i created the table using phpmyadmin. As far as i can remember the statements were...

CREATE TABLE employees (
name varchar(20) NOT NULL DEFAULT "",
lang_spoken TINY INT UNSIGNED NOT NULL DEFAULT '0'
) TYPE=MyISAM;

 

 

Since both the opendb.php and config.php files are not reporting any errors i will not include them.

 

 

Any sugguestions?

 

Cheers!

 

 

Link to comment
Share on other sites

If $languages_spoken is a INT, then remove the quotes completely from around it.

 

And again, echo your query so you can see what is being sent.

 

Turn on error reporting so you can get an actual error from MySQL.

 

mysql_query($query) or die("Error, insert query failed $query" .mysql_error());

Link to comment
Share on other sites

thanks for you help guys!

 

However, i just cant seem to figure this out.

 

Firstly i noticed that the list box does not store an integer value but a string so i altered the field in my database to varchar(20). i also put the single quotes back around Languages_spoken variable.

 

Then i echoed the Languages_spoken variable to the screen and i got the message, "your chosen language is:EnglishError, insert query failed No database selected"

 

So can anyone sugguest why it's says EnglishError as opposed to english?

 

In regards to "No database selected", i used the mysql_error() function and got the msg...

"Access denied for user ''@'localhost' to database 'translators'"

 

At the moment the username and password are left blank as i dont know where or how to set them. Can i do this within phpmyadmin?

 

Thanks.

Link to comment
Share on other sites

ok ive set my username and pw in phpmyadmin.

This what i now have for my config.php

 

<?php
$dbhost = "localhost:8889"; //this is the mysql port number on osx
$dbuser = "glens1234";
$dbpass = "mypw";
$dbname = 'translators';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname) or die ('Error connecting to mysql '.mysql_error());
?>

 

And now i get the "Error connecting to mysql" msg.

 

any ideas??

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.