hoopplaya4 Posted November 1, 2008 Share Posted November 1, 2008 Hi All, I've tried googling this for the past hour, but I haven't been able to find anything to help me. I'm trying to insert 3 different fields (it's a phone number-- area code, prefix, suffix) into one row in my mySQL database. How would I go about doing this? Here's my HTML: <label for="phone">Phone Number</label> <input type="text" size=3 maxlength="3" name="phone[]" id='phone[]'/>- <input type="text" size=3 maxlength="3" name="phone[]" id='phone[]'/>- <input type="text" size=4 maxlength="4" name="phone[]" id='phone[]'/> And here's my PHP Script: $usrPhone = str_replace("'", "''", $_POST["phone"]); $sql = "INSERT INTO tblUsers (usrPhone)"; $sql .=" VALUES ('$usrPhone')"; Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/ Share on other sites More sharing options...
Flames Posted November 1, 2008 Share Posted November 1, 2008 what error are you getting, what problems are you getting. Personally im not sure how this is going to work, giving the variable $usrPhone 3 different values as well as putting the [] in the name and id. Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680016 Share on other sites More sharing options...
marcus Posted November 1, 2008 Share Posted November 1, 2008 <?php $phones = $_POST['phone']; $new = ""; $x = 1; $c = count($phones); foreach($phones AS $phone){ $pipe = ($x == $c) ? "" : "|"; $new .= $phone . $pipe; $x++; } ?> Use $new as your input value, then when you wanna select it just explode it with | Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680036 Share on other sites More sharing options...
laffin Posted November 1, 2008 Share Posted November 1, 2008 why not just use implode, yer function does the same thing as implode <?php $phones = $_POST['phone']; $new =implode('|',$phones); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680049 Share on other sites More sharing options...
hoopplaya4 Posted November 1, 2008 Author Share Posted November 1, 2008 Thanks mgallforever and laffin, both worked pefectly! I just had one question, for mgallforever. Can you explain to me what's happening in the code snippet you provided? I'd like to learn a little bit about what was going on. Or if you can point me to a tutorial, that'd be helpful. Thanks again guys! Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680060 Share on other sites More sharing options...
laffin Posted November 1, 2008 Share Posted November 1, 2008 <?php $phones = $_POST['phone']; // Get The Form Values $new = ""; // Initialize our string $x = 1; // Initialize a counter $c = count($phones); // How many items did we get from the form foreach($phones AS $phone){ // loop through each item from form $pipe = ($x == $c) ? "" : "|"; // is it last item? Yes - add Blank, No - add | to our string $new .= $phone . $pipe; // add to our string the current item and optional | $x++; // prepare for next item } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680072 Share on other sites More sharing options...
hoopplaya4 Posted November 1, 2008 Author Share Posted November 1, 2008 Awesome! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680081 Share on other sites More sharing options...
laffin Posted November 2, 2008 Share Posted November 2, 2008 Hope it all makes sense now Good luck Quote Link to comment https://forums.phpfreaks.com/topic/130990-solved-insert-multiple-phone-number-fields-into-database/#findComment-680412 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.