Вопрос

I need to know if I can query and check database table data before the php scrip will INSERT INTO that table ?

I have made an html with 2 fields and submit button(POST), and I also have no trouble with adding data to database table and getting its result, but I have duplicates and I don't want to have it ... I want script to check my data , the entering data and if no duplicate then add it.

Please assist.

Thank you

Code

<?php


$con=mysqli_connect("localhost","root","Opera1","railway");


if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: ", mysqli_connect_error;
}

$data1 = $_POST['data1'];
$data2 = $_POST['data2'];



$sql="replace into pipe (data1, data2) 
values ('$data1','$data2')";



if(!mysqli_query($con,$sql))
{
die('Error : ' . mysqli_error($con));
}


echo "Record to Registraton added";

$result = mysqli_query($con,"SELECT * FROM pipe");

echo "<table border='1'>
<tr>
<th>Data1</th>
<th>Data2</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
 echo "<tr>";
  echo "<td>" . $row['data1'] . "</td>";
  echo "<td>" . $row['data2'] . "</td>";
  echo "</tr>";
  }
echo "</table><br><br>";


mysqli_close($con);


?>
Это было полезно?

Решение

       <?php


    $con=mysqli_connect("localhost","root","Opera1","railway");


    if(mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: ", mysqli_connect_error;
    }

    $data1 = $_POST['data1'];
    $data2 = $_POST['data2'];

    $sql1="select * from pipe";
    $q1=mysql_query($con, $sql1);
    $t=0;
    while($row1=mysql_fetch_array($q1))
    {

    if($row1['data1']== '$data1' || $row1['data2']=='$data2')
    {
    $t=1;
    break;
    }
    }
    if($t==1)
     { 
       echo "Duplicate Entry!";
     }
    else
    {

    $sql="replace into pipe (data1, data2) 
    values ('$data1','$data2')";



    if(!mysqli_query($con,$sql))
    {
    die('Error : ' . mysqli_error($con));
    }


    echo "Record to Registraton added";

    $result = mysqli_query($con,"SELECT * FROM pipe");

    echo "<table border='1'>
    <tr>
    <th>Data1</th>
    <th>Data2</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
      {
     echo "<tr>";
      echo "<td>" . $row['data1'] . "</td>";
      echo "<td>" . $row['data2'] . "</td>";
      echo "</tr>";
      }
    echo "</table><br><br>";

    }

    mysqli_close($con);


    ?>

Другие советы

If you do not want to handle teh case when there is duplicate just use INSERT IGNORE INTO instead of INSERT INTO. It would work exactly as INSERT but would not fail if there are duplicates.

If you want to eliminate the duplicate records in 1 stroke, you can use REPLACE command available in mysql ( ref: http://dev.mysql.com/doc/refman/5.0/en/replace.html ). If a row with the same value already exists then it will delete the existing row and add new else it will insert the new record.

You can even look for other options at Insert into a MySQL table or update if exists

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top