Jump to content

The UDPATE MySQL Query Does Not Modify?


glassfish
Go to solution Solved by glassfish,

Recommended Posts

The Script:

<?php

include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/header.php");

?>
<?php

include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/connect.php");

?>
<?php

if(isset($_GET['thread_id'])) {

    $tqs = "SELECT * FROM `thread` WHERE `id` = '" . $_GET['thread_id'] . "'";
    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
    $row = mysqli_fetch_assoc($tqr);

    ?>

    <div class="div">
        <h1>Admin Modify</h1>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="text" name="title" value="<?php echo $row['title']; ?>"/><br/>
            <textarea type="text" name="caption"><?php echo $row['caption']; ?></textarea>
            <input type="submit" name="admin_update" value="Update" />
        </form>
    </div>

<?php

    // This prints e.g.:
    // 47
    echo "The Thread ID: ";
    echo $_GET['thread_id'];

    if(isset($_POST['admin_update'])){
        $tqs = "UPDATE `thread` SET `title` = '" . $_POST['title'] . "', `caption` = '" . $_POST['caption'] . "' WHERE `id` = '" . $_GET['thread_id'] . "'";
        $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
    }

}



?>
<?php

include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/footer.php");

?>

I do not see why this is not working. The "UPDATE" SQL statement has worked without the "WHERE" part and modified all of the posts. (With this the database connection works too.)

 

Though, this statement does work in PhpMyAdmin:

UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '47';

I added the "WHERE" part to the script and now it is not modifying the text in the table anymore, basically the "updates" do not happen.

 

What could be the issue here?

 

Any suggestions are much appreciated.

 

Also, I have used this way of "syntax style" in PHP, perhaps somebody could be modifying the "UPDATE" SQL statement into a different syntax style so I can try it out, I would much appreciate it.

I would also like to ask how the "syntax style" I have used is and if it can be better than this?

Edited by glassfish
Link to comment
Share on other sites

Thanks for the response!

 

The URL is at first:

http://localhost/gallerysite/admin_modify.php?thread_id=46

I tried to print the SQL statement on screen like in this following example:

    if(isset($_POST['admin_update'])){
        $tqs = "UPDATE `thread` SET `title` = '" . $_POST['title'] . "', `caption` = '" . $_POST['caption'] . "' WHERE `id` = '" . $_GET['thread_id'] . "'";
        $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
        echo "The query: " . $tqs;
    }

When I click on the "submit button" ("admin_update") then the URL turns into the following:

http://localhost/gallerysite/admin_modify.php

And I am getting a blank page, the "header", "footer" and the "stylesheet" is still there, though the SQL statement does not get printed on screen.

 

Any suggestions how to have the "printing on screen" of the SQL statement work out with this script?

Edited by glassfish
Link to comment
Share on other sites

Addendum:

 

I tried the following and it worked:

 

The Script:

<?php

    // This prints e.g.:
    // 47
    echo "The Thread ID: ";
    echo $_GET['thread_id'];

    //if(isset($_POST['admin_update'])){
        $tqs = "UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '46'";
        $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
        echo "The query: " . $tqs;
    //}

}

?>

I also tried the following and it worked:

<?php

    // This prints e.g.:
    // 47
    echo "The Thread ID: ";
    echo $_GET['thread_id'];

    //if(isset($_POST['admin_update'])){
        $tqs = "UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '" . $_GET['thread_id'] . "'";
        $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
        echo "The query: " . $tqs;
    //}

}

?>

Printed on Screen:

The query: UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '46'

Note: The "submit button" in the "if statement" is commented out.

 

It looks like there may have been an issue with the POST method.(?)

 

EDIT:

When I bring the "submit button" with the "if statement" back in it does not work anymore, though I am not seeing where the issue is with this.

EDIT 2:

Okay I am getting the following error message:

Undefined index: thread_id

Perhaps, this does happen when the URL changes to:(?)

http://localhost/gallerysite/admin_modify.php

Though, this does happen, after the "submit button" has gotten clicked.(?)

 

So, basically I click on the "submit button" and the "$_GET['thread_id']" variable is becoming not set?

Edited by glassfish
Link to comment
Share on other sites

  • Solution

Addendum:

So, basically this is how I have the script. I have commented the "If statement" with the "$_GET['thread_id']" variable out.

<?php

include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/header.php");

?>
<?php

include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/connect.php");

?>
<?php

//if(isset($_GET['thread_id'])) {

    $tqs = "SELECT * FROM `thread` WHERE `id` = '" . $_GET['thread_id'] . "'";
    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
    $row = mysqli_fetch_assoc($tqr);

    ?>

    <div class="div">
        <h1>Admin Modify</h1>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="text" name="title" value="<?php echo $row['title']; ?>"/><br/>
            <textarea type="text" name="caption"><?php echo $row['caption']; ?></textarea>
            <input type="submit" name="admin_update" value="Update" />
        </form>
    </div>

<?php
//}
?>
<?php

    // This prints e.g.:
    // 47
    echo "The Thread ID: ";
    echo $_GET['thread_id'];

    if(isset($_POST['admin_update'])){
        $tqs = "UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = '" . $_GET['thread_id'] . "'";
        $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
        echo "The query: " . $tqs;
    }

?>
<?php

include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/footer.php");

?>

After clicking on the submit button I am getting the following errors:

Notice: Undefined index: thread_id in C:\xampp\htdocs\gallerysite\admin_modify.php on line 15

Notice: Undefined index: thread_id in C:\xampp\htdocs\gallerysite\admin_modify.php on line 38

Notice: Undefined index: thread_id in C:\xampp\htdocs\gallerysite\admin_modify.php on line 41

The SQL query printed on screen:

UPDATE `thread` SET `title` = 'testing', `caption` = 'testing' WHERE `id` = ''

@Zane

I am becoming confused, why "thread_id" is even becomes not set anymore.(?) It happens after the "submit button" has gotten clicked.

EDIT:

I stored "thread_id" in the form as a hidden field, and then I go over to update the table with "$_POST['thread_id']". This works.

    <div class="div">
        <h1>Admin Modify</h1>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="thread_id" value="<?php echo $_GET['thread_id']; ?>"/>
            <input type="text" name="title" value="<?php echo $row['title']; ?>"/><br/>
            <textarea type="text" name="caption"><?php echo $row['caption']; ?></textarea>
            <input type="submit" name="admin_update" value="Update" />
        </form>
    </div>

Thanks for the answers!

Edited by glassfish
Link to comment
Share on other sites

it was saying thread_id wasn't a defined index because you were echo-ing out the thread_id get variable.  You also commented out the if (isset) portion.

 

So it seems the answer is that thread_id isn't being passed when you seem to think it is.  If you plan to use the same value on multiple pages, it's best to use the actual tool for that task... $_SESSION.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.