如何删除重复的行并继续该值?

时间:2019-06-25 03:37:22

标签: php

enter image description here如果有两个相同的名称,一个名称将被删除,但数字将递增以显示总数为两个。编写php脚本

2 个答案:

答案 0 :(得分:0)

嗨,如果您使用的是php和数据库。

undefined

使用选择查询位置并获取值,然后

$oneDataFromdatabase = SELECT * from table_name where name = $name

答案 1 :(得分:-1)

我认为您正在询问类似用户是否一次又一次输入相同名称(或电子邮件)的方法。您可以执行类似的操作。 在下面的示例中,我正在创建一个不为null的电子邮件字段。我想您可以在中添加额外字段作为 email_count 。请在此示例中注意 您的数据库结构应如下所示。 电子邮件不为空,电子邮件计数为空

<?php
//start the session first
session_start();
//check is the user is a valid user
if (isset($_SESSION['UserName'])) {
    //include the db connection
    include '../../public_pages/dbcon.php';
    //fetch data from user inputs
    $createdBy = $_SESSION['UserName'];
    $email= $_POST['email'] ;
     //remove white spaces
    $removeWhiteSpaces = trim($email);
    //now check is that email available in your db
    $sqlCheck = "SELECT * FROM users WHERE email= '$email'";
     //query the result with the connection
    $result = mysqli_query($conn,$sqlCheck);
    $rowResult = mysqli_fetch_assoc($result);
    //count the number of rows that result output
    $numRows = mysqli_num_rows($result);
    //if result rows are greater than 0 it means that email almost exist in the system.So you dont need to enter email here.What you can do is to update the email_count
    if($numRows>0){
        //first take the current value of the relevant email count
        $currentValue =$rowResult['email_count'];
        //now increase the current value with additional ::where('active', 1)->count();
        $newValue = $currentValue + 1 ;
        //Now you can increase the count
        $sqlUpdateEmailCount = "UPDATE users SET email_count  = '" . $newValue . "' WHERE email ='".$email."' ";
        if ($conn->query($sqlUpdateEmailCount) === TRUE) {
           //so after updating email count send user back to a page
        header('Location: ./addEmail.php');
      //also you can alert something like this
        echo "<script> alert('Sorry email almost exist in t the system. But email count had been increased ') </script>";
      //then close the connection
        die();

    }else{
    try {
         //if result output no rows it means that email does not exist in the db. So that you can let it to be inserted
        $sqlInsertNewEmail = "INSERT INTO users(email,eamil_count,created_by) VALUES ('" .$email. "',0,'".$createdBy."')";
        if ($conn->query($sqlInsertNewEmail) === TRUE) {
            $conn->close();
            header('Location: ./addEmail.php');
            echo "<script> alert('New email added successfully ') </script>";
            die();
        } else {
           header('Location: ./addEmail.php');
               echo "<script> alert('Sorry something went wrong ') </script>";
//show the error
            echo "Error: " . $sql . "<br>" . $conn->error;
            $conn->close();
            die();
        }
    } catch (Database_Exception $exception) {
        header('Location: ./addEmail.php');
        echo "<script> alert('Sorry there is an error') </script>";
        die();
    }
}
}

希望您可以从中获得一些东西。谢谢