Kiwii Posted March 16, 2010 Share Posted March 16, 2010 So. i've made a really simple form (learning the basics of mysql + php) to send someones name and age into one table and name and favourite food into another. It works but the only trouble is that it sends it four times. Like i'll press submit and then the information will appear in the database multiple times. Anyway, this is the html form code: <form action="sent.php" method="get"> Forename: <input type="text" name="fname" /><br/> Age: <input type="text" name="age" /><br/> Favourite Food: <input type="text" name="fav_food" /><br/> <input type="submit" value="Submit to database"/> </form> and this is the php: <?php mysql_connect("localhost", "admin", "1admin") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $name = $_GET['fname']; $age = $_GET['age']; $fav_food = $_GET['fav_food']; mysql_query("INSERT INTO example (name, age) VALUES('$name', '$age') ") or die(mysql_error()); mysql_query("INSERT INTO example2 (name, fav_food) VALUES('$name', '$fav_food') ") or die(mysql_error()); echo 'Added '.$name.' who is '.$age.' and loves ' .$fav_food. ' to the database.'; ?> Oh and i have mysql 5.1.36 and php 5.3.0 Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/195466-sending-form-output-more-than-once-to-mysql/ Share on other sites More sharing options...
jskywalker Posted March 16, 2010 Share Posted March 16, 2010 can you descibe the tables 'example' and example2' to us? Quote Link to comment https://forums.phpfreaks.com/topic/195466-sending-form-output-more-than-once-to-mysql/#findComment-1027134 Share on other sites More sharing options...
Kiwii Posted March 16, 2010 Author Share Posted March 16, 2010 CREATE TABLE IF NOT EXISTS `example` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) DEFAULT NULL, `age` int(11) DEFAULT NULL, KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `example2` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) DEFAULT NULL, `fav_food` varchar(50) DEFAULT NULL, KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; Quote Link to comment https://forums.phpfreaks.com/topic/195466-sending-form-output-more-than-once-to-mysql/#findComment-1027139 Share on other sites More sharing options...
jskywalker Posted March 16, 2010 Share Posted March 16, 2010 aaahhh so, the "INSERT INTO example (name, age) VALUES('$name', '$age') "" can insert multiple names and ages.... are you sure you did not hit the 'submit' button multiple times? Quote Link to comment https://forums.phpfreaks.com/topic/195466-sending-form-output-more-than-once-to-mysql/#findComment-1027150 Share on other sites More sharing options...
Kiwii Posted March 16, 2010 Author Share Posted March 16, 2010 yeah i'm sure i'm only pressing it ones, i've done it many times. i'm sure if i add a script to make it check if the name and age already exists it will only input it onces but i can't think of that script. i tried a few times but just got errors, what you think? Quote Link to comment https://forums.phpfreaks.com/topic/195466-sending-form-output-more-than-once-to-mysql/#findComment-1027157 Share on other sites More sharing options...
jskywalker Posted March 16, 2010 Share Posted March 16, 2010 you can add an index on 'name' to make sure a name will only be entered once alter table example add unique (name); a next attempt to enter an already existing name will result in an error: ERROR 1062 (23000): Duplicate entry ....... Quote Link to comment https://forums.phpfreaks.com/topic/195466-sending-form-output-more-than-once-to-mysql/#findComment-1027198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.