Jump to content

Inserting info from a form into MySQL database


Boerboel649

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!
Link to comment
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.



Link to comment
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.
Link to comment
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!
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.