Nydigov
Nydigov

Reputation: 15

Submit Button To Update Query

I have researched a lot and I think my error is something super simple or very hard, either way, this is my first question on StackOverflow.

So when I run this:

$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";

it updates my database just fine but now I try to put this under a submit button and it won't work?

<?php
    $link = mysqli_connect("localhost", "root", "TESTTEST", "sales");

    if (mysqli_connect_error()) {
        die("Could not connect to database");
    }
    mysqli_query($link, $query);
    if ($_POST['update']) 
    {
        echo 'Updating...';
        $query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
        echo '<br>Successfully Updated';
    } else {
        echo 'Unsuccessful';
    }
?>

It echoes successfully updated then I check back to the database and nothing changed... Hopefully, you could help me! Thank you for reading, James.

First PHP tag is there just not showing on blockquote.

Upvotes: 0

Views: 939

Answers (2)

Gravindra
Gravindra

Reputation: 34

firstly

$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = '1'; ";

with single quotes

Then the mysqli_query($link, $query); part.

Upvotes: 0

Denny Sutedja
Denny Sutedja

Reputation: 538

move your mysqli_query under query like this.

$link = mysqli_connect("localhost", "root", "TESTTEST", "sales");

    if (mysqli_connect_error()){

    die("Could not connect to database");

    }

    if ($_POST['update']) {

        echo 'Updating...';

        $query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";

        mysqli_query($link, $query);

        echo '<br>Successfully Updated';

    } else{

        echo 'Unsuccessful';

    }

Upvotes: 1

Related Questions