Validating data already existing in the db

Discussion in 'PHP' started by vishals81, Jun 25, 2008.

  1. #1
    $results = mysql_query("SELECT COUNT(*)
    FROM ip
    WHERE grid_no = '$grid_no'");

    $grid_no = mysql_result($results,0);

    if ($grid_no > 0) {
    echo"The grid_no that you have entered already exists in the db";
    exit();
    }



    $sql= "INSERT INTO ip (name, address, city, country, grid_no)
    VALUES ('$name','$address','$city','$country', '$grid_no')";
    $result = mysql_query($sql);
    //mysql_query($sql);
    //print_r ($_POST);
    echo"</br>";
    echo "You have added the following details";
    //$result = mysql_query("SELECT * FROM user where name='$name'");
    $result = mysql_query("SELECT name, address, city, grid_no, product FROM ip order by country DESC");
    if ($myrow = mysql_fetch_array($result)) {
    echo "<table border=1 width=1000>\n";
    echo "<tr><td>name</td><td>address</td><td>city</td><td>country</td><td>grid no</tr>\n";
    do {
    printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
    $myrow[0], $myrow[1], $myrow[2], $myrow[3], $myrow[4]);
    } while ($myrow = mysql_fetch_array($result));
    echo "</table>\n";
    echo"<br><br>Thank you for entering your details.<br>Please click on main to enter another entry.</br></br>";
    } else {
    echo "Sorry no records were found";
    }

    }

    I am finding it difficult to display an echo statement for the above that checks for already exisitng grid_no in the db.
    If I enter a grid_no that is already in my db then it correctly displays me the message" The grid_no that you have entered already exists in the db", however if i try to add a completely new grid number then that entry does not go into the db although the table is displayed with the existing grid_nos present in the db(New entry not added)

    Please could you help to resolve this issue so as to if I enter a grid_no that is not in the db it also gets added to the db.

    I have defined the grid_no as UNIQUE in my db structure. Would appreciate your help on this.
     
    vishals81, Jun 25, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    try to use mysql_error after the query, maby you have an error in your insert query?
     
    EricBruggema, Jun 26, 2008 IP
  3. vishals81

    vishals81 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi I tried to put the following stmt after the INSERT stmt,

    $result = mysql_query($sql) or die ('Unable to insert' . mysql_error());

    This returned me,

    "Unable to insertDuplicate entry '0' for key 2"

    For a matter of fact I am not able to figure out the issue as to why does it display this message when I am entering a new grid_no entry to the db.

    Also as expected if I enter an existing grid_no from the db, the message "Grid_no already exists" is correctly displayed to me.

    I am new to PHP and would really appreciate if you help me resolve the above issue
     
    vishals81, Jun 26, 2008 IP