0
 <?php if(isset($_POST['submit'])) 
    { 
      $tadd=$_POST["tadd"]; //getting values 
      $pname=$_POST["pname"]; 
      $date=$_POST["date"];
      $result=mysql_query("insert into pannel(tadd,pname,date)values('$tadd','$pname','$date')");
      echo "<script type='text/javascript'>
           alert('Quotation Generated Successfully!')
      </script>"; 
      } ?> 
     <center>
     <h1>Title</h1>
     </center>

 <form name="form" method="post" action="" onSubmit="submit;">
 <center><table border="0" cellspacing="0" style="width:350px">

 <tr> <td><b>To Address</td> <td><textarea name="tadd" rows="5"
 cols="30"></textarea></td></tr>
     <tr> <td><b>Project Name</td> <td><input type="text" name="pname" required></td></tr>


  <tr> <td><b>Date</td> <td><input type="text" name="date"
 id="datepicker" required></td></tr>


 <tr> <td colspan="2" align="center"><input type="submit" name="submit"
 value="submit"/></td> </tr></center>  </table>   </form>

I have one record in my database with

id  tadd   pname    date
1    hello  vvv      22/10/2014

if i insert values into database again it should data already inserted

please help me regarding this issue

Dinesh
  • 4,066
  • 5
  • 21
  • 35
vyuser
  • 31
  • 1
  • 1
  • 5
  • 1
    It should **what** data already inserted? – hd1 Oct 14 '14 at 03:06
  • You can make your column(s) as UNIQUE as already stated, or you can check on SELECT with `mysql_num_rows()`, or `mysqli_num_rows()` or `rowCount()` if using PDO. If data exists in a particular row, you basically abort if it does. Many other ways to do this; these are but a few. – Funk Forty Niner Oct 14 '14 at 03:09
  • You shouldnt be using `mysql` as it is depraceted use either `mysqli` or `PDO` and do you mean that you would like this to overwrite the current data and not add a new line? – akaBase Oct 14 '14 at 03:10
  • The HTML is malformed (the `table` and `center` tags are nested incorrectly). It is also very old-fashioned (tables are for data, not forms, and the `center` tag is something that belongs to the past). The `onSubmit` attribute will also not do anything. And the `b` tag is not closed. And the `mysql_query` function is deprecated. And you are not checking that the POST variables are set before using them. And... well, you should find a more up-to-date tutorial. – Sverri M. Olsen Oct 14 '14 at 03:31

1 Answers1

0

You can achieve this using mysql_num_rows() which is one way to do this, which I believe the goal is to avoid duplicates.

Sidenote: You can also set your column(s) as UNIQUE to avoid duplicates.

N.B.: I used the pname column as an example. It's up to you to check which one will always be unique in regards to a username for instance.

$query = "SELECT * FROM pannel where pname = '".$pname."'";

$result = mysql_query($query);

if(mysql_num_rows($result) > 0){

    echo "Already exists.";
}
else{
   mysql_query("insert into pannel (tadd, pname,date) values ('$tadd','$pname','$date')");
}

Do sanitize your data:

$tadd = mysql_real_escape_string($_POST["tadd"]);

and do the same for the others.

Even better, use mysqli with prepared statements, or PDO with prepared statements.

  • They're much safer, because your present code is open to SQL injection.

Footnotes:

You should get rid of onSubmit="submit;" in your form. As outlined in comments, it's not going to do anything.


Edit:

<?php 

// assuming DB connection has been made.

if(isset($_POST['submit'])) {

$tadd= mysql_real_escape_string($_POST["tadd"]);
$pname= mysql_real_escape_string($_POST["pname"]);
$date= mysql_real_escape_string($_POST["date"]);

$query = "SELECT * FROM pannel where pname = '".$pname."'";

$result = mysql_query($query);

    if(mysql_num_rows($result) > 0){

        echo "Already exists.";
        exit;
    }
    else{
       mysql_query("insert into pannel (tadd, pname,date) values ('$tadd','$pname','$date')");

       echo "<script type='text/javascript'>alert('Quotation Generated Successfully!')</script>";
        }



  } // brace for if(isset($_POST['submit']))

 ?>

<!DOCTYPE html>
<head></head>

<body>
 <center><h1>Title</h1></center>

 <form method="post" action="">
 <div align="center">
    <center>

 <table border="0" cellspacing="0" style="width:350px">

 <tr> <td><b>To Address</td> <td><textarea name="tadd" rows="5" cols="30"></textarea></td></tr>
     <tr> <td><b>Project Name</td> <td><input type="text" name="pname" required></td></tr>

 <tr> <td><b>Date</td> <td><input type="text" name="date" id="datepicker" required></td></tr>


 <tr> <td colspan="2" align="center">
 <input type="submit" name="submit" value="submit"/>
 </td> </tr>
 </table>
    </center>
 </div>
 </form>

</body>
</html>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • @vish Describe "not working". Are you in fact connected to DB? – Funk Forty Niner Oct 14 '14 at 04:15
  • its inserting into database multiple times – vyuser Oct 14 '14 at 04:17
  • @vish Inserting multiple times at once, or meaning you can still enter duplicate data? – Funk Forty Niner Oct 14 '14 at 04:19
  • id tadd pname date 1 hello vvv 22/10/2014 2 hi bbb 22/4/2014 – vyuser Oct 14 '14 at 04:22
  • @vish I'm sorry, I don't understand what you mean. Please explain in detail. – Funk Forty Niner Oct 14 '14 at 04:23
  • submitted first time in database id tadd pname date 1 hello vvv 22/10/2014 if i submit second time it stored as id tadd pname date 2 fdsv ggg 20/10/2014 i dont want to insert second time into database – vyuser Oct 14 '14 at 04:28
  • @vish I honestly don't know what you wish to achieve. You only want ONE piece of data in your table and not allow for anymore after that? I'm confused. What is the goal you wish to achieve? – Funk Forty Niner Oct 14 '14 at 04:30
  • want ONE piece of data in my table..i am editing code which u gave thanks for ur help – vyuser Oct 14 '14 at 04:31
  • fred submitting first time inserting into database after refreshing page and if we insert it should display already inserted – vyuser Oct 14 '14 at 04:41
  • @vish It will only show it is already inserted if there is matching data in the `pname` column. If you try and enter `bbb` for example, something you said you entered already, well if it's still in your database, and you try to enter it again, it will show you the message that it "Already exists". – Funk Forty Niner Oct 14 '14 at 04:44
  • fred but if i enter any data second time into database it should display "already exists" – vyuser Oct 14 '14 at 04:47
  • Then you need to add conditions to the `WHERE` clause then, if you want to check if any of the data entered exists in your table and not just a single row. If that gets too complicated, then you will need to modify your columns to be `UNIQUE`, which is the simplest method without having to first do a `SELECT`. – Funk Forty Niner Oct 14 '14 at 04:50
  • can u provide me query once please – vyuser Oct 14 '14 at 04:53
  • @vish `$query = "SELECT * FROM pannel where tadd = '".$tadd."' AND pname = '".$pname."' AND date = '".$date."' ";` -or- `$query = "SELECT * FROM pannel where tadd = '".$tadd."' OR pname = '".$pname."' OR date = '".$date."' ";` this is the best I can do. Try one of them once at a time, and not together. – Funk Forty Niner Oct 14 '14 at 04:56
  • @vish Sorry, I don't have time to chat. I cannot help you any longer, sorry. – Funk Forty Niner Oct 14 '14 at 05:02