adamlacombe Posted June 13, 2009 Share Posted June 13, 2009 I have this code here: <?php include "connect.php"; $password = md5($password); $join="INSERT INTO admins(username,password,avatar,myspace) values('$username','$password','$avatar','$myspace')"; mysql_query($join) or die("Could not join"); //insert post print "You have registerd, <A href='main_login.php'>login</a>."; print "<form name='form' action='register.php' method='post'>"; print "Your name:<br />"; print "<input type='text' name='username' size='20'><br />"; print "Password:<br />"; print "<input type='text' name='password' size='20'><br />"; print "Avatar:<br />"; print "<input type='text' name='avatar' size='20'><br />"; print "Myspace:<br />"; print "<input type='text' name='myspace' size='20'><br />"; print "<input type='submit' name='submit' value='Join!'></form>"; ?> and when I register, it creates two rows, one with all the information like username,password ect. and another with nothing. How would i fix this? Thanks in advanced, Adam L Quote Link to comment https://forums.phpfreaks.com/topic/162061-solved-insert-into-help/ Share on other sites More sharing options...
Maq Posted June 13, 2009 Share Posted June 13, 2009 It may be because when you load the page the insert will get executed and when you submit it gets executed a second time. What is 'register.php'? Quote Link to comment https://forums.phpfreaks.com/topic/162061-solved-insert-into-help/#findComment-855124 Share on other sites More sharing options...
adamlacombe Posted June 13, 2009 Author Share Posted June 13, 2009 oh duh! i hadn't thought of that lol. register.php is just part of a script im making. Quote Link to comment https://forums.phpfreaks.com/topic/162061-solved-insert-into-help/#findComment-855127 Share on other sites More sharing options...
adamlacombe Posted June 13, 2009 Author Share Posted June 13, 2009 But exactly how would I be able to fix that problem? Quote Link to comment https://forums.phpfreaks.com/topic/162061-solved-insert-into-help/#findComment-855135 Share on other sites More sharing options...
Maq Posted June 13, 2009 Share Posted June 13, 2009 You can actually just do this all in one page. It will make things a lot easier, especially validation and readability so you don't have to go from script to script. A couple things I noticed/fixed: You weren't properly escaping your data, which in turn could result in a security issue. Learn to use mysql_real_escape_string. The query was being executed, unconditionally, with blank information, resulting in your blank entry issue. I submitted your data to the same page for the reasons stated above. There is no error or validation checking. I'm not sure if you got to this or not but users could enter blank, duplicate, or potential invalid usernames that would have otherwise been successfully INSERTed into your database. Cleaned and formatted your code a bit. include "connect.php"; if(isset($_POST['submit'])) { $password = md5(mysql_real_escape_string($_POST['password'])); $avatar = mysql_real_escape_string($_POST['avatar']); $username = mysql_real_escape_string($_POST['username']); $myspace = mysql_real_escape_string($_POST['myspace']); $join="INSERT INTO admins(username,password,avatar,myspace) values('$username','$password','$avatar','$myspace')"; mysql_query($join) or die("Could not join" . mysql_error()); //insert post print "You have registerd, login."; } print "</pre> <form name="'form'" action="'register.php'" method="'post'">"; print "Your name: "; print " "; print "Password: "; print " "; print "Avatar: "; print " "; print "Myspace: "; print " "; print "</form>";<br><br Hope this helps, good luck. Quote Link to comment https://forums.phpfreaks.com/topic/162061-solved-insert-into-help/#findComment-855136 Share on other sites More sharing options...
adamlacombe Posted June 16, 2009 Author Share Posted June 16, 2009 yep that worked! Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/162061-solved-insert-into-help/#findComment-857195 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.