how insert array into mysql??

Discussion in 'PHP' started by JoeyWong, Oct 19, 2010.

  1. #1
    hi all, I don't know how to add the sql coding.. Can somebody help me?? thanks

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Insert New Institution</title>
    <script type="text/javascript">
    function addRowToTable()
    {
    var tbl = document.getElementById('tblAddress');
    var lastRow = tbl.rows.length;
    // if there's no header row in the table, then iteration = lastRow + 1
    var iteration = lastRow;
    // var iteration = lastRow + 1;
    var row = tbl.insertRow(lastRow);

    // cell 0
    var cell0 = row.insertCell(0);
    var el = document.createElement('input');
    el.type = 'text';
    el.NAME = 'InstitutionName[]';
    el.size = 30;
    cell0.appendChild(el);

    //cell 1
    var cell1 = row.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.NAME = 'Address[]';
    el.size = 50;
    cell1.appendChild(el);

    //cell 2
    var cell2 = row.insertCell(2);
    var el = document.createElement('input');
    el.type = 'text';
    el.NAME = 'Website[]';
    el.size = 30;
    cell2.appendChild(el);

    }
    </script>
    </head>

    <body>
    <h2>Insert New Institution</h2>

    <br /><br />
    <form action="" name="" method="post">
    <table id="tblAddress">
    <tr>
    <td class="txtBase">Institution Name</td>
    <td class="txtBase">Address</td>
    <td class="txtBase">Website</td>
    </tr>
    <tr>
    <td><input name="InstitutionName[]" type="text" size="30" maxlength="100"></td>
    <td><input name="Address[]" type="text" size="50" maxlength="1000"></td>
    <td><input name="Website[]" type="text" size="30" maxlength="100"></td>
    </tr>
    </table><input type="button" name="Add" value="Add" onClick="addRowToTable();">
    <input type="submit" name="Submit" value="Submit">
    </form>


    </body>
    </html>
     
    JoeyWong, Oct 19, 2010 IP
  2. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    you should change the form action to a php file like this <form action="myfile.php"....> ...

    then you may create a table in your db, and call it for example mytable
    your table has to contain 4 fields:
    `ID`(an integer autoincrement and primary key field)
    `InstitutionName` (a varchar or text field)
    `Address` (a varchar or text field)
    `Website` (a varchar or text field)

    you can use phpmyadmin to manage your mysql db

    then in the file myfile.php add something like this:

    $DBhost=""; // usually localhost
    $DBuser=""; // your db username
    $DBpass=""; // your db password
    $DBname=""; // the db name where you have the table

    $db = mysql_connect ($DBhost, $DBuser, $DBpass)
    or die ("Error connecting MySQL");

    mysql_select_db($DBname, $db)
    or die ("Error selecting ".$DBname);

    for ($x=0; $x<count($_POST['InstitutionName']); $x++) {

    // escaping post data for db security reasons, this can be done better
    $instname=mysql_real_escape_string($_POST['InstitutionName'][$x]);
    $address=mysql_real_escape_string($_POST['Address'][$x]);
    $website=mysql_real_escape_string($_POST['Website'][$x]);

    $mysql_query = "INSERT INTO `mytable` (`InstitutionName` , `Address`, `Website`)
    VALUES ('".$instname."', '".$address."', '".$website."');";

    $insert = mysql_query($mysql_query, $db)
    or die ("Error inserting record");

    }
     
    max2010, Oct 19, 2010 IP
  3. silviuks

    silviuks Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    what you did there seems to work ok, but it don't insert into db. you can use ajax / jquery to do this, or can do what max2010 said. Assuming you already have a script that inserts the record, you can do something like this:

    $.ajax({
    type: 'post',
    url: url_to_php_script,
    data: 'name=' + instname + '&address=' + address + '&website=' + website,
    complete: function(data){
    // do something with your response ...
    }
    })

    hope you understand what I wanted to say ...
     
    silviuks, Oct 19, 2010 IP
  4. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, thanks for the help. I tested it already. But it just can save 1 data into db. Below is the code, can you help me check isn't having wrong on it. Thanks!! Then I want to ask javascript and input text there isn't I need to add [$x] also?? Thanks~

    <?php
    $DBhost="localhost"; // usually localhost
    $DBuser="root"; // your db username
    $DBpass=""; // your db password
    $DBname="inno_education"; // the db name where you have the table

    $db = mysql_connect ($DBhost, $DBuser, $DBpass)
    or die ("Error connecting MySQL");

    mysql_select_db($DBname, $db)
    or die ("Error selecting ".$DBname);

    for ($x=0; $x<count($_POST['InstitutionName']); $x++) {

    // escaping post data for db security reasons, this can be done better
    $instname=mysql_real_escape_string($_POST['InstitutionName'][$x]);
    $address=mysql_real_escape_string($_POST['Address'][$x]);
    $website=mysql_real_escape_string($_POST['Website'][$x]);

    $mysql_query = "INSERT INTO `institute` (`InstitutionName` , `Address`, `Website`)
    VALUES ('".$instname."', '".$address."', '".$website."');";

    $insert = mysql_query($mysql_query, $db)
    or die ("Error inserting record");

    }
    ?>
     
    JoeyWong, Oct 19, 2010 IP
  5. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    silviuks, thanks for your help also... But I no very familiar on ajax/ jquery... I just know and learn a bit about php/css/javascript... Anyway, thanks for your help..
    :)
     
    JoeyWong, Oct 19, 2010 IP
  6. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    max2010, Oct 20, 2010 IP
  7. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use serialize function while inserting and can use unserialize function after fetching data from the table.
     
    xpertdev, Oct 20, 2010 IP
  8. silviuks

    silviuks Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    as xpertdev said, you can use serialize / unserialize to insert array into db, but i don't think it's the case here.
    you need to insert values from 3 fields into 3 columns, so, for each set of values you'll have a row in your table. (am I right? maybe I misunderstood something)
     
    silviuks, Oct 20, 2010 IP
  9. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    php is right, the problem is in the javascript, the added input fields are not passed in the post variable
     
    max2010, Oct 20, 2010 IP
  10. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ya, you are right.. i want save the form fields into mysql like this:

    InstitutionName Address Website
    University of Malaysia Malaysia www.uni.edu.my
    University of Singapore Singapore www.uni.edu.sg
     
    JoeyWong, Oct 20, 2010 IP
  11. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks all... Finally it done... really thanks to your help.... Thanks you all very much....
     
    JoeyWong, Oct 20, 2010 IP
  12. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    May I ask another question at here?
    Like say I want insert courses into mysql. I have a selection menu which is let the users select the institute to add the courses.
    Selection Menu:
    University Malaysia
    University Singapore
    University UK
    After users select the University UK and insert the courses..
    Can I insert the selection (University UK) together into MYSQL?
    Now what I do is just can insert courses in MYSQL, the selection (University Uk) cannot insert into MYSQL.
    Below is my table in MYSQL
    id --->pk,a_i
    InstitutionName
    CourseName
    Description
    Thanks~
     
    JoeyWong, Oct 20, 2010 IP