nadz Posted July 7, 2007 Share Posted July 7, 2007 ok, i am just trying to make a simple form at the moment that logs the users id and name. basically i have a form that asks for the users name and email address, it checks if the email address is registered by checking if there is an "id" present with the email address in my "user" table. I would like it to create an entry in my "crush" table with the users name and id. and if the email address does not match an id it should echo "id incorrect" or the login form. i have this code but it always echoes invalid id, even when i put the right details in: <?php if (!$_POST) { include("form.php"); } $dbhost = 'mysql.d****.co.uk'; $dbuser = 'nextman'; $dbpass = '*****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'main'; mysql_select_db($dbname); $fetch=mysql_fetch_assoc(mysql_query("SELECT `id` FROM `user` WHERE `email`='$email'")); $id = $fetch['id']; $id = isset($_GET['id'])?$_GET['id']:0; if ($id > 0) { $name = $_POST['txtName']; $email = $_POST['txtEmail']; mysql_query("INSERT INTO `crush` (name, id) VALUES ('$name', '$id')"); include("member.php"); } else { echo 'invalid entry id'; } ?> Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/ Share on other sites More sharing options...
nadz Posted July 7, 2007 Author Share Posted July 7, 2007 UPDATE: ive got the form to work properly now, but i have another problem. At the bottom of the original form there is "invalid id" - Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292119 Share on other sites More sharing options...
cooldude832 Posted July 7, 2007 Share Posted July 7, 2007 your idea is good except try using (take all you need in the query because if its right it prevents a need for a second query to retrive info (don't use get/post info for you will develop caps issue also you never define $email maybe you are missing: <?php $email = $_POST['email']; $fetch = mysql_query("SELECT `id`, `username`, `email` FROM `user` WHERE `email`='$email'"); if(mysql_count_rows($fetch) > 0) { //User is already a member load the mysql data } else { //user is not part of the table so add to table via an insert } ?> Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292120 Share on other sites More sharing options...
cooldude832 Posted July 7, 2007 Share Posted July 7, 2007 your issue is because your logic never says don't query. It always queries it merly ads the form if !$_POST try saying if($_POST) {query and test} else { include(form.php); } Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292121 Share on other sites More sharing options...
nadz Posted July 7, 2007 Author Share Posted July 7, 2007 UPDATE: ive also fixed that problem. sorry, i just took one detailed look at every single line of code and found 2 mistakes. thankyou for the quick replies anyway. Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292122 Share on other sites More sharing options...
cooldude832 Posted July 7, 2007 Share Posted July 7, 2007 probably not a big deal for your page but using the if($_POST) isn't a very common method of testing if a form is submitted there are better ways you might want to explore Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292124 Share on other sites More sharing options...
nadz Posted July 7, 2007 Author Share Posted July 7, 2007 hi, ive tried using this code instead but im getting a "Call to undefined function mysql_count_rows()" error. i got everything to work using the old code but i thought id try this if its more efficient. your idea is good except try using (take all you need in the query because if its right it prevents a need for a second query to retrive info (don't use get/post info for you will develop caps issue also you never define $email maybe you are missing: <?php $email = $_POST['email']; $fetch = mysql_query("SELECT `id`, `username`, `email` FROM `user` WHERE `email`='$email'"); if(mysql_count_rows($fetch) > 0) { //User is already a member load the mysql data } else { //user is not part of the table so add to table via an insert } ?> Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292187 Share on other sites More sharing options...
cooldude832 Posted July 7, 2007 Share Posted July 7, 2007 whops sorry its mysql_num_rows not mysql_count_rows try that and it should help Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292188 Share on other sites More sharing options...
nadz Posted July 8, 2007 Author Share Posted July 8, 2007 hi thanks, i just realised that and changed it but now im getting "mysql_num_rows(): supplied argument is not a valid MySQL result resource" Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292189 Share on other sites More sharing options...
nadz Posted July 8, 2007 Author Share Posted July 8, 2007 hi, i fixed the issue. thanks alot for your help. Link to comment https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/#findComment-292197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.