<html>
<head> <title>INSERTING INVENTORY</title> </head>
<body>
<?php
// Teresa Roberson
// PHP -Program13_4a
//August 18, 2008
?>
<form name="input" action="Program13_4b.php" method="post">
<h1><font color="purple">INVENTORY DATA</font></h1>
<br>
Item Name:<input type="text" size="20" maxlength="20" name="Item_Name">
<br>
Purchase Cost:<input type="text" size="5" maxlength="20" name="Purchase_Cost">
<br>
Sales Price:<input type="text" size="5" maxlength="20" name="Sales_Price">
<br>
Number Sold:<input type="text" size="5" maxlength="20" name="Number_Sold">
<br>
Number in Inventory:<input type="text" size="5" maxlength="20" name="Number_in_Inventory">
<br>
<br><br><br><input type="submit" value="Click To Submit">
<input type="reset" value="Erase and Restart"></center>
</form>
</body>
</html>
<html>
<head> <title>INSERT RESULTS </title> </head>
<body>
<form name="input" action="Program13_4a.php" method="post">
<h1><font color="purple">INVENTORY INSERT RESULTS</font></h1>
<br>
<?php
// Teresa Roberson
// PHP -Program13_4b
//September 4, 2008
//Creating a PHP PROGRAM THAT WILL ALLOW YOU TO ENTER DATA INTO A DATABASE TABLE
$Item = $_POST['Item_Name'];
$Cost = $_POST['Purchase_Cost'];
$Price = $_POST['Sales_Price'];
$Sold = $_POST['Number_Sold'];
$Inventory = $_POST['Number_in_Inventory'];
$server = 'localhost';
$user = 'root';
$passwd = '';
$database = 'program13';
$table_name = 'Inventory';
$query = "INSERT INTO $table_name VALUES
($Item, $Cost, $Price, $Sold, $Inventory)";
$connect = mysql_connect($server, $user, $passwd);
if (!$connect) {
die ("Cannot connect to the $server using $user"); }
else{
print "The Query is <i>$query</i><br>";
mysql_select_db($database, $connect);
print '<br><font size="4" color="blue">';
if (mysql_query($query, $connect)) {
print "Insert into $database was successful!</font>";
} else {
print "Insert into $database failed!</font>";
}
}
mysql_close ($connect);
?>
<br><br><br><input type="submit" value="Enter Another Record">
</form>
</body>
</html>
PROGRAM TO ENTER DATA INTO A DATABASE TABLENEED HELP WITH THIS PHP PROGRAM
Page 1 of 1
7 Replies - 1654 Views - Last Post: 09 September 2008 - 06:56 PM
#1
PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 07 September 2008 - 07:56 PM
Replies To: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
#2
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 07 September 2008 - 08:00 PM
Hello lucydinner, do you have a question about this code that you have posted?
#3
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 07 September 2008 - 09:33 PM
In this line:
I would take out the connection argument ($connect). I've never had to use mysql_query in that manner.
if (mysql_query($query, $connect)) {
I would take out the connection argument ($connect). I've never had to use mysql_query in that manner.
#4
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 08 September 2008 - 04:24 AM
Wild-ass guess as to the problem which is provided...why does this not insert data into the database as I expect?
Answer to this divination:
If any of the matching columns in the database are string types (VARCHAR, for example), the contents of the variables must be quoted. I suspect that $_POST['Item_Name'] is a string, and that the query should therefore be:
Answer to this divination:
$query = "INSERT INTO $table_name VALUES ($Item, $Cost, $Price, $Sold, $Inventory)";
If any of the matching columns in the database are string types (VARCHAR, for example), the contents of the variables must be quoted. I suspect that $_POST['Item_Name'] is a string, and that the query should therefore be:
$query = "INSERT INTO $table_name VALUES ('$Item', $Cost, $Price, $Sold, $Inventory)";
#5
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 08 September 2008 - 04:35 AM
Taking this example, what would happen if the first column of the table was just a pk_ID full of ints, would the value insertion move onto the next column or would it throw an error?
#6
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 08 September 2008 - 07:56 AM
If the first column in the table was an autoincrementing integer primary key, as it should be in any proper relational DB, then the insert would fail (99.9% sure on this, but it's been a while). You would need to pass NULL in the first VALUE field to get the desired result.
The best way to do an insert, IMHO, is to give the fields in the query so things are blatantly obvious:
That way, it's a bit easier to catch any mismatch.
The best way to do an insert, IMHO, is to give the fields in the query so things are blatantly obvious:
$query = "INSERT INTO Inventory (`ID`, `Item`, `Cost`, `Sold`, `Inventory`) VALUES (NULL, '$ItemName', $Cost, $Sold, $Inventory);";
if (!mysql_query($query))
{
// Log the error
trigger_error ("There was an error in your INSERT query [$query]: " . mysql_error());
}
else if (mysql_affected_rows() < 1)
{
// Log the unaffected insert
trigger_error ("The INSERT query [$query] did not affect any rows", E_NOTICE);
}
else
{
echo ('New record added with ID ' . mysql_insert_id() . "\n");
}
That way, it's a bit easier to catch any mismatch.
#7
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 09 September 2008 - 06:49 PM
JackOfAllTrades, on 8 Sep, 2008 - 07:56 AM, said:
If the first column in the table was an autoincrementing integer primary key, as it should be in any proper relational DB, then the insert would fail (99.9% sure on this, but it's been a while). You would need to pass NULL in the first VALUE field to get the desired result.
If the first column in the table was an autoincrementing integer primary key... the insert query would be successful.
I have a database table with three fields; auto-increment integer ID, username, and hashed_password. My insert query is...
$qry ="INSERT INTO users (username,hashed_password) VALUES ('{$username}','{$hashed_password}')";
... and it works like a charm.
~ grinnZ ~
#8
Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE
Posted 09 September 2008 - 06:56 PM
Thanks, I was very wrong then.
Always happy to have my ignorance fought.
It's been a while since I've done any real MySQL work (have been living in the world of PostgreSQL and sprocs, when I've been in the DB at all). When I did work in that realm, I always passed NULL in the list of values to explicitly set an autoincrementing primary key. I'll have to keep it in mind for the next time I've got to do so.
It's been a while since I've done any real MySQL work (have been living in the world of PostgreSQL and sprocs, when I've been in the DB at all). When I did work in that realm, I always passed NULL in the list of values to explicitly set an autoincrementing primary key. I'll have to keep it in mind for the next time I've got to do so.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|