How to insert form data from multible fields into

2019-08-23 06:19发布

问题:

Consider the following html fields.

<form style="width:100px" action="phpformposttest.php" method="post">
<input type="text" name="Area">
<input type="text" name="Area2">
<input type="text" name="Zip">
<input type="submit">
</form>

Using php, how do i insert ("Area", "Area2", and "Zip") into one column collected from these multiple inputs.

回答1:

You can insert JSON data.

$arr=array("Area"=>$_POST['Area'],"Area2"=>$_POST['Area2'],"Zip"=>$_POST['Zip']);
$json=json_encode($arr);

And insert $json value to database. In PDO :

$sql=$dbh->prepare("INSERT INTO table (column) VALUES(?)");
$sql->execute(array($json));

You can retrieve back Area, Area2 & Zip with json_decode :

$arr=json_decode($json,true);
echo $arr['Area'];
echo $arr['Area2'];
echo $arr['Zip'];


回答2:

You can make a single string:

  $area1 = $_POST['Area1'];
        $area2 = $_POST['Area2'];
        $zip = $_POST['Zip'];
        //prepare single string
        $address = 'Area1: '.$area1.' Area2: '.$area2.' Zip:'.$zip;
        //Now insert it into a single column of table


回答3:

Use implode And explode Function of PHP :-

$arr = array($_POST['Area'], $_POST['Area2'], $_POST['Zip']);
$temp=implode(",",$arr);


回答4:

Why not store as separate columns.. It's always easier to leave data apart then to have to parse it. Also having them all together will make it harder to query.