Forum Moderators: coopster
<?php
$connection = mysql_connect($_host, $db_user, $db_password) or die("Unable to connect to the database");
mysql_select_db($db_name, $connection);
$link = "<a href='resistor.php'> Click Here to Return To Data Entry Page </a>";
$date = time();
$component_Cat = 1;
If (isset($_POST['submit']) &&!empty($_POST['faiveley_PN']) &&!empty($_POST['uksupplier_PN']) &&!empty($_POST['Supplier_Name']) &&!empty($_POST['component_Value']))
{
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
VLAUE('$date', 1, 'bin_Number', '$component_Value', '$package_type', '$voltage_rating', '$current_rating', '$wattage_rating', '$quantity, '$description')";
mysql_query($query, $connection);
}
else
{
echo "you must fill out every field in the form";
echo $link;
}
?>
Form:
<!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>resistor</title>
<link href="resistor.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style></head>
<body>
<h1 align="center">Resistor</h1>
<form action="resistor_in.php" method="post">
<p>Faiveley Part Number:
<input name="faiveley_PN" type="text" id="faiveley_PN" maxlength="10" />
</p>
<p>UK Supplier Part Number:
<input name="uksupplier_PN" type="text" id="uksupplier_PN" maxlength="10" />
</p>
<p>Supplier Name:
<select name="Supplier_Name" id="Supplier_Name">
<option value="far">Farnell Electronics</option>
<option value="Rs">RS Electronics</option>
</select>
</p>
<p>Component Value:
<input name="component_Value" type="text" id="component_Value" maxlength="10" />
</p>
<p>Package Type:
<input name="package_type" type="text" id="package_type" />
</p>
<p>Voltage Rating:
<input name="voltage_rating" type="text" id="voltage_rating" maxlength="10" />
</p>
<p>Current Rating:
<input name="current_rating" type="text" id="current_rating" maxlength="10" />
</p>
<p>Wattage Rating:
<input name="wattage_rating" type="text" id="wattage_rating" maxlength="10" />
</p>
<p>Quantity:
<input name="quantity" type="text" id="quantity" maxlength="6" />
</p>
<p>Cost (£):
<input name="cost" type="text" id="cost" maxlength="4" />
</p>
<p>Description:
<textarea name="description" cols="50" rows="10" id="description"><Please Enter Component Details Here>
</textarea>
</p>
Bin Number:
<select name ="num">
<?php
require($_SERVER["DOCUMENT_ROOT"]."/Php_Codes/User_details.php");
$connection = mysql_connect($_host, $db_user, $db_password) or die("Unable to connect");
mysql_select_db($db_name, $connection);
$query = "SELECT * FROM bin_Number";
$result = mysql_query($query);
while ($record = mysql_fetch_assoc($result))
{
echo "<OPTION VALUE = '".$record["num"]."'>".$record["num"];
}
?>
</select>
</p>
<p>Submit:
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
[b]VALUES[/b] ('$date', 1, 'bin_Number', '$component_Value', '$package_type', '$voltage_rating', '$current_rating', '$wattage_rating', '$quantity, '$description')";
secondly, where are you getting these variables from? They seem like POST variables to me:
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
VLAUE('$date', 1, '".$_POST['bin_Number']."', '".$_POST['component_Value']."', '".$_POST['package_type']."', '".$_POST['voltage_rating']."', '".$_POST['current_rating']."', '".$_POST['wattage_rating']."', '".$_POST['quantity']."', '".$_POST['description']."')";
Thirdly, if this is the case, then you must escape your variables:
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
VLAUE('$date', 1, '".mysql_real_escape_string($_POST['bin_Number'])."', '".mysql_real_escape_string($_POST['component_Value'])."', '".mysql_real_escape_string($_POST['package_type'])."', '".mysql_real_escape_string($_POST['voltage_rating'])."', '".mysql_real_escape_string($_POST['current_rating'])."', '".mysql_real_escape_string($_POST['wattage_rating'])."', '".mysql_real_escape_string($_POST['quantity'])."', '".mysql_real_escape_string($_POST['description'])."')";
Try this. Also, if you want to know what you are getting in your _POST variables, put this at the top of the script:
echo '<pre>';
print_r($_POST);
echo '</pre>';
Good luck! :)
I found this on someone elses forum and modified it....
$faiveley_PN = addslashes(faiveley_PN);
$uksupplier_PN = addslashes($uksupplier_PN);
$component_Value = addslashes($component_Value);
$package_type = addslashes($package_type);
$voltage_rating = addslashes($voltage_rating);
$current_rating = addslashes($current_rating);
If I just use this just like that before the $query line....will this work?
Also, add this to the top of your script:
error_reporting(E_ALL);
And make sure to use Dreamcatcher's advice for adding
die(mysql_error()) after your mysql_query call.
But I get these to lines of error thrown.
Notice: Use of undefined constant uksupplier_PN - assumed 'uksupplier_PN' in #*$!#*$!#*$! on line 29
Notice: Use of undefined constant faiveley_PN - assumed 'faiveley_PN' in #*$!#*$!#*$!X on line 35
What does it mean?
Other than those notices, is it still not adding values to the database?
Have you tried to manually add it yourself through phpmyadmin or the command prompt? Have you checked the spelling of all the tables, databases, column names, etc...?
Keep us updated :)
this is what my table looks like.....
Field type Null
auto_Date int(10)unsigned No
component_Cat int(4) NO
faiveley_PN tinyiny(10) unsigned NO
uksupplier_PNtiny int(10) unsigned NO
bin_Number int(4) unsignedNo PRI
quantity int(4) NO
package_type varchar(10) NO
component_Value varchar(10) NO
volatage_rating varchar(6) NO
current_rating varchar(6) NO
wattage_rating varchar(6) No
cost decimal(3,3) NO
description tinytext NO
Is it possible that some of the type I got wrong? like auto_date field....and faiveley_PN where the user have to input decimal numbers like 123456.807. Do you think I have all the fields correct against the type?
if(!empty($_POST['faiveley_PN']) &&!empty($_POST['uksupplier_PN']) &&!empty($_POST['Supplier_Name']) &&!empty($_POST['component_Value']))
<?php
error_reporting(E_ALL);
echo '<pre>';
print_r($_POST);
echo '</pre>';
$connection = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_name, $connection);
$link = "<a href='resistor.php'> Click Here to Return To Data Entry Page </a>";
$date = time();
$component_Cat = 1;
if(!empty($_POST['faiveley_PN']) &&!empty($_POST['uksupplier_PN']) &&!empty($_POST['Supplier_Name']) &&!empty($_POST['component_Value']))
{
$faiveley_PN = mysql_real_escape_string($faiveley_PN);
$uksupplier_PN = mysql_real_escape_string($uksupplier_PN);
$component_Value = mysql_real_escape_string($component_Value);
$package_type = mysql_real_escape_string($package_type);
$voltage_rating = mysql_real_escape_string($voltage_rating);
$current_rating = mysql_real_escape_string($current_rating);
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
VALUE('$date', 1, 'bin_Number', '$component_Value', '$package_type', '$voltage_rating', '$current_rating', '$wattage_rating', '$quantity', '$description')";
mysql_query($query, $connection);
}
else
{
echo "you must fill out every field in the form";
echo $link;
}
?>
It was a typing error mavherick but in the table its correct.
But still not writting to the mysql table, what am I doing wrong? Im about to pull all my remaining hair out!
$faiveley_PN = mysql_real_escape_string($_POST['faiveley_PN']);
$uksupplier_PN = mysql_real_escape_string($_POST['uksupplier_PN']);
$component_Value = mysql_real_escape_string($_POST['component_Value']);
$package_type = mysql_real_escape_string($_POST['package_type']);
$voltage_rating = mysql_real_escape_string($_POST['voltage_rating']);
$current_rating = mysql_real_escape_string($_POST['current_rating']);
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
VALUE(now(), 1, 'bin_Number', '$component_Value', '$package_type', '$voltage_rating', '$current_rating', '$wattage_rating', '$quantity', '$description')";
mysql_query($query, $connection);
JAG
You're missing the cost value :-)
See you at the pub in 20 minutes ;)
I now have something else in the code html form, a value not getting inserted to mysql table.
Array
(
[faiveley_PN] => 100000
[uksupplier_PN] => 10000
[Supplier_Name] => far
[component_Value] => 34K
[package_type] => Axial
[voltage_rating] => 200V
[current_rating] => 10A
[wattage_rating] => 20W
[quantity] => 120
[cost] => 2
[description] => Resistor
[bin_Number] => 8
[Submit] => Submit
)
Query failed: Duplicate entry '0' for key 1
Its throwing out this error above....I empty the table just to test to see what it inserts. It always trying to insert 0 in the bin_Number field. But 8 is selected or if I select any other numbers it still says Duplicate entry? For some reason php is not passing the correct number to the Mysql table from the form select options. The code bellow fetches these numbers from the table and put the value in the option field in the html form. When a number is selected the value is not inserted to the MySQL table. But clearly above the Array have value 8 in the bin_Number variable. What am I doing wrong?
<select name ="bin_Number">
<?php
$connection = mysql_connect($_host, $db_user, $db_password) or die("Unable to connect");
mysql_select_db($db_name, $connection);
$query = "SELECT * FROM bin_Number";
$result = mysql_query($query);
while ($record = mysql_fetch_assoc($result))
{
echo "<OPTION VALUE = '".$record["num"]."'>".$record["num"];
}
?>
[edited by: Atomic_Guy at 8:32 am (utc) on Nov. 19, 2006]
$query = "INSERT INTO resistor(auto_Date, component_Cat, bin_Number, component_Value, package_type, voltage_rating, current_rating, wattage_rating, quantity, cost, description)
VALUE('$date', 1, '".[b]mysql_real_escape_string($_POST['bin_Number'])[/b]."', '$component_Value', '$package_type', '$voltage_rating', '$current_rating', '$wattage_rating', '$quantity', '$description')";
You never were inserting the value of $_POST['bin_Number'], just the text "bin_number". This would cause you to have duplicate entries.