0

so i have a table of data in web ui

table

as soon as I click the button. all of the field data in "Status Email" changed. not just selected field that i meant. this is the sintaks sql

if($mail->Send())
{
    $query = "UPDATE nearly_inactive SET EmailSent = 'Sudah Kirim Email' WHERE  EmailSent = 'Belum Kirim Email'";
    $update = $con->prepare($query);
    $update->execute();     
}

how can i get the "update" only the data that I click on the button??

1
  • 4
    Your query doesn't uniquely distinguish between the others. The only criteria is Belum Kirim Email which matches the others. You need to grab a unique identifier such as a the primary key in the database and associate it with the checkbox, such as an ID which is commonly used as the primary key. Then pass this primary key to PHP once the checkbox is checked. Then add it to your query so you only work on the record in the database associated with the primary key. Commented Feb 7, 2022 at 7:22

3 Answers 3

1

Get specific field

In order to get the specific field from a MYSQL database

Select column FROM databse WHERE x = y

Example:

SELECT id, firstname, lastname FROM MyGuests WHERE lastname='Doe'

The issue

It's best to get a unique identifier, which no other user has used. For example a 10 digit user id code. Check that this code doesn't exist, for it to be unique.

UPDATE:

Easily use the UNIQUE SQL tag to resolve this issue.

CREATE TABLE X (
    ID INT UNIQUE 
)

Example:

SELECT id, firstname, lastname FROM MyGuests WHERE id=ryan9273__2

Update a specific field

Now that we have fixed the issue we can easily

UPDATE x SET y=z WHERE id=b

Lets fix your code:

UPDATE nearly_inactive SET EmailSent = 'Sudah Kirim Email' WHERE EmailSent = 'Belum Kirim Email'

Lets make it more dynamic

UPDATE nearly_inactive SET :email = :emailaddr WHERE EmailSent = :id

final code:

    $query = $con->prepare("UPDATE nearly_inactive SET :email = :emailaddr WHERE EmailSent = :id");
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->bindParam(':emailaddr', $emailaddr, PDO::PARAM_STR);
    $query->bindParam(':id', $id, PDO::PARAM_STR);
    $update->execute(); 

Security Matters

You are using PDO, so use bindParam aswell. Secret code enthusiast answer isn't as secure as the current code i provided!

Practice Makes Perfect

Please don't copy my code right away. learn from it and code it again ! Make it better. Also check the official PHP documentation for more info on these topics

Stay safe !

Regards,

Ryan

Sign up to request clarification or add additional context in comments.

Comments

1

you need to determine which record need to be changed based on their unique ID. usually it's the primary key of the table. so, If your primary key is enroller_id, then pass the value of enroller_id, and put it inside your sql.

if($mail->Send())
{
    //prepare your query
    $statement = $this->mysqli->prepare("UPDATE nearly_inactive SET EmailSent = 'Sudah Kirim Email' WHERE enroller_id = ?");
    
    //check for statement preparation
    if ($statement === false) {
        trigger_error($this->mysqli->error, E_USER_ERROR);
        return;
    }

    //bind the value
    $statement->bindParam("i", $id);

    //get id for the query
    $id = your_field_enroller_id;

    //execute the statement
    $statement->execute();  
}

where enroller_id is your table primary key, and $id is the value of that field primary key.

1 Comment

Don't spoonfeed. They won't learn. And don't assume they should use $_GET. the stmt preparation check if useless.
0
<?php
    $servername="localhost";
    $username="root";
    $password="";
    $dbname="demon";
    //CREATE CONNECTION
    $conn=new mysqli($servername,$username,$password,$dbname);
    //CHECK CONNECTION
    if ($conn->connect_error) 
    {
        die("connection failed:".$conn->connect_error);
    }
    $sql="UPDATE student set NAME='JohnRambo' where STUDENT_ID=1000";
    $result=$conn->query($sql);
    if ($result===TRUE) 
    {
        echo"NEW RECORD CREATED SUCCESSFULLY";
    }
    else
    {
        echo "ERROR:".$sql."<br>".$conn->error;
    }
    $conn->close();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.