Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/153087-let-php-recognise-earlier-post/
Share on other sites

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);
}
?>

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);

}

?>

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)

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 = ''

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

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;

 

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.