简体   繁体   English

将表格从表格插入SQL

[英]Insert array from form to sql

I have a form that someone can add multiple phone numbers and extension, and I am trying to save them in the database, whenever I save the data it shows the word "Array" in both filed. 我有一个表单,有人可以添加多个电话号码和分机号,并且我试图将它们保存在数据库中,只要保存数据,它在两个文件中都显示“ Array”一词。

Here is my code so far: 到目前为止,这是我的代码:

if (!empty($_POST['Phone']) && isset($_POST['Extension'])) {
foreach ($_POST['Phone'] as $key => $value) {
    foreach ($_POST['Extension'] as $key => $value2) {
        $para = array("UserID" => \PDO::PARAM_INT, "Phone" => \PDO::PARAM_STR, "Extension" => \PDO::PARAM_STR);
        $val = array($_SESSION['UserID'], $_POST['Phone'], $_POST['Extension']);
        $r = DB::Call("[spPhoneInsert]", $para, $val);           
    }
}

if (count($r) > 0 && $r[0]['Result'] == 'Ok') {
    header("location:home.php?added_phone=1");
} else {
    header("location:home.php?error_phone=1");
}
exit;
}

whenever you post a form with multiple same name elements like 每当您发布具有多个相同名称元素的表单时

<input type="text" name="phone[]">
<input type="text" name="phone[]">

data is posted as array to the action page. 数据以数组形式发布到操作页面。 You have to loop through this array to save them in data base 您必须遍历此数组才能将它们保存在数据库中

you can loop like this and create whatever format you want Following code will save your number and extension comma seprated 您可以像这样循环并创建所需的任何格式以下代码将保存您的电话号码和扩展名逗号分隔

    foreach ($_POST['phone'] as $number){
    $allNumbers .= $number.',';
    }
foreach ($_POST['extension'] as $ext){
    $allExt .= $ext.',';
    }
  $para = array("UserID" => \PDO::PARAM_INT, "Phone" => \PDO::PARAM_STR, "Extension" => \PDO::PARAM_STR);
        $val = array($_SESSION['UserID'], trim($allNumbers,','), trim($allExt,','));
        $r = DB::Call("[spPhoneInsert]", $para, $val); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM