Empty values being added to all mysql rows instead of just the target row

后端 未结 2 1982
不知归路
不知归路 2021-01-28 13:47

Empty columns are being added to my MySQL rows upon submitting data into my server. I am entering it into one row but all rows get at least an empty column. How can I prevent th

相关标签:
2条回答
  • 2021-01-28 14:30

    Have you attempted to print out the contents of category to the console? Is it possible that a valid category and a null category is being posted back from the source?

    you could also try encapulating the sql call with a nul check against the category. This might catch the null before its inserted.

    $category = $_POST['category'];
    $cf = $_FILES['cf'];
    
    if($category != NULL)
    {
        mysqli_query($conn, "INSERT INTO adDatabase(".$category.") VALUES(8)");
    }
    

    Lastly you could set one of the columns in the table to not allow null values. Which would allow you to put a try catch block and dispose of the empty data

    Try {
        mysqli_query($conn, "INSERT INTO adDatabase(".$category.") VALUES(8)");
    } 
    catch (exception ex)
    {
        // do nothing!
    }
    
    0 讨论(0)
  • 2021-01-28 14:50

    whenever you do an insert query, sql will insert a whole row. if you only specify one column then all the other columns will be filled with null (or whatever the default value is).

    if you want to fill the other columns in the row you can do that..

    insert into table (col1, col2) values ('val1', 'val2')
    

    If you wan tto update a column in an existing row..

    Update table set column = 'value' where col='somevalue'
    

    bottom line is, you can;t create a row and expect only a single column to be populated. that makes no sense.. where you expecting something like this to happen?

    0 讨论(0)
提交回复
热议问题