oliverj777 Posted April 3, 2012 Share Posted April 3, 2012 Hello, I'm working on a form that once submitted, it inserts the result into my SQL table, but I'm trying to get it so it will update an imploded array. EG: current SQL table is "red,blue,green". Then user submits 'white', the SQL table should then update to: "red,blue,green,white" (notice how its added to the end). This is what I have so far: $attack_out = $info['attack_out']; $attack_out_split = explode(",", $attack_out); if(isset($_POST["submit_attack"])){ $attack_user = $_GET["attack"]; // user - sent from form //$troops_send = $_POST["troop_send"]; // ammount - sent from form $attack_user_array = array(); $i = 0; $max = (count($attack_out_split) + 1); while($i < $max){ $reverse = ($max - 1) - $i; $attack_user_array[$i] = $attack_out_split[$reverse]; $attack_user_array[$max - 1] = $attack_user; $i++; } $attack_user_glue = implode(",",$attack_user_array); $result = mysql_query("UPDATE users SET attack_out='$attack_user_glue' WHERE username='$username'"); } I can't figure out the algorithm in my while loop. Would appreciate any help, thanks Quote Link to comment https://forums.phpfreaks.com/topic/260284-php-sql-insert-imploded-array-into-table/ Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 I don't get what your code has to do with what you said. You can use a combo of implode and explode, or just concat the new value. $values .= ',white'; Quote Link to comment https://forums.phpfreaks.com/topic/260284-php-sql-insert-imploded-array-into-table/#findComment-1334121 Share on other sites More sharing options...
samshel Posted April 3, 2012 Share Posted April 3, 2012 As Jesirose rightly pointed out, you dont need to fetch the existing field data in variable, explode it, attached new string at end and implode it again. since it is comma separated, you can just append it at the end. If you want to insert the text in middle of the field and sequence does matter then you will have to do what you are trying to do. $result = mysql_query("UPDATE users SET attack_out=CONCAT(attack_out, '".$attack_user."') WHERE username='$username'"); Quote Link to comment https://forums.phpfreaks.com/topic/260284-php-sql-insert-imploded-array-into-table/#findComment-1334132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.