fehrenbacher33 Posted December 12, 2009 Share Posted December 12, 2009 Ok i dont know what im doing wrong here it says failed to insert data. heres the form <?php echo "<html>"; echo "<title> Insert Order </title>"; echo "<head> <h1>SIU Instructional Support Services Equipment Order</h1> </head>"; echo "<body>"; echo "<form method= 'post' action='addEquipment.php'>"; echo "Equipment_Category:"; $server = "localhost"; $user = "root" ; $pwd = "" ; $connect = mysql_connect($server, $user, $pwd) or die ("Error: Connection lost!"); $sel = mysql_select_db ("SIU"); if (!$sel) exit ("Error: Could not select database!"); $sql = "SELECT * FROM Equipment"; $query = mysql_query($sql); if (mysql_num_rows($query) == 0) { echo "No records found"; } else { echo "<td><select name='equipment' id='equipment' size = '8'>"; while($row = mysql_fetch_object($query)) { echo "<option Value = $row->Equip_Number>$row->Equipment_Category</option>"; } echo "</select></td>"; mysql_close($connect); } echo"<p>"; echo "Product_Details: <textarea name='Product_Details' id='Product_Details' cols='25' rows='5'>"; echo "</textarea>"; echo "<p>"; echo "Equipment_Name: <input type = 'text' name ='Equipment_Name'> </input>"; echo"<p>"; echo "Comments:<textarea name='Comments' id='Comments' cols='25' rows='5'>"; echo "</textarea>"; echo "<p>"; echo "<button type ='submit' value ='Submit' name= 'Submit'> Submit"; echo "</button>"; echo "<button type ='Reset' value ='Reset' name= 'Reset' > Reset </button>"; echo "</form>"; echo "</body>"; echo "</html>"; ?> Heres php <?php /**************************************************** Print the content of the array POST Use it to verify the content coming from the FORM ****************************************************/ //echo "<pre>"; //print_r($_POST); //echo "</pre>"; /**************************************************** Assign variables with information from the array POST ****************************************************/ $Equipment_Name = $_POST['Equipment_Name']; $Product_Details = $_POST['Product_Details']; $Equipment_Category = $_POST['Equipment_Category']; $Comments = $_POST['Comments']; /**************************************************** Connect to database ****************************************************/ $server = "localhost"; // Server name $user = "root"; // User name $pass = ""; // Password // Connect to MySQL $connect = mysql_connect($server, $user, $pass) or die( "ERROR: Connection failed!"); // Select a database to use $sel = mysql_select_db("SIU"); if(!$sel) exit("Error: Could not select database"); // Create the SQL command to insert data into the table student $sql = "INSERT INTO Equipment (Equipment_Name, Product_Details, Equipment_Category, Comments) "; $sql .= "VALUES ('$Equipment_Name','$Product_Details','$Equipment_Category','$Comments')"; // Perform Query $query = mysql_query($sql); $numRows = mysql_affected_rows($connect); if($numRows > 0) echo "<h2>New Order Inserted:</h2>"; else echo "<h2>Fail to Add New Order</h2>"; // Close the database connection mysql_close($connect); ?> here is the database if needed DROP DATABASE if exists SIU; CREATE DATABASE if not exists SIU; USE SIU; CREATE TABLE EQUIPMENT ( Equip_Number INT NOT NULL, Equipment_Name CHAR (40) NOT NULL, Product_Details TEXT NOT NULL, Equipment_Category CHAR(30) NOT NULL, Comments TEXT NOT NULL, Primary Key(Equip_Number) )engine=innodb; CREATE TABLE Equipment_Receipt( Order_ID INT NOT NULL AUTO_INCREMENT, Request DATE NOT NULL, Pickup DATE NOT NULL, Name CHAR(40) NOT NULL, Buliding CHAR(30) NOT NULL, SIU_STATUS CHAR(20) NOT NULL, Dept CHAR (25) NOT NULL, Dwag_Tag INT NOT NULL, Phone INT NOT NULL, Comments TEXT, Equip_Number INT NOT NULL, Primary Key(Order_ID), Foreign Key(Equip_Number) REFERENCES EQUIPMENT(Equip_Number) )engine=innodb; INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 0100,'Dell Latitude Laptop', 'Operating System: Windows 7, Microsoft Office 2007, Adobe PDF ', 'Laptop', 'None'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 2020, 'Dell OptiPlex 360 Desktop','Operating Systems:Windows 7 and Linux, Microsoft and Open Office' , 'Dekstop', 'None'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 2030, 'Flat Panel Monitor',' Mointor Size 20' , 'Monitor', 'Comes with the cables'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 0200, 'Dynex DVD Player ', 'Formate:DVD, DVD+R, DVD+RW, DVD-R, DVD-RW, CD, MP3 and WMA','DVD', 'None'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 0300, 'JVC VCR Player', 'None','VCR', 'None'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 0500, 'Epson Multimedia Projector ', ' S-video, PC video and RCA audio inputs', 'Projector', 'None'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 0400, 'Dynex® - 40 inch LCD' , 'None', 'Televison', 'None'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 0600, 'Stero HeadPhone ' , 'None', 'HeadPhone', 'None' ); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 6061, 'Nikon Megapixel Camera', ' Comes with 50MB internal memory', 'Digital Camera', 'Ask for the guide book'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 661, 'Sony - Handycam DVD Camcorder', ' Rechargeable battery, AC adapter, USB cable, A/V connecting cable', 'Video Camera', 'Ask for the guide book'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 441, 'Polk Audio Speaker', ' None', 'Speaker', 'none'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 491, 'Sony Microphone', 'Cable type', 'Microphone', 'Comes with the stand'); INSERT INTO Equipment (Equip_Number ,Equipment_Name, Product_Details,Equipment_Category, Comments) VALUES( 391, 'Sony Audio System', 'Full audio system', 'Audio System', 'Microphone, Speaker and audio channel included'); INSERT INTO Equipment_Receipt (Request ,Pickup, Name ,Buliding, SIU_STATUS, Dept, Dwag_Tag, Phone, Comments, Equip_Number) VALUES('2009-11-06', '2009-11-07', 'John Smith', 'Life Science 2', ' Student', 'Education', 850031208, 618-354-8995, 'Comes with Satnd', 0100); INSERT INTO Equipment_Receipt (Request ,Pickup, Name ,Buliding, SIU_STATUS, Dept, Dwag_Tag, Phone, Comments, Equip_Number) VALUES('2009-11-07', '2009-11-08', 'Frank Hatten', 'Communications', ' Teacher', 'Liberal Arts', 850023345, 618-392-3047, 'None', 2020); INSERT INTO Equipment_Receipt (Request ,Pickup, Name ,Buliding, SIU_STATUS, Dept, Dwag_Tag, Phone, Comments, Equip_Number) VALUES('2009-11-09', '2009-11-12', 'Ed Thomas', 'Library', ' Teacher', 'Agriculture', 850031190, 618-453-1017, 'None', 2030 ); INSERT INTO Equipment_Receipt (Request ,Pickup, Name ,Buliding, SIU_STATUS, Dept, Dwag_Tag, Phone, Comments, Equip_Number) VALUES('2009-11-10', '2009-11-12', 'Sunny Okoro', 'Pullman Hall', 'Student', 'Information Systems Technology', 850123407, 618-395-8551, 'Ask for the guide book', 0200); INSERT INTO Equipment_Receipt (Request ,Pickup, Name ,Buliding, SIU_STATUS, Dept, Dwag_Tag, Phone, Comments, Equip_Number) VALUES('2009-11-09', '2009-11-13', 'Ryan Fehrenbacher', 'Wham Buliding', 'Student', 'Engineering', 850031299, 618-453-1032, 'Ask for the guide book', 0300); Link to comment https://forums.phpfreaks.com/topic/184851-insert-into-database-help-please/ Share on other sites More sharing options...
dawsba Posted December 12, 2009 Share Posted December 12, 2009 what data is your print_r displaying? can u echo your sql and display here, on a not i would use ` ` around your table and field names also for your posts u could use <code>foreach($_POST as $k=>$v){$$k=$v;}</code> Link to comment https://forums.phpfreaks.com/topic/184851-insert-into-database-help-please/#findComment-975789 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.