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!

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

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!

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.