Storing Multiple Checkbox Data in MySQL Database with PHP

前端 未结 4 855
-上瘾入骨i
-上瘾入骨i 2021-02-10 05:30

I want to have multiple checkbox values be stored into one field in a database. (Ex. 1, 24,56,100). I am wanting to know how I can make this happen, and how does PHP read thes

4条回答
  •  鱼传尺愫
    2021-02-10 06:00

    You're going to want to go through the basics of PHP and MySQL.

    Check out tizag.com or w3schools.com, or some other sites (the tutorials are plentiful).

    Here's the basics though--and remember this, as it'll help you in your understanding.

    MySQL is a database. A database is typically used for storing data. PHP is a programming language. It's typically used for programming.

    So, some wonderful developers out there have already taken care of the steps for talking to the database from PHP. All you have to do is establish a connection.

    
    

    See mysql_connect via W3Schools

    Okay, so what you have done here, is you've connected to the database SERVER. You haven't selected a database yet. Think of it like going to a movie theater, you still have to pick a movie to watch.

    So, now we connect to our database:

        mysql_select_db("my_db", $con); //Notice we're using our connection, "$con"
    

    See mysql_select_db via W3Schools

    Once you've connected to your database, you're ready to grab some information from it.
    To do this, you need to create your SQL query.

    Something to the tune of:

    $sql = "SELECT article_id FROM user_view_blog_posts WHERE user_id = '$user_id'";
    

    See the SELECT statement via W3Schools

    Depending on how your table is set up, that will get you the list for the current user. I'm assuming that you already have the user's id (or a way to get it), since they have the ability to define their own preferences for the site.

    Now that you have the SQL query to get the articles, you need to query the database.

    $result = mysql_query($sql);
    // Now, get the data
    while($row = mysql_fetch_array($result)) {
        $articles[] = $row['article_id'];
    }
    

    That should get you your list of ID's that you need.

    I'll let you figure out the rest. It's pretty straight-forward, and you've got all of the tools that you need now. The only thing you might want to brush up on is explode and foreach.

    So, the way you store them is up to you. Look into explode for splitting them up when they're stored that way.

提交回复
热议问题