skip to Main Content

I have a simple code (very simple one) that I was using to try something out for a work and I was trying a function to work with the variables of a form radio in post method, to update my SQL table with the output of the form. But when I’m going to try it, it doesn’t update and gives me a notice.

It has something to do with the query (because the error says is in that line of the code) but I still don’t know what it is.

I tried to change the syntax of the SQL sentence in different ways. I changed the user I was going to use to change the “image_value” column. I even checked the syntax of the query in phpmyadmin, and it worked.

Here is the php code:

<?php
mysql_connect("localhost","root","");

function user_image($value){
    print_r($value);

    //This is the problem
    $query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'";

    mysql_query($query);
}
?>

And here i have the code of the form and how I’m using the function (if there is any mistake that I haven’t seen)

<form method="post" action="">
        <input type="radio" name="1" value="1">imagen1
        <br>
        <input type="radio" name="2" value="2">imagen2
        <br>
        <input type="radio" name="3" value="3">imagen3
        <br>
        <input type="radio" name="4" value="4">imagen4
        <br>
        <button type="submit"><span>Submit</span></button>
    </form>
    <?php
    user_image($_POST);
    ?>

2

Answers


  1. Your problem is you are passing the complete object $_POST.

    You don’t specify if your radio name is correct (The name of your radio as a number from 1 to 4).

    In the case, you are trying to set the value of image_value from a radio button should be.

    <form method="post" action="">
            <input type="radio" name="image_value" value="1">imagen1
            <br>
            <input type="radio" name="image_value" value="2">imagen2
            <br>
            <input type="radio" name="image_value" value="3">imagen3
            <br>
            <input type="radio" name="image_value" value="4">imagen4
            <br>
            <button type="submit"><span>Submit</span></button>
        </form>
    
    <?php
        if (isset($_POST['image_value'])) {
           user_image($_POST['image_value']);
        }
    ?>
    

    and your function

    function user_image($value) {
        mysql_connect("localhost","root","");
    
        print_r($value);
    
        //This is the problem
        $query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'"; //ID should be dynamic base on the user I guess
    
        mysql_query($query);
    }
    ?>
    
    Login or Signup to reply.
  2. $_POST pass an object value to the server, you need to specify the property you want to use, var_dump $value to understand all what it contains.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search