Results 1 to 4 of 4

Thread: can't insert data to MySQL with .php (form won't work)

  1. #1
    Join Date
    Jan 2006

    Question can't insert data to MySQL with .php (form won't work)

    I am a PHP and MySQL newbie, so please be gentle with me.

    I have made a database and a .php page which reads it and displays the data, however, I am having difficulty getting my page which inserts data to the database, to work.

    This is the code for the form which accepts the data:

    HTML Code:
    <form action="insert.php" method="get">
    <input type="Submit" name="submit" /> <br />
    Name/Model: <input type="text" name="namemodel" /><br />
    Manufacturer: <input type="text" name="manufacturer" /><br />
    Tagline: <input type="text" name="tagline" /><br />
    Ed Summary: <textarea cols="20" rows="10" name="ed_summary"></textarea><br />
    Main Editorial: <textarea cols="20" rows="20" name="ed_main"></textarea><br />
    Features: <textarea cols="20" rows="20" name="features"></textarea><br />
    Specs: <textarea cols="20" rows="10" name="specs"></textarea><br />
    Options: <textarea cols="20" rows="10" name="options"></textarea><br />
    Thumbnail Picture: <input type="text" name="pic_thumb_100x100" /><br />
    Picture 1: <input type="text" name="pic_1" /><br />
    Picture 2: <input type="text" name="pic_2" /><br />
    Cost Price: <input type="text" name="costprice" /><br />
    Retail Price: <input type="text" name="retailprice" /><br />
    Supplier ID: <input type="text" name="supplierid" /><br />
    Current Stock: <input type="text" name="curr_stock" /><br />
    Minimum Stock: <input type="text" name="minstock" /><br />
    Maximum Stock: <input type="text" name="maxstock" /><br />
    <input type="Submit" name="submit" />

    and this is the page called ‘insert.php’ which should insert the data to the database:

    PHP Code:

    ="removed for forum";
    $password="removed for forum";

    $query = ("INSERT INTO products VALUES ('', '$NameModel', '$Manufacturer', '$Tagline', '$Ed_Summary', '$Ed_Main', '$Specs', '$Features', '$Options', '$Pic_Thumb_100x100', '$Pic_1', '$Pic_2', '$CostPrice', '$RetailPrice', '$SupplierID', '$Curr_Stock', '$MinStock', '$MaxStock')");

    My testing has led me to believe that the data is being received by insert.php but is for some reason, not being stored in the database.

    There is no error message.

    Thanks for your time


  2. #2
    Join Date
    Dec 2005
    Sydney, Australia

    Which DB your product table reside?

    Assuming a connection is established, in your mysql_connect statement, the next step is to select the database with which you want to work. So,You need to insert a sql statement similar to this:
    If you're new to mySQL, its advisable to test the status on your SQL querry statements...

  3. #3
    Join Date
    Aug 2004
    You never actually execute the query, you just assign it to a variable. Here's a coding pattern to use to help debug any problems:
    PHP Code:
    $conn1=odbc_connect($database,$username,$password) or die("DB connextion failed.");
    mysql_select_db($database) or die("DB select failed: " mysql_error());
    $query = ("INSERT INTO products VALUES ('', '$NameModel', '$Manufacturer', '$Tagline', '$Ed_Summary', '$Ed_Main', '$Specs', '$Features', '$Options', '$Pic_Thumb_100x100', '$Pic_1', '$Pic_2', '$CostPrice', '$RetailPrice', '$SupplierID', '$Curr_Stock', '$MinStock', '$MaxStock')");
    $result mysql_query($query) or die("Query failed: $query == " mysql_error());
    "<p>".mysql_affected_rows()." row(s) inserted into database.</p>\n"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Join Date
    Aug 2005
    The Garden State
    actually, NogDog's code won't work as you're using an ODBC connection. however his approach is right.

    PHP Code:
    $query "INSERT INTO products VALUES ('', '$NameModel', '$Manufacturer', '$Tagline', '$Ed_Summary', '$Ed_Main', '$Specs', '$Features', '$Options', '$Pic_Thumb_100x100', '$Pic_1', '$Pic_2', '$CostPrice', '$RetailPrice', '$SupplierID', '$Curr_Stock', '$MinStock', '$MaxStock')";
    $odbcdoquery odbc_exec($conn1,$query); 
    here's the ODBC listing from PHP, for reference. you might want to review it. However, I would caution against using ODBC as most places (web hosts, businesses) prefer to stay away from its insecure settings.


    However, I'm assuming you're using IIS and its datasources, so it's up to you.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center