Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. preg_split & foreach loop - $_POST variables into an array, then into MySQL

preg_split & foreach loop - $_POST variables into an array, then into MySQL

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phphtmldatabasemysqldata-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tasoptics
    wrote on last edited by
    #1

    I'm new to PHP and I'm trying to build a simple library interface... For that I need a html form that has a text input for "keyword" and another for "author" I am hoping to split (a) "keywords" by the regular expression ";", and (b) the "authors" by the regular expression ";", to separate one author from another and then by the regular expression "," to separate the array into two separate arrays - Surname and Firstname (for the MySQL Table Author). I will also want to include in each INSERT INTO query, another variable (from the same form), being the ID_Number of the book or article... So, I am thinking if I used preg_split(";") for both (a) & (b) to give an array, say for table (a) Keyword

    I'd dearly love some feedback, I'm trying to teach myself this on the fly :sigh: If I can work out the first one (with only one split) then I can work on the nested array(s) and splitting the $author array into the same. The alternative is one ugly XHTML form, which I'd rather avoid.

    C 1 Reply Last reply
    0
    • T tasoptics

      I'm new to PHP and I'm trying to build a simple library interface... For that I need a html form that has a text input for "keyword" and another for "author" I am hoping to split (a) "keywords" by the regular expression ";", and (b) the "authors" by the regular expression ";", to separate one author from another and then by the regular expression "," to separate the array into two separate arrays - Surname and Firstname (for the MySQL Table Author). I will also want to include in each INSERT INTO query, another variable (from the same form), being the ID_Number of the book or article... So, I am thinking if I used preg_split(";") for both (a) & (b) to give an array, say for table (a) Keyword

      I'd dearly love some feedback, I'm trying to teach myself this on the fly :sigh: If I can work out the first one (with only one split) then I can work on the nested array(s) and splitting the $author array into the same. The alternative is one ugly XHTML form, which I'd rather avoid.

      C Offline
      C Offline
      chevu
      wrote on last edited by
      #2

      Hi, what you are trying to do it right. You can go with that pattern. You want some improved version then you can refer this link. http://php.net/manual/en/function.preg-split.php[^] They have included good number of example also. If you have already refer that page and you din't get any example then mention it so we can explain it.

      T 1 Reply Last reply
      0
      • C chevu

        Hi, what you are trying to do it right. You can go with that pattern. You want some improved version then you can refer this link. http://php.net/manual/en/function.preg-split.php[^] They have included good number of example also. If you have already refer that page and you din't get any example then mention it so we can explain it.

        T Offline
        T Offline
        tasoptics
        wrote on last edited by
        #3

        Thanks for the reply, seems I've scared some people off - all I need is to split the HTML variable strings by one regular expression ";", that is what I've requested people use when using the form (and if they don't they don't)... So if I do something along the lines of this:

        $Authors = $_POST["Authors"];
        $keywords = $_POST["keywords"];
        $ID_Number = $_Post["ID_Number"];
        $keyword = preg_split(";",'$POST_[keywords]',-1,PREG_SPLIT_NO_EMPTY)
        mysql="INSERT INTO Keyword (Keyword, ID_Number) Values
        (FOREACH '$keyword', '$POST[ID_Number]')";
        if (!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error()); }
        echo "Table: Keyword Updated";
        $author = preg_split(";",'$POST_[Authors]',-1,PREG_SPLIT_NO_EMPTY)
        mysql="INSERT INTO Author (Author, ID_Number) Values
        (FOREACH '$author', '$POST[ID_Number]')";
        if (!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error());}
        echo "Table: Author Updated";

        I should not only get the results I want (in both cases, in each table, a row with both the array item (Author or Keyword) and the ID_Number? If so, that will be wonderful, that is PRECISELY what I need... :-D That plus the feedback for each table should be good - I can always remove that later, first of all I have to get the code to work :wtf:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups