Jump to content

Can you tell me why my form won't insert to database?


harlequeen

Recommended Posts

Hi

 

I'm trying to get some data from a form to the database, but I keep getting an error.  I know the info is being passed as I am echoing it just to see.

 

Here is my code, I'm sure it's something simple, but I can't see it.

include("myblah.php");

$Surname=$_POST['Surname'];
$Firstname=$_POST['Firstname'];
$Position=$_POST['Position'];
$previousClub=$_POST['previousClub'];

$playerquery=("INSERT INTO player VALUES ('$Surname','$Firstname','$Position','$previousClub')");

echo ($Surname);
echo ($Firstname);
echo ($Position);
echo ($previousClub);

$result=mysql_query($playerquery);
if ($result) {
echo "<b>Entry Successfully Posted!</b>";
}//end then
else {
echo "<b>ERROR: unable to post.</b>";
}//end else

 

 

Here is my Html

 

<form name="getentry" action="addplayer.php" method="POST"  target="_self">
Surname: <input type="text" name="Surname" size="35" ><br>
First Name: <input type="text" name="Firstname" size="30" ><br>
Preferred Position: <input type="text" name="Position" size="35" ><br>

Previous Clubs: 
<textarea name="previousClub" cols="20" rows="6"></textarea><br>
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="do" value="send" />
<input type="reset"  name="reset" value="Reset" />
</form>

 

and here are the fields I have in my database.

 

Surname

Firstname

Position

Age

Honours

Playerimage

Number (autoincrement field)

previousClub

 

I have been trying to get this to work, but just can't do it.  I know its bound to be something simple, please help.

 

Link to comment
Share on other sites

$playerquery=("INSERT INTO player VALUES ('$Surname','$Firstname','$Position','','','','','$previousClub')");

 

When you listed the fields in your database, did you list them in the order they are in the table? If you did, the above code should fix it (you were trying to put $previousClub into the age field which presumably you have set as integer in the table, and this was causing the error).

Link to comment
Share on other sites

adjust your query to this, to ensure that any error is spat out:

 

$result=mysql_query($playerquery) or die('query ['.$playerquery.'] failed.  mysql said: '.mysql_error());

 

i think you'll find it's failing because the columns and fields mismatch in count.  you have more fields than parameters that you're inputting.  when that's the case, you have to explicitly state the columns you're inserting:

 

$playerquery=("INSERT INTO player (Surname, Firstname, Position, previousClub) VALUES ('$Surname','$Firstname','$Position','$previousClub')");

Link to comment
Share on other sites

Thanks all, I thought it might have been that, but didn't realise I could put the '' to represent a missing field.  Once I added the 4 blanks required, it worked a treat. 

 

I also used "mysql said: '.mysql_error());" which I didn't know about before.

 

Thanks again

 

Harlequeen

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.