-2

I am trying to update my database through a input box in html. So far everything works exception of the name. It wont update the name if its actual characters such as 'toys'. It will however update if I type in numbers such as 45634. My database value for "name" is varchar(15).

Here is my code to update the name in the database.

if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `item` SET `name`=$data[4] WHERE itemID = $data[0]";
    try{
        $update_Result = mysqli_query($conn, $update_Query);

    if($update_Result)
    {
        if(mysqli_affected_rows($conn) > 0)
        {
            echo 'Data Updated';
        }else{
            echo 'Data Not Updated';
        }
    }
} catch (Exception $ex) {
    echo 'Error Update '.$ex->getMessage();
    }
}

My HTML box is here just the name part of it

<input type="text" name="name" placeholder="name" value="<?php echo $name;?>"><br><br>

and this is how I store the value name there is others but they work fine only having trouble with the name

function getPosts()
{
    $posts = array();
    $posts[4] = $_POST['name'];
    return $posts;
}

1 Answers1

-1

If name type is VARCHAR, you have to put string into it, not integer. Try to change SET `name`=$data[4] into SET `name`='".$data[4]."'.

  • worked like a charm I was unaware you needed both ' ' " ". I originally had tried it as `name`='$data[4]' Got it working now though thanks. – aaagggg1233 Apr 22 '20 at 06:37