Boerboel649 Posted May 19, 2006 Share Posted May 19, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/10025-inserting-info-from-a-form-into-mysql-database/ Share on other sites More sharing options...
SharkBait Posted May 19, 2006 Share Posted May 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10025-inserting-info-from-a-form-into-mysql-database/#findComment-37238 Share on other sites More sharing options...
Boerboel649 Posted May 19, 2006 Author Share Posted May 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10025-inserting-info-from-a-form-into-mysql-database/#findComment-37281 Share on other sites More sharing options...
.josh Posted May 19, 2006 Share Posted May 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10025-inserting-info-from-a-form-into-mysql-database/#findComment-37290 Share on other sites More sharing options...
Boerboel649 Posted May 21, 2006 Author Share Posted May 21, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/10025-inserting-info-from-a-form-into-mysql-database/#findComment-37542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.