DannyM Posted February 13, 2008 Share Posted February 13, 2008 Hello everyone! I've run into a glitch in my Add Employee database. It doesn't want to add an Employee if there are no employees already in the database, and when there are, it adds the employee as many times as there are employees(IE, I have 2 employees in the database it adds the new employee twice, three employees it adds the employee three times, et cetera) <?php //Created by Danny Martin if($_COOKIE["User"] != "0001" || !$_COOKIE["User"]) { header("Location: Login.php"); } if(isset($_POST["back"])) { header("Location: userCP.php"); } //connect to the database $con=mysql_connect("***********","******","************"); //Select the database mysql_select_db("easyswipe_timecardtest",$con); //Check to see what they clicked if(isset($_POST["submit"])) { $sql="SELECT * FROM employees"; $result=mysql_query($sql,$con); while($row = mysql_fetch_array($result)) { if($row['ID'] == $_POST["ID"]) { echo "<b>ID already taken!</b>"; } else { //Insert into everything $sql=mysql_query("INSERT INTO employees(Name, ID,Address,Phone) VALUES('$_POST[Name]', '$_POST[iD]', '$_POST[Address]' , '$_POST[phone]')"); $sql=mysql_query("INSERT INTO Jan(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Feb(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Mar(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Apr(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO May(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Jun(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Jul(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Aug(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Sep(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO M1(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Nov(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO M2(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); } } } //Center everything echo'<center> <img src="logo.png"/>'; echo "<form action='addEmployee.php' method='POST'>"; echo '<b>Employee Name</b> - <input type="text" name="Name"/>'; echo '<br/>'; echo '<b>Employee Phone</b> - <input type = "text" name = "phone"/>'; echo '<br/>'; echo '<b>Employee Address</b> - <input type = "text" name = "Address"/>'; echo '<br/>'; echo '<b>Employee ID</B> - <input type="text" name="ID"/>'; echo '<br/>'; echo '<input type="submit" name="submit" value="Add Employee"/>'; echo '<input type="submit" name="back" value="Return"/>'; echo '</form>'; ?> What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/90978-more-database-troubles-not-adding-and-more/ Share on other sites More sharing options...
revraz Posted February 13, 2008 Share Posted February 13, 2008 Your INSERT statement is in your WHILE loop. Link to comment https://forums.phpfreaks.com/topic/90978-more-database-troubles-not-adding-and-more/#findComment-466276 Share on other sites More sharing options...
DannyM Posted February 13, 2008 Author Share Posted February 13, 2008 Your INSERT statement is in your WHILE loop. Oh jeeze. Thankyou, it's been bothering me. Gaah, I'll remember to check my loops from now on. Thanks! Now, where is that Topic Solved button located.. Link to comment https://forums.phpfreaks.com/topic/90978-more-database-troubles-not-adding-and-more/#findComment-466280 Share on other sites More sharing options...
revraz Posted February 13, 2008 Share Posted February 13, 2008 You should use an array for all those querries too, would cut down on your code. Link to comment https://forums.phpfreaks.com/topic/90978-more-database-troubles-not-adding-and-more/#findComment-466281 Share on other sites More sharing options...
DannyM Posted February 13, 2008 Author Share Posted February 13, 2008 Could you give me an example please? Link to comment https://forums.phpfreaks.com/topic/90978-more-database-troubles-not-adding-and-more/#findComment-466284 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.