joshgarrod Posted January 26, 2008 Share Posted January 26, 2008 Hi everyone, I need some help. I have here a script that is supposed add a category to my database, but it doesn't work. I have written it to the best of my ability but it doesn't work. Can someone please take a look and tell me where I am going wrong? Thanks in advance. <? $usr = "usernamer; $pwd = "password"; $db = "database"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> <HTML> <HEAD> <TITLE>Add Category</TITLE> </HEAD> </font> <P>Add stock</P> <font face="Arial, Helvetica, sans-serif"> <? # this is processed when the form is submitted # back on to this page (POST METHOD) if ($REQUEST_METHOD=="POST") { # double-up apostrophes $Catname = str_replace("'","''",$Catname); # setup SQL statement $SQL = " INSERT INTO Categories "; $SQL = $SQL . " (Cat_name) VALUES "; $SQL = $SQL . " ('$Catname') "; #execute SQL statement $result = mysql_db_query($db,"$SQL",$cid); $ID=mysql_insert_id(); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P>New Category added</P>\n"); } ?> </font> <FORM NAME="fa" ACTION="file:///C|/Users/Josh/Documents/Websites/Stow Caravans/addStock.php" METHOD="POST"> <TABLE width="392"> <TR> <TD width="133"><font face="Arial, Helvetica, sans-serif"><B>Category name :</B> </font></TD><TD width="247"><font face="Arial, Helvetica, sans-serif"> <INPUT NAME="Cat_name" TYPE="text" id="Cat_name" SIZE=40> </font></TD></TR> <TR> <TH COLSPAN=2><font face="Arial, Helvetica, sans-serif"> <input name="submit" type="submit" value="Add Category" /> </font></TH> </TR> </TABLE> <p align="center"><font face="Arial, Helvetica, sans-serif"><a href="file:///C|/Users/Josh/Documents/Websites/Stow Caravans/sparesAdminPage.html">Back</a></font></p> </FORM> <font face="Arial, Helvetica, sans-serif"> <? mysql_close($cid); ?> Link to comment https://forums.phpfreaks.com/topic/87905-solved-add-entries-to-a-database-using-html-form/ Share on other sites More sharing options...
hatrickpatrick Posted January 26, 2008 Share Posted January 26, 2008 You could try it this way: <? $usr = "usernamer; $pwd = "password"; $db = "database"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } $database=mysql_select_db($db); ?> <HTML> <HEAD> <TITLE>Add Category</TITLE> </HEAD> </font> <P>Add stock</P> <font face="Arial, Helvetica, sans-serif"> <? # this is processed when the form is submitted # back on to this page (POST METHOD) if ($_POST["submit"]) { # double-up apostrophes $Catname = str_replace("'","''",$cat_name); # setup SQL statement $SQL = "INSERT INTO Categories (Cat_name) VALUES ('$Catname')"; #execute SQL statement $result = mysql_query($SQL); $ID=mysql_insert_id(); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P>New Category added</P>\n"); } ?> </font> <FORM NAME="fa" ACTION="file:///C|/Users/Josh/Documents/Websites/Stow Caravans/addStock.php" METHOD="POST"> <TABLE width="392"> <TR> <TD width="133"><font face="Arial, Helvetica, sans-serif"><B>Category name :</B> </font></TD><TD width="247"><font face="Arial, Helvetica, sans-serif"> <INPUT NAME="Cat_name" TYPE="text" id="Cat_name" SIZE=40> </font></TD></TR> <TR> <TH COLSPAN=2><font face="Arial, Helvetica, sans-serif"> <input name="submit" type="submit" value="Add Category" /> </font></TH> </TR> </TABLE> <p align="center"><font face="Arial, Helvetica, sans-serif"><a href="file:///C|/Users/Josh/Documents/Websites/Stow Caravans/sparesAdminPage.html">Back</a></font></p> </FORM> <font face="Arial, Helvetica, sans-serif"> <? mysql_close($cid); ?> I think your main problem there is that you called the input field "cat_name" but you used the variable $Catname in the str_replace function, which is a non existant variable... Link to comment https://forums.phpfreaks.com/topic/87905-solved-add-entries-to-a-database-using-html-form/#findComment-449746 Share on other sites More sharing options...
joshgarrod Posted January 26, 2008 Author Share Posted January 26, 2008 hey thanks your a genius, works spot on! thanks Link to comment https://forums.phpfreaks.com/topic/87905-solved-add-entries-to-a-database-using-html-form/#findComment-449749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.