HELP: how to insert the selected data and input text data (array) into MySQL??

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

  1. #1
    Hi all, I want to ask how i insert the selected data (Institution Name) and the input text data (School name) into MYSQL?? Thanks~
    my database as below:



    --
    -- Database: `inno_education`
    --

    -- --------------------------------------------------------

    --
    -- Table structure for table `institution`
    --

    CREATE TABLE IF NOT EXISTS `institution` (
    `i_id` int(11) NOT NULL AUTO_INCREMENT,
    `i_name` varchar(100) NOT NULL,
    `address` varchar(1000) NOT NULL,
    `website` varchar(100) NOT NULL,
    PRIMARY KEY (`i_id`),
    UNIQUE KEY `i_name` (`i_name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

    --
    -- Dumping data for table `institution`
    --


    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



    --
    -- Database: `inno_education`
    --

    -- --------------------------------------------------------

    --
    -- Table structure for table `school`
    --

    CREATE TABLE IF NOT EXISTS `school` (
    `s_id` int(11) NOT NULL AUTO_INCREMENT,
    `s_name` varchar(100) NOT NULL,
    `institute` int(11) NOT NULL,
    PRIMARY KEY (`s_id`),
    KEY `FK_institute` (`institute`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

    --
    -- Dumping data for table `school`
    --


    --
    -- Constraints for dumped tables
    --

    --
    -- Constraints for table `school`
    --
    ALTER TABLE `school`
    ADD CONSTRAINT `school_ibfk_1` FOREIGN KEY (`institute`) REFERENCES `institution` (`i_id`) ON DELETE CASCADE ON UPDATE CASCADE;

    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



    <?php
    $connection = mysql_connect("localhost", "root", "") or die("Could not connect to MySQL " .mysql_error() );
    $selection = mysql_select_db("inno_education") or die("Unable to select database. " .mysql_error());
    ?>

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    var counter = 0;

    function moreFields(){
    counter++;
    var newFields = document.getElementById('readroot').cloneNode(true);
    newFields.id = '';
    newFields.style.display = 'block';
    var newField = newFields.childNodes;
    for (var i=0;i<newField.length;i++)
    {
    var theName = newField.name
    if (theName)
    newField.name = theName + counter;
    }
    var insertHere = document.getElementById('writeroot');
    insertHere.parentNode.insertBefore(newFields,insertHere);
    }

    window.onload = moreFields;
    </script>
    </head>

    <body>
    <table>
    <tr>
    <td>Institution Name</td>
    <td>:</td>
    <td>
    <?php $sql = "SELECT i_name from institution";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["InstitutionName"]))
    $InstitutionName = $_POST["InstitutionName"];
    echo"<select name='InstitutionName'>";
    while ($row = mysql_fetch_array($result))
    { $i_name=$row["i_name"];
    echo"<option value='$i_name'";
    if (isset($InstitutionName) && $InstitutionName ==$i_name)
    echo " selected> $i_name";
    else echo "> $i_name"; }
    echo "</select><br>";
    ?>
    </td>
    </tr>
    </table>
    <div id="readroot" style="display: none">
    <input type="button" value="Remove" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
    <table>
    <tr>
    <td>School Name</td>
    <td>:</td>
    <td><input name="s_name[]" type="text" size="30" maxlength="100"></td>
    </tr>
    </table>
    </div>

    <form method="post" action="">

    <span id="writeroot"></span>

    <input type="button" value="Add" onClick="moreFields()"/>
    <input type="submit" name="send" value="Send form" />

    </form>

    </body>
    </html>
     
    JoeyWong, Oct 25, 2010 IP
  2. kai555

    kai555 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi

    keep in mind by the time the user clicks on Add php is done executing and the results is now with the user's browser. Either you have to:
    A. submit the form to the server by using
    <form method="post" action="somePageHere.php">
    and having just one submit button.

    you can then use $_GET to get the variables the user entered.

    B. Use ajax to submit the data to a new page that inserts it in the database.


    The important thing to understand is that when a page is loaded in the browser it is no longer on the server, so for you to access the database a new call to the server is required.
     
    kai555, Oct 25, 2010 IP
  3. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, I know what you mean. Can u help me check. I do not know where mistake. It cannot show the data. Thanks~

    Example:
    <?php
    $connection = mysql_connect("localhost", "root", "") or die("Could not connect to MySQL " .mysql_error() );
    $selection = mysql_select_db("inno_education") or die("Unable to select database. " .mysql_error());
    ?>
    <html><body>
    <form action="view.php" name="form" method="post">
    <?php $sql = "SELECT i_name from institution";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["InstitutionName"]))
    $InstitutionName = $_POST["InstitutionName"];
    echo"<select name='InstitutionName'>";
    while ($row = mysql_fetch_array($result))
    { $i_name=$row["i_name"];
    echo"<option value='$i_name'";
    if (isset($InstitutionName) && $InstitutionName ==$i_name)
    echo " selected> $i_name";
    else echo "> $i_name"; }
    echo "</select><br>";
    ?>
    <input type="submit" value="Send form" />
    </form>
    </body></html>

    view.php
    <html><body>
    <?php
    $InstitutionName=$_POST['i_name'];
    echo "$InstitutionName";
    ?>
    <body></html>
     
    JoeyWong, Nov 6, 2010 IP
  4. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, I know what you mean. Can u help me check. I do not know where mistake. It cannot show the data. Thanks~

    Example:
    <?php
    $connection = mysql_connect("localhost", "root", "") or die("Could not connect to MySQL " .mysql_error() );
    $selection = mysql_select_db("inno_education") or die("Unable to select database. " .mysql_error());
    ?>
    <html><body>
    <form action="view.php" name="form" method="post">
    <?php $sql = "SELECT i_name from institution";
    // execute query
    $result = mysql_query($sql);
    if(isset($_POST["InstitutionName"]))
    $InstitutionName = $_POST["InstitutionName"];
    echo"<select name='InstitutionName'>";
    while ($row = mysql_fetch_array($result))
    { $i_name=$row["i_name"];
    echo"<option value='$i_name'";
    if (isset($InstitutionName) && $InstitutionName ==$i_name)
    echo " selected> $i_name";
    else echo "> $i_name"; }
    echo "</select><br>";
    ?>
    <input type="submit" value="Send form" />
    </form>
    </body></html>

    view.php
    <html><body>
    <?php
    $InstitutionName=$_POST['i_name'];
    echo "$InstitutionName";
    ?>
    <body></html>
     
    JoeyWong, Nov 6, 2010 IP
  5. helloguys12345678

    helloguys12345678 Greenhorn

    Messages:
    50
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #5
    Change echo"<option value='$i_name'"; to echo"<option value='i_name'";
     
    helloguys12345678, Nov 7, 2010 IP
  6. JoeyWong

    JoeyWong Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks all...=)
     
    JoeyWong, Nov 9, 2010 IP