dape2009 Posted April 8, 2009 Share Posted April 8, 2009 Hi! I have had a problem for sometime now and i am not being able to solve it. I am constructing a form with php where students in a school are soupose to register for a school trip. Everything works fine on server but i need to add a function (or anything else) to avoid dubble registers. It is a simple form really and what i need is to add the code that remins the user that he/she is already registerd. Only the name needs to be checked, nothing else!. I just want to make sure that users dont register twice and is enough checking the name. I am very thankfull for any help i get. This is the code: <?php //namn = name if(empty($_POST['namn'])) $namn = "not set"; if(empty($_POST['kurs'])) $kurs = "not set"; if(empty($_POST['first_time'])) $first_time = "not set"; if(empty($_POST['more'])) $more = "not set"; if(!empty($_POST['namn'])) { $namn = $_POST['namn']; $kurs = $_POST['kurs']; $first_time = $_POST['first_time']; $more = $_POST['more']; $connection = mysql_connect("localhost", "dape0015", "ruroangu" , "dape0015") or die("Could not connect!"); mysql_select_db("dape0015") or die("Couls not choose database"); $laggTill = "INSERT INTO fritidsdagen (id , namn, kurs, first_time, more) VALUES ('$id' ,'$namn', '$kurs', '$first_time', '$more')"; mysql_query($laggTill) or die("Colud not add information!"); header('Location:tack.html'); exit(); mysql_close($connection); } ?> Thanks/Daniel Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/ Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Do a SELECT statement and see if it returns anything. Try this, I also cleaned up your code a bit: //namn = name if(empty($_POST['namn'])) $namn = "not set"; if(empty($_POST['kurs'])) $kurs = "not set"; if(empty($_POST['first_time'])) $first_time = "not set"; if(empty($_POST['more'])) $more = "not set"; if(!empty($_POST['namn'])) { $namn = $_POST['namn']; $kurs = $_POST['kurs']; $first_time = $_POST['first_time']; $more = $_POST['more']; $connection = mysql_connect("localhost", "dape0015", "ruroangu" , "dape0015") or die("Could not connect!"); mysql_select_db("dape0015") or die("Couls not choose database"); $sql = "SELECT * FROM fritidsdagen WHERE namn = '$namn'"; //ADDED $result = mysql_query($sql) or die(mysql_error()); //ADDED if(mysql_num_rows($result) == 0) { //ADDED echo "You've already regisereted for this trip."; //ADDED } else { //ADDED $laggTill = "INSERT INTO fritidsdagen (id , namn, kurs, first_time, more) VALUES ('$id' ,'$namn', '$kurs', '$first_time', '$more')"; mysql_query($laggTill) or die("Colud not add information!"); header('Location:tack.html'); exit(); } //ADDED mysql_close($connection); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804110 Share on other sites More sharing options...
dape2009 Posted April 8, 2009 Author Share Posted April 8, 2009 Thanks Maq!!!! I really appreciate your help! I´ll check it up and see if it work:) Thank again! Daniel Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804112 Share on other sites More sharing options...
dape2009 Posted April 8, 2009 Author Share Posted April 8, 2009 MMM...i published the code and still works well....But the problem is still there. It still lets me register the same names...(?) This is the code i published as you recommended: <?php //namn = namne if(empty($_POST['namn'])) $namn = "not set"; if(empty($_POST['kurs'])) $kurs = "not set"; if(empty($_POST['first_time'])) $first_time = "not set"; if(empty($_POST['more'])) $more = "not set"; if(!empty($_POST['namn'])) { $namn = $_POST['namn']; $kurs = $_POST['kurs']; $first_time = $_POST['first_time']; $more = $_POST['more']; $connection = mysql_connect("localhost", "dape0015", "ruroangu" , "dape0015") or die("Could not connect!"); mysql_select_db("dape0015") or die("Couls not choose database"); $sql = "SELECT * FROM fritidsdagen WHERE namn = '$namn'"; //ADDED $result = mysql_query($sql) or die(mysql_error()); //ADDED if(mysql_num_rows($result) == 0) { //ADDED echo "You've already regisereted for this trip."; //ADDED } else { //ADDED $laggTill = "INSERT INTO fritidsdagen (id , namn, kurs, first_time, more) VALUES ('$id' ,'$namn', '$kurs', '$first_time', '$more')"; mysql_query($laggTill) or die("Colud not add information!"); header('Location:tack.html'); exit(); } //ADDED mysql_close($connection); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804124 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Can you echo out the query to see if it's correct? $sql = "SELECT * FROM fritidsdagen WHERE namn = '$namn'"; //ADDED echo "QUERY=> " . $sql; $result = mysql_query($sql) or die(mysql_error()); //ADDED Pleas use tags. (The # sign) Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804127 Share on other sites More sharing options...
dape2009 Posted April 8, 2009 Author Share Posted April 8, 2009 I put on the server this to check the select: <?php $connection = mysql_connect("localhost", "dape0015", "ruroangu" , "dape0015") or die("Could not connect!"); mysql_select_db("dape0015") or die("Couls not choose database"); $sql = "SELECT * FROM fritidsdagen WHERE namn = '$namn'"; //ADDED echo "QUERY=> " . $sql; $result = mysql_query($sql) or die(mysql_error()); //ADDED ?> And i get this on my browser: QUERY=> SELECT * FROM fritidsdagen WHERE namn = '' Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804138 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 I bet your table is empty... You need something like this for all of your POST vars. $namn = (empty($_POST['namn'])) ? "Not set" : $_POST['namn']; Before you weren't assigning anything to $namn if $_POST['namn'] wasn't empty. Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804141 Share on other sites More sharing options...
dape2009 Posted April 8, 2009 Author Share Posted April 8, 2009 Hi!!! No, my table is not empty...i have about 15 post in it. The code i had and the one you suggested both are working fine. I am able to add post on my table with no problems. But i still can add same names which is what i am trying to avoid:) I have another file that opens the table. When i open this file on my browser i see my table. It looks like this: <?php $connection = mysql_connect("localhost", "dape0015", "ruroangu" , "dape0015") or die("Kunde inte skapa koppling!"); mysql_select_db("dape0015") or die("Colud not choose database"); $hamta = "SELECT * FROM fritidsdagen"; $resultat = mysql_query($hamta) or die("Det gick inte att hämta information från databasen!"); print("<table border=1 cellpadding=2 cellspacing=2 bordercolor=000000>"); print("<tr><td width=100> <b>Antal</b></td><td width=20> <b>Namn</b></td><td width=50> <b>Kurs</b></td><td width=40> <b>Första gång?</b></td><td width=140> <b>Behöver låna?</b></td></tr>"); while($rad = mysql_fetch_array($resultat)) { print("<tr><td>"); print($rad["id"]); print("</td><td>"); print($rad["namn"]); print("</td><td>"); print($rad["kurs"]); print("</td><td>"); print($rad["first_time"]); print("</td><td>"); print($rad["more"]); print("</td></tr>"); } mysql_free_result($resultat); ?> And this is the file that inserts the new values in the table (the one that you suggested): <?php if(empty($_POST['namn'])) $namn = "not set"; if(empty($_POST['kurs'])) $kurs = "not set"; if(empty($_POST['first_time'])) $first_time = "not set"; if(empty($_POST['more'])) $more = "not set"; if(!empty($_POST['namn'])) { $namn = $_POST['namn']; $kurs = $_POST['kurs']; $first_time = $_POST['first_time']; $more = $_POST['more']; $connection = mysql_connect("localhost", "dape0015", "ruroangu" , "dape0015") or die("Could not connect!"); mysql_select_db("dape0015") or die("Couls not choose database"); $sql = "SELECT * FROM fritidsdagen WHERE namn = '$namn'"; //ADDED $result = mysql_query($sql) or die(mysql_error()); //ADDED if(mysql_num_rows($result) == 0) { //ADDED echo "You've already regisereted for this trip."; //ADDED } else { //ADDED $laggTill = "INSERT INTO fritidsdagen (id , namn, kurs, first_time, more) VALUES ('$id' ,'$namn', '$kurs', '$first_time', '$more')"; mysql_query($laggTill) or die("Colud not add information!"); header('Location:tack.html'); exit(); } //ADDED mysql_close($connection); } ?> It wouldnt suprise though if there were many mistakes becouse i am still learning:) Daniel Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804153 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Nvm, I see where you set $namn. I don't see why my query would show $namn as empty but yours would INSERT it. Can you echo out your query: $laggTill = "INSERT INTO fritidsdagen (id , namn, kurs, first_time, more) VALUES ('$id' ,'$namn', '$kurs', '$first_time', '$more')"; echo "YOUR QUERY=> " . $laggTill; Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804158 Share on other sites More sharing options...
dape2009 Posted April 8, 2009 Author Share Posted April 8, 2009 I dont know eighther Maq. But you can be sure that there is something i am doing wrong:) You seem to understand this much better than me:)....Begginers "unluck" i will study a lilte the code i got from you and see if i can get somewhere:) Thanks a lot!! Very much appreciated!!! I go to bed now.. It is 2 am here. Dont want to get any PHP nightmares:) Catch you around! Daniel Quote Link to comment https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/#findComment-804168 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.