Jump to content

Archived

This topic is now archived and is closed to further replies.

Boerboel649

Inserting info from a form into MySQL database

Recommended Posts

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

[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!");

?>[/code]
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!

Share this post


Link to post
Share on other sites
I usually check to make sure that the $_POST values have something in them to make sure its passing them properly.

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

You can also do this for your query:

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

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.



Share this post


Link to post
Share on other sites
Still not working, and MySQL is not throwing me any errors... Here's my HTML form

[code]<!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>[/code]

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

[code] <?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>[/code]
It would be greatly appreciated if someone could help me with this.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.