Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php mysql insert

    Hi

    i have a code like this, which gona use for add multiple comments to a record

    PHP Code:
    <script language="javascript">
    fields 0;
    function 
    addInput() {
        if (
    fields != 10) {
            var 
    htmlText =  "<input type='text' value='' name='field[]' /><br />";
            var 
    newElement document.createElement('div');
            
    newElement.id 'text';
         
    newElement.innerHTML htmlText;

            var 
    fieldsArea document.getElementById('text');
        
    fieldsArea.appendChild(newElement);

            
    fields += 1;
        } else {
            
    alert("Only 10 fields allowed.");
            
    document.form.add.disabled=true;
        }
    }
    </script>

    <form name="form" action="form.php" method="post">
    <input type="button" onclick="addInput()" name="add" value="Add input field" />
    <div id="text"></div>
    <br />
    <input type="submit" value="Submit" />
    </form> 
    how can i insert all values submit from this code into a mysql table?


    sorry for language errors.

    Thanks
    Last edited by extremed; 11-14-2010 at 09:18 AM.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    very simple, without any filtering, validating and escaping it looks like this:
    PHP Code:
    foreach($_POST['field'] as $field) {
     
    $sql[] = "('$field')";
    }
    mysql_query("INSERT INTO tablename (fieldname) VALUES " implode(','$sql)); 

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Note that'll only work if every _POST you get put in $sql has the name of the columns you have in mySQL AND it is in the order of the table.

    Really, you should be referencing the column with INSERT INTO table (column1, column2, etc) VALUES (value1, value2, etc). In addition, if any of those values are strings, that won't work (though I'm pretty sure poyzn knows that).

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this work for me

    PHP Code:
    mysql_query ("insert into comments (id,comment,date) values ('$id','$value',NOW())"); 

    but now i have a problem which if the text field empty it insert a blank record into database

    with the code i have no idea how to validate values

    any help would be great

    Thanks for the replies guys.

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    if($value) { // check if $value not empty
      
    mysql_query(...); // send query to db

    by the way DATE is reserved mysql word, you should backquote it in SQL queries:
    PHP Code:
    `date
    Last edited by poyzn; 11-14-2010 at 04:55 PM.

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot

    i tried to insert data with same page without sending to another php with

    PHP Code:
    action="<?php echo $_SERVER['PHP_SELF']; ?>"
    if i use like that im getting error in foreach

    any idea why?


    Regards

  • #8
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    May be you do not pass an array to foreach loop. Try to check the variable if it is an array before start foreach
    PHP Code:
    if(is_array($fileds)) {
       foreach(
    $fields as ...) {
          ...
       }


  • #9
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    tried like this, no errors but record doesn't insert

    PHP Code:
    <script language="javascript">
    fields = 0;
    function addInput() {
        if (fields != 10) {
            var htmlText =  "<input type='text' value='' name='field[]' /><br />";
            var newElement = document.createElement('div');
            newElement.id = 'text';
         newElement.innerHTML = htmlText;

            var fieldsArea = document.getElementById('text');
        fieldsArea.appendChild(newElement);

            fields += 1;
        } else {
            alert("Only 10 fields allowed.");
            document.form.add.disabled=true;
        }
    }
    </script>

    <form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="button" class="add" onclick="addInput()" name="add" value="Add Comment"  />
    <div id="text"></div>
    <br />
    <input type="submit" value="Submit" />
    </form>


    <?php
    $host 
    'localhost';
    $user 'root';
    $pass '';
    $db 'testc';

    $con mysql_connect($host $user $pass) or die ("Connection could not established." .mysql_error() );

    mysql_select_db($db $con)  or die ("Colud not select database" mysql_error());

    if(
    is_array($field)) {
    foreach(
    $_POST['field'] as $value)
     
    {

     
    mysql_query ("insert into comm (comment) values ('$value')");
    }

    }
    ?>

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    1) You're not santising input.

    2) You're not escaping input.

    3) PHP_SELF is dangerous used as is.

    4) Learn to use basic error checking.

    How people ever expect to track down problems in half the code posted is beyond me.

    Code:
    mysql_query ("insert into comm (comment) values ('$value')") or die(__FILE__, __LINE__, mysql_error());

  • #11
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Quote Originally Posted by extremed View Post
    tried like this, no errors but record doesn't insert
    so, what's the problem? You don't want to insert empty value into db, do you?

    Quote Originally Posted by extremed View Post
    but now i have a problem which if the text field empty it insert a blank record into database

  • #12
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no thats not what i mean

    with <?php echo $_SERVER['PHP_SELF']; ?>

    even after value entered, value doesn't store in db. seems i messed up somewhere

  • #13
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    if(is_array($_POST['field'])) { ... 
    you should always sanitize input

    PHP Code:
    $fields_input filter_input(INPUT_POST'field'FILTER_SANITIZE_STRING, array('flags' => FILTER_REQUIRE_ARRAY));
    if(
    is_array($fields_input)) {
        foreach(
    $fields_input as ... 
    Last edited by poyzn; 11-14-2010 at 06:01 PM.

  • #14
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks it works now.

    also thanks MattF for advice


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •