Jump to content

[SOLVED] Insert Multiple Phone Number Fields into Database


hoopplaya4

Recommended Posts

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!

Link to comment
Share on other sites

<?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 |

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

<?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
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.