Jump to content

[SOLVED] add entries to a database using html form


joshgarrod

Recommended Posts

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); ?>

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...

Archived

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

×
×
  • 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.