Click to See Complete Forum and Search --> : can't insert data to MySQL with .php (form won't work)


snowweb
01-18-2006, 01:06 AM
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:



<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" />

</form>




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



<?php
$username="removed for forum";
$password="removed for forum";
$database="myodbc_store";


$NameModel=$_GET['namemodel'];
$Manufacturer=$_GET['manufacturer'];
$Tagline=$_GET['tagline'];
$Ed_Summary=$_GET['ed_summary'];
$Ed_Main=$_GET['ed_main'];
$Features=$_GET['features'];
$Specs=$_GET['specs'];
$Options=$_GET['options'];
$Pic_Thumb_100x100=$_GET['pic_thumb_100x100'];
$Pic_1=$_GET['pic_1'];
$Pic_2=$_GET['pic_2'];
$CostPrice=$_GET['costprice'];
$RetailPrice=$_GET['retailprice'];
$SupplierID=$_GET['supplierid'];
$Curr_Stock=$_GET['curr_stock'];
$MinStock=$_GET['minstock'];
$MaxStock=$_GET['maxstock'];

$conn1=odbc_connect($database,$username,$password);
$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

pete

whitelight
01-20-2006, 11:48 PM
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:
mysql_select_db('myodbc_store',$conn1);
If you're new to mySQL, its advisable to test the status on your SQL querry statements...

NogDog
01-21-2006, 12:05 AM
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:

$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());
echo "<p>".mysql_affected_rows()." row(s) inserted into database.</p>\n";

chazzy
01-21-2006, 05:44 PM
actually, NogDog's code won't work as you're using an ODBC connection. however his approach is right.


$conn1=odbc_connect($database,$username,$password);
$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.

http://php.net/manual/en/ref.uodbc.php

However, I'm assuming you're using IIS and its datasources, so it's up to you.