Greysoul Posted January 3, 2011 Share Posted January 3, 2011 yeah i've completey forgotten everything i learned last year with php and have a simple question. i'm just making a very easy insert but i dont want when i hit the submit button for it to have to go to another page to process the insert. i just want when i hit the add button for it to insert it, and that be it. this is what i have so far but clearly i've forgotten something as its not working. any suggestions? <?phpecho "<tr>";echo "<td>" . "<input type='text' size='10' name='billname' class='form' value=''>" . "</td>";echo "<td>" . "<input type='text' size='18' name='projectedamount' class='form' value=''>" . "</td>";echo "<td>" . "<input type='text' size='8' name='duedate' class='form' value=''>" . "</td>";echo "<td>" . "<input type='text' size='10' name='company' class='form' value=''>" . "</td>";echo "<td>" . "<input type='text' size='17' name='paymentmethod' class='form' value=''>" . "</td>";echo "</tr>";echo "<tr>";echo "<td colspan='5'>" . "<input class='form' type='Submit' value='Add' name='Add'>" . "</td>";echo "</tr>";echo "</table>";if($_POST['Add']){mysql_query("INSERT INTO bills (Name, ProjAmnt, DueDate, Company, PayMethod)VALUES('$_POST[billname]','$_POST[projectedamount]','$_POST[duedate]','$_POST[company]','$_POST[paymentmethod]'");}?> Link to comment Share on other sites More sharing options...
Greysoul Posted January 3, 2011 Author Share Posted January 3, 2011 think it may have something to do with the primary ID key field..i have it numbered. don't recall how to make it just automatically add the next number :3 could be wrong though..actually seems like the button isn't firing at all anyone out there?....didn't add my form tags..*walks away slowly* Link to comment Share on other sites More sharing options...
birbal Posted January 3, 2011 Share Posted January 3, 2011 your form is not complete..you have to write where the requests you want to send...it should be like.. if(isset($_POST['add'])){//process the form}else{//show up the formecho "<form action='yourpagelocation.php' method='post'><input type=text....................<input type=text....................<input type=text....................<input type=submit name='add'....</form>";} you can put $_SERVER['PHP_SELF'] instead of yourpagelocation.php Link to comment Share on other sites More sharing options...
Greysoul Posted January 3, 2011 Author Share Posted January 3, 2011 changed it up a little..and i'm getting error back "Duplicate entry '0' for key 1". i'm not sure how to resolve this..does it have to do with the ID field? if($_POST['Add']){ while (isset($_POST['billname'])) {$billname=$_POST['billname'];$projectedamount=$_POST['projectedamount'];$duedate=$_POST['duedate'];$company=$_POST['company'];$paymentmethod=$_POST['paymentmethod'];$query="INSERT INTO bills(Name, ProjAmnt, DueDate, Company, PayMethod)VALUES ('".$billname."','".$projectedamount."','".$duedate."','".$company."','".$paymentmethod."')";mysql_query($query) or die(mysql_error());}} Link to comment Share on other sites More sharing options...
justsomeguy Posted January 3, 2011 Share Posted January 3, 2011 What is the first key defined on the table? If it's a primary key field, do you have that set to auto increment? Link to comment Share on other sites More sharing options...
Greysoul Posted January 3, 2011 Author Share Posted January 3, 2011 What is the first key defined on the table? If it's a primary key field, do you have that set to auto increment?My ID field is my key, numbered 0,1,2,3,4,etc. When i try to set it to auto increment it won't let me. i get...SQL query: ALTER TABLE `bills` CHANGE `ID` `ID` INT( 20 ) NOT NULL AUTO_INCREMENT MySQL said: #1062 - Duplicate entry '1' for key 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted January 3, 2011 Share Posted January 3, 2011 Delete the field and then re-create it as a primary key int autonumber field. You can also create a new table with the correct structure and then import records like this (make sure to not import the old ID field):INSERT INTO new_table (Name, ProjAmnt, DueDate, Company, PayMethod) SELECT Name, ProjAmnt, DueDate, Company, PayMethod FROM old_tableOnce the records are imported you can delete the old one and rename the new one. Link to comment Share on other sites More sharing options...
Greysoul Posted January 3, 2011 Author Share Posted January 3, 2011 that worked..does it only let you make it auto increment when you first create it or something? also..any idea why when i did my insert form it kept inserting thousands of records until i navigated off the page? lol Link to comment Share on other sites More sharing options...
justsomeguy Posted January 3, 2011 Share Posted January 3, 2011 You're telling it to loop while this condition is true:while (isset($_POST['billname'])) {That's never going to be false unless you explicitly unset that array element. Maybe a loop isn't what you're looking for in that case. Link to comment Share on other sites More sharing options...
Greysoul Posted January 3, 2011 Author Share Posted January 3, 2011 yeah was using an example of some much more complex stuff i was doing before oops, works now..thanks for the help Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.