dcampbell18 Posted January 20, 2008 Share Posted January 20, 2008 Hello! Can someone please help me to understand this section of code? It works correctly, but I don't fully understand how it works, which is preventing me from moving further in my program. Here is the code: if(!empty($_POST['color']) && is_array($_POST['color'])) { $_POST['color'] = array_map('mysql_real_escape_string', $_POST['color']); $colors = implode('\'), (\'', $_POST['color']); $query = 'INSERT INTO colors (color) VALUES (\'' . $colors . '\')'; $rs = mysql_query($query); } else { // they didn't specify a color, or there was a problem with the data type } I understand this much: If "color" is checked (true) and it's an array, then insert the value of "color" into the database table named "colors". Else, do nothing. This will insert the data each in a separate row in my table which is exactly what I want it to do. Here are my specific questions: 1. What does 'array_map' and 'mysql_real_escape_string' do? 2. What does the '\' do in the line: $colors = implode('\'), (\'', $_POST['color']); 3. What does the '\' do in the line: $query = 'INSERT INTO colors (color) VALUES (\'' . $colors . '\')'; Thank you in advance for any explanation you can give me! Quote Link to comment https://forums.phpfreaks.com/topic/86908-help-with-understanding-code/ Share on other sites More sharing options...
trq Posted January 20, 2008 Share Posted January 20, 2008 1. array_map will call the function named in its first argument (in this case mysql_real_escape_string) on each element of the array in its second. 2. That doesn't look at all valid to me. 3. Backslashes escape the next following char within a string. Quote Link to comment https://forums.phpfreaks.com/topic/86908-help-with-understanding-code/#findComment-444316 Share on other sites More sharing options...
kenrbnsn Posted January 20, 2008 Share Posted January 20, 2008 A more understandable way to write these two lines: <?php $colors = implode('\'), (\'', $_POST['color']); $query = 'INSERT INTO colors (color) VALUES (\'' . $colors . '\')'; ?> would be to use double quotes to delimit the strings instead of single quotes: <?php $colors = implode("'), ('", $_POST['color']); $query = "INSERT INTO colors (color) VALUES ('" . $colors . "')"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/86908-help-with-understanding-code/#findComment-444331 Share on other sites More sharing options...
trq Posted January 20, 2008 Share Posted January 20, 2008 Actually, looking at question 2. It looks like it may be valid. explode splits a string (by the first argument) into an array. So if you had... <?php $str = 'foo,bar,bob'; $arr = explode(',',$str); // you'd end up with. echo $arr[0]; // foo echo $arr[1]; // bar echo $arr[2]; // bob ?> Given your code it would split the string contained in $_POST['color'] on '), (' Quote Link to comment https://forums.phpfreaks.com/topic/86908-help-with-understanding-code/#findComment-444333 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.