Jump to content


Photo

Inserting info from a form into MySQL database


  • Please log in to reply
4 replies to this topic

#1 Boerboel649

Boerboel649
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 19 May 2006 - 06:37 PM

Hi all,
I'm trying to insert a information from a form into a MySQL database, but it isn't working... here's my code

<?
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$nickname = $_POST['nickname'];
$number = $_POST['number'];
$position = $_POST['position'];
$height = $_POST['height'};
$weight = $_POST['weight'];
$other = $_POST['other'];
   $$mysql_access = mysql_connect($dbh);
   mysql_select_db("steam");
   $query = "INSERT INTO players (fname, lname, nickname, number, position, height, weight, other)";
   $query .= "VALUES('$fname', '$lname', '$nickname', '$number', '$position', '$height', '$weight', '$other')";
   mysql_query($query, $mysql_access);
   print("successfully added your info to the mySQL database!");

?>
database name is steam, table is players.
I'm sure it's rather simple, but I'm having some trouble. What is wrong?
Thanks in advance!

#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 19 May 2006 - 07:01 PM

I usually check to make sure that the $_POST values have something in them to make sure its passing them properly.

if(isset($_POST['fname'])) {
  // Name is entered
} else {
  // Name is missing
}

You can also do this for your query:

mysql_query($query, $mysql_access) or die("MySQL Error: <br /> {$query} <br /> ". mysql_error());

Tells you whether or not your sql statement is correct :) When debugging I will also echo the query before I submit it so that I can make sure the values are proper.





#3 Boerboel649

Boerboel649
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 19 May 2006 - 09:13 PM

Still not working, and MySQL is not throwing me any errors... Here's my HTML form

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="../newplayer.php">
  <p align="center">Add a new player </p>
  <table width="300" border="1" align="center">
    <tr>
      <td width="87">First Name</td>
      <td width="197"><input type="text" name="fname" /></td>
    </tr>
    <tr>
      <td>Last Name</td>
      <td><input type="text" name="lname" /></td>
    </tr>
    <tr>
      <td>Nickname (optional)</td>
      <td><input type="text" name="nickname" /></td>
    </tr>
    <tr>
      <td>Number</td>
      <td><input type="text" name="number" /></td>
    </tr>
    <tr>
      <td>Position</td>
      <td><input type="text" name="position" /></td>
    </tr>
    <tr>
      <td>Height </td>
      <td><input type="text" name="height" /></td>
    </tr>
    <tr>
      <td><label>Weight</label></td>
      <td><input type="text" name="weight" /></td>
    </tr>
    <tr>
      <td height="39">Other</td>
      <td><textarea name="other" cols="30" id="other"></textarea></td>
    </tr>
  </table>
  <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

And here's my PHP for the file newplayer.php

<?php $dbh=mysql_connect ("localhost", "<removed>", "<removed>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("steam");  
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$nickname = $_POST['nickname'];
$number = $_POST['number'];
$position = $_POST['position'];
$height = $_POST['height'};
$weight = $_POST['weight'];
$other = $_POST['other'];
   $$mysql_access = mysql_connect($dbh);
   mysql_select_db("steam");
   $query = "INSERT INTO players (fname, lname, nickname, number, position, height, weight, other)";
   $query .= "VALUES('$fname', '$lname', '$nickname', '$number', '$position', '$height', '$weight', '$other')";
   mysql_query($query, $dbh) or die("MySQL Error: <br /> {$query} <br /> ". mysql_error());
   print("successfully added your info to the mySQL database!");

?>
</body>
</html>
It would be greatly appreciated if someone could help me with this.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 19 May 2006 - 09:29 PM

first off take out these 2 lines you don't need them you already established the connection at the beginning of your script:

$$mysql_access = mysql_connect($dbh);
mysql_select_db("steam");

then do this:

echo $query;

and post what outputs.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Boerboel649

Boerboel649
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 21 May 2006 - 01:37 AM

Hey thanks guys... I just realized something... I had been posting to another file (previous attempt)... I figured this out when I took that print out, and put the echo in. I submitted the form and it gave me the print. lol. Anyways it's working great now. Thanks so much for your help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users