7 Replies - 2133 Views - Last Post: 09 September 2008 - 06:56 PM Rate Topic: -----

#1 lucydinner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-August 08

PROGRAM TO ENTER DATA INTO A DATABASE TABLE

Post icon  Posted 07 September 2008 - 07:56 PM

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



Is This A Good Question/Topic? 0
  • +

Replies To: PROGRAM TO ENTER DATA INTO A DATABASE TABLE

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,066
  • Joined: 10-May 07

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?
Was This Post Helpful? 0
  • +
  • -

#3 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE

Posted 07 September 2008 - 09:33 PM

In this line:
if (mysql_query($query, $connect)) {

I would take out the connection argument ($connect). I've never had to use mysql_query in that manner.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

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:
$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)";

Was This Post Helpful? 0
  • +
  • -

#5 homemade-jam  Icon User is offline

  • Gabe's Nemesis
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,300
  • Joined: 17-March 08

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?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

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:

$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.
Was This Post Helpful? 0
  • +
  • -

#7 grinnZ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 06-August 08

Re: PROGRAM TO ENTER DATA INTO A DATABASE TABLE

Posted 09 September 2008 - 06:49 PM

View PostJackOfAllTrades, 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 ~
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1