如何在mysql中插入多个字段

时间:2011-06-20 19:46:09

标签: php mysql html

您好我正在尝试提交一个webform并将数据插入到mysql中。我遇到的问题是我已经设置了多个输入框的字段。例如,我只有“名字”,“姓氏”而我的地址被分成许多字段,以确保最终用户不会错过他们地址的某些部分。

我遇到的问题是我无法将这些多个字段提交到mysql中。如果我想简单地提交“名称”字段,我可以,但不是名字+姓氏。为了更清楚,我有一个名为“name”的列,我希望将“firstname”和“lastname”都存储在那里。地址也一样。

这是我的HTML代码

<ul>            
    <li id=name">
        <label class="description" for="element_1">Name </label>
        <span>
            <input id="first" name= "first" class="element text" maxlength="255" size="8" value=""/>
            <label>First</label>
        </span>
        <span>
            <input id="last" name= "last" class="element text" maxlength="255" size="14" value=""/>
            <label>Last</label>
        </span> 
    </li>
    <li id="phone">
        <label class="description" for="element_2">Phone </label>
        <span>
            <input id="parea" name="parea" class="element text" size="3" maxlength="3" value="" type="text" /> -
            <label for="parea">(###)</label>
        </span>
        <span>
            <input id="pfirst" name="pfirst" class="element text" size="3" maxlength="3" value="" type="text" /> -
            <label for="pfirst">###</label>
        </span>
        <span>
            <input id="plast" name="plast" class="element text" size="4" maxlength="4" value="" type="text" />
            <label for="plast">####</label>
        </span>
    </li>
    <li id="email">
        <label class="description" for="element_3">Email </label>
        <div>
            <input id="email1" name="email1" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
    </li>
    <li id="address">
        <label class="description" for="element_4">Address </label>
        <div>
            <input id="street" name="street" class="element text large" value="" type="text">
            <label for="element_4_1">Street Address</label>
        </div>
        <div>
            <input id="element_4_2" name="element_4_2" class="element text large" value="" type="text" />
            <label for="element_4_2">Address Line 2</label>
        </div>
        <div class="left">
            <input id="element_4_3" name="element_4_3" class="element text medium" value="" type="text" />
            <label for="element_4_3">City</label>
        </div>
        <div class="right">
            <input id="element_4_4" name="element_4_4" class="element text medium" value="" type="text" />
            <label for="element_4_4">State / Province / Region</label>
        </div>
        <div class="left">
            <input id="element_4_5" name="element_4_5" class="element text medium" maxlength="15" value="" type="text" />
            <label for="element_4_5">Postal / Zip Code</label>
        </div>
    </li>
</ul>

我的PHP变量是这样的:

$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$pod=$_POST['pod'];
$sku=$_PST['sku'];
$description=$_POST['description'];
$problem=$_POST['problem'];
$phone=$_POST['phone'];

2 个答案:

答案 0 :(得分:4)

$name=$_POST['first'] ." ". $_POST['last'];
$email=$_POST['email'];
$address=$_POST['address'];
$pod=$_POST['pod'];
$sku=$_PST['sku'];
$description=$_POST['description'];
$problem=$_POST['problem'];
$phone=$_POST['phone'];

答案 1 :(得分:0)

理想情况下,您需要重新设计表,但要使其与现有模式一起使用,您需要序列化和反序列化数据。见

http://php.net/manual/en/function.serialize.php

因此,在名称列中存储类似{firstname:lastname}

的内容
$name[0] = $firstname;
$name[1] = $lastname;
$ser = serialize($name); \\--->put this in db