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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

    Inserting arrays into table using php

    Hi,

    I have an array which I want to explode , insert each value into the table as a new row, but also insert into another column the string.

    My syntax is as follows

    PHP Code:
        $reap=$_POST['details'];
        
    $steep=str_replace(";"," ",$reap);

        
    $to=$_POST['details'];    
        
    $names explode("; ",$to);
        foreach (
    $names as $Invitee){    
        
    $sql="INSERT INTO event (invitee, to)
            VALUES
            ({$Invitee},{$steep})"
    ;
        } 
    I have some other fields, but I have removed and added all fields one by one to work out that inserting into "to" is causing a fatal syntax error and that invitee is inserting as 0. The value of $_POST['details'] is 2;4; (this is for my test it can go up and down and include any number of values when operating for real) when I echo out $names[0] and $names[1] I get 2 and 4 respectively.

    Needless to say despite there being two values so I want this inserted twice once for 2 and once for 4 there is only one insertion.

    I have tried inserting as '{$invitee}' and "{$invitee}" but whatever I try I get 0.

    In the table invitee and to are both set to INT, which is why I tried the replace to get rid of the semi colons incase that was a problem (though I would prefer to keep them).

    The table names are correct, I double checked.

    I have another variable which is a word which I can quite happily insert using '{$var}'

    I have run out of things to try.

  • #2
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    PHP Code:
    $reap=$_POST['details'];  //Say value is 2;4
    $steep=str_replace(";"," ",$reap);

    $names explode("; ",$reap);
    for(
    $i=0;$i<count($names);$i++) {
    $sql="INSERT INTO event (invitee, to)
            VALUES
         ('"
    .$names[$i]."','$steep')";
        } 
    Little different, but if I understand, this should do what you want... and if not, I'll give it another shot.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • Users who have thanked dniwebdesign for this post:

    Jenny Dithe (11-12-2010)

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Thank you for helping.

    Unfortunately I am still getting a syntax error for $steep and if I remove that the insert is still entering only once and putting the invitee value as 0.

    I tried echoing out $names[0] and $names[1] and that printed 2 4 so I am baffled as to why it isn't inserting.

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    print out $sql in the loop and post it here
    PHP Code:
    for($i=0;$i<count($names);$i++) {
    $sql="INSERT INTO event (invitee, to)
            VALUES
         ('"
    .$names[$i]."','$steep')";
    echo 
    $sql '<br />';

    and where is that part of a the code where you are inserting data to db? If you put data to the db not in the loop, you should do that before inserting:
    PHP Code:
    for($i=0;$i<count($names);$i++) {
    $sql[]="('".$names[$i]."','$steep')";
    }
    $sql "INSERT INTO event (invitee, to) VALUES " implode(',  '$sql); 
    Last edited by poyzn; 11-12-2010 at 07:34 AM.

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Ok my full syntax goes:
    Code:
    	if($_POST['view']=='1'){
    	$view='public';
    	} else {
    	$view='private';}
    
    	$reap=$_POST['details'];
    	$steep=str_replace(";"," ",$reap);
    	
    	$names = explode("; ",$reap);
    	echo $names[0];
    	echo $names[1] . "<br />";
    	for($i=0;$i<count($names);$i++){	
    	$sql="INSERT INTO event (host, invitee, to, state, date, view)
    		VALUES
    		('{$_SESSION['me']}','" . $names[$i] . "','$steep','pending',NOW(),'{$view}')";
    	echo $sql . '<br />';
    	}
    And my printed $sql is:

    2 4 //this is my echo
    INSERT INTO event (host, invitee, to, state, date, view) VALUES ('1','',' 2 4 ','pending',NOW(),'private')


    And I get the error message:

    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to, state, date, view) VALUES ('1','',' at line 1


    I don't have anything separately inserted into the table, but for each name I want a new row to be inserted.
    Last edited by Jenny Dithe; 11-12-2010 at 09:08 AM.

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    TO is reserved mysql word, should be `to`, either as date -> `date`
    mysql reserved words

  • Users who have thanked poyzn for this post:

    Jenny Dithe (11-12-2010)

  • #7
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    poyzn is right... "to" is a reserved MySQL word and need to be quotes using `... I usually find it best to do that to all of my column & table titles.

    PHP Code:
    INSERT INTO `event` (`host`, `invitee`, `to`, `state`, `date`, `view`)... 
    And use the [ php ] tags when quoting php. ;-) Makes it easier to read, for myself at least.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #8
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Stupid mistake on my part, thank you for pointing it out.

    I still have the problem though that the invitee/$names will not insert at all

  • #9
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Sorry I am tired which is why some silly mistakes are being made.

    My code again using the PHP
    PHP Code:
        if($_POST['view']=='1'){
        
    $view='public';
        } else {
        
    $view='private';}

        
    $reap=$_POST['details'];
        
    $steep=str_replace(";"," ",$reap);
        
        
    $names explode("; ",$reap);
        echo 
    $names[0];
        echo 
    $names[1] . "<br />";
        for(
    $i=0;$i<count($names);$i++){    
        
    $sql="INSERT INTO `event` (`host`, `invitee`, `to`,` state`,` date`,` view`)
            VALUES
            ('{$_SESSION['me']}','" 
    $names[$i] . "','$steep','pending',NOW(),'{$view}')";
        echo 
    $sql '<br />';
        } 
    I changed to to tom

  • #10
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Quote Originally Posted by Jenny Dithe View Post
    Sorry I am tired which is why some silly mistakes are being made.

    My code again using the PHP ...
    and post echo $sql result

  • #11
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    The print $sql says:
    PHP Code:
    2 4
    INSERT INTO event 
    (hostinviteetomstatedateviewVALUES ('1',' 2',' 2; 4; ','pending',NOW(),'private')
    INSERT INTO event (hostinviteetomstatedateviewVALUES ('1',' 4',' 2; 4; ','pending',NOW(),'private'
    But what is entering my table is

    1,0,2,pending, 12.11.2010 12.00,private

    And this is only being entered once

  • #12
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    what type of columns are invitee and tom? may by the type of the invitee column is INT and you're trying to put there a string

  • Users who have thanked poyzn for this post:

    Jenny Dithe (11-12-2010)

  • #13
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Tom was an int (I changed that to varchar and it now works) Invitee I changed to varchar and it came up blank.

    So it is still not inserting into invitee and there is only one insertion which is 0 instead of two where one is 2 and one is 4.

  • #14
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    I cannot really see any reason as to why the insert is blank for the column invitee... how many characters do you have the invitee varchar set to?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #15
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    255.

    But I think I know why, I just don't know what is causing it.

    The echo I am getting is
    PHP Code:
    INSERT INTO privateevent (hostinviteetomstatedateviewVALUES ('1',' 2',' 2; 4; ','pending',NOW(),'private')
    INSERT INTO privateevent (hostinviteetomstatedateviewVALUES ('1',' 4',' 2; 4; ','pending',NOW(),'private')
    INSERT INTO privateevent (hostinviteetomstatedateviewVALUES ('1','',' 2; 4; ','pending',NOW(),'private'
    So I think the third submission is overwriting the first two. However I have no idea where the third submission is coming from. This is my code:
    PHP Code:
    <?php

        $con 
    mysql_connect("localhost","user","password");
        if (!
    $con)
            {
            die(
    'Could not connect: ' mysql_error());
            }
        
    mysql_select_db("mydb"$con);
        
    function 
    check_input($value){

    if (
    get_magic_quotes_gpc()){
        
    $value=stripslashes($value);
        }
        
    if (!
    is_numeric($value)){
        
    $value mysql_real_escape_string($value);
        }
    return 
    $value;
    }

    foreach (
    $_POST as $key=>$value){
        
    $_POST[$key]= check_input($value);
        }
        
        
    $reap=$_POST['details'];
        
    $steep=str_replace(";"," ",$reap);
        
        
    $names explode("; ",$reap);
        for(
    $i=0;$i<count($names);$i++){    
        
    $sql="INSERT INTO privateevent (host, invitee, tom, statae, datesent, view)
            VALUES
            ('{$_SESSION['Me']}','" 
    $names[$i] . "','{$_POST['details']}','pending',NOW(),'{$view}')";
        echo 
    $sql '<br />';
        }
        
     
    ?>
    Sorry for only bringing this up now I thought it was simply generated by my

    $result=mysql_query($sql) or die('Error: ' . mysql_error());

    But I removed that today to check, and realised it was an actual error in the script. I tried the foreach as well but I still get three responses.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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