我想创建一个表单来输入宠物主人和他的宠物详细信息

时间:2012-06-22 19:49:57

标签: php mysqli

我正在为宠物创建一个网站。我想创建一个页面,宠物主人可以注册并添加他的宠物。我已经制作了一个表单并添加了javascript以添加更多宠物,这会在点击“添加更多宠物”链接时创建新字段。但是我无法将这些信息收集到php变量中。链接在这里:http://animalswecare.in/mypet.php

我有php和mysqli的工作知识。

我的PHP代码                    if(isset($ _POST ['mypet'])){

   $name         = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['name'] ) )  );
  $email     = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['email'] ) )  );
  $password    = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['password'] ) )  );
   $gender       = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['sex'] ) )  );
     $contact     = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['contact'] ) )  );
   $country     = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['country'] ) )  );
    $state       = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['state'] ) )  );
  $city        = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['city'] ) )  );
   //$photo       = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['picture'] ) )  );

 $fname = mysqli_real_escape_string ( $dbc, $_FILES['picture']['name'] );
            $ext=$_FILES['picture']['name'];
            $targetpath = "images/user/big/";

            if($ext !="")
            {
        $ext = explode(".",$ext);
        if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG" || $ext[1]=="GIF" || $ext[1]=="PNG" || $ext[1]=="BMP")
        {   
            if($_FILES['picture']['size'] <= 2000000)
            {

                $filename=$targetpath.$id."-" .$fname;
                $photo = $id."-" .$fname;
                if (file_exists($filename)) 
                {
                    chmod($filename, 0777); 
                    unlink($filename); 
                }
                if(move_uploaded_file($_FILES['picture']['tmp_name'],$filename))
                {
                    $thpath = "images/user/thumb/";
                    $file = createThumb1($filename, $thpath, $fl_db,350,280);
                }
            }
        }
     }

  $query = "INSERT INTO `user` SET `name`= '$name',`email` = '$email' ,`password` = '$password',`gender` = '$gender', `contact` = '$contact', `country` = '$country', `state` = '$state', `city` = '$city', `photo` = '$photo',`date` = now(),`status` = '1' ";
  $insComm = mysqli_query($dbc, $query);
  $uid = mysqli_insert_id( $dbc );

 if($_POST['petname'])
 {
 $array = $_POST['petname'];
 foreach($array as $petname)
 {
 if(strlen($petname)>0)
 {

 //$petname    = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['petname'] ) )  );
  $type     = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['type'] ) )  );
  $breed    = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['breed'] ) )  );
  $gender   = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['gender'] ) )  );
  //$photo      = mysqli_real_escape_string ( $dbc, strip_tags ( trim ( $_POST['photo'] ) )  );

  $fname = mysqli_real_escape_string ( $dbc, $_FILES['petimage']['name'] );
            $ext=$_FILES['petimage']['name'];
            $targetpath = "images/pet/big/";

            if($ext !="")
            {
        $ext = explode(".",$ext);
        if($ext[1]=="jpg" || $ext[1]=="gif" || $ext[1]=="jpeg" || $ext[1]=="png" || $ext[1]=="bmp" || $ext[1]=="wbmp" || $ext[1]=="JPEG" || $ext[1]=="JPG" || $ext[1]=="GIF" || $ext[1]=="PNG" || $ext[1]=="BMP")
        {   
            if($_FILES['petimage']['size'] <= 2000000)
            {

                $filename=$targetpath.$id."-" .$fname;
                $petphoto = $id."-" .$fname;
                if (file_exists($filename)) 
                {
                    chmod($filename, 0777); 
                    unlink($filename); 
                }
                if(move_uploaded_file($_FILES['petimage']['tmp_name'],$filename))
                {
                    $thpath = "images/pet/thumb/";
                    $file = createThumb1($filename, $thpath, $fl_db,350,280);
                }
            }
        }
      }
  $query = "INSERT INTO `mypet` SET `uid`= '$uid',`petname`= '$petname',`type` =        '$type',`breed` = '$breed',`gender` = '$gender', `photo` = '$petphoto',`date` = now(),`status` = '1' "; 
   $insComm = mysqli_query($dbc, $query); 
   $msg = "Your information is successfully Added!!";

4 个答案:

答案 0 :(得分:1)

已更新

您的petname字段HTML设置了重复条目(name="petname[]"),的正确属性,但您的类型,性别和品种输入需要[]在结尾处他们的名字属性<input type="text" name="type[]">

在PHP方面,您应该在POST(或GET,如果您的表单方法是GET)数组中看到这样的内容:$_POST["petname"][0]和{{ 1}}。所以我会做这样的事情:

$_POST["petname"][1]

由于您使用的是多个字段,因此您可能需要按如下方式构建它们:

foreach ($_POST["petname"] as $name)
{
    // store $name in the database
}


foreach ($_POST["type"] as $type)
{
    // store $type in the database
}

etc...

然后你可以做到以下几点:

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

答案 1 :(得分:1)

查看$_POST数据的最佳方式是:

print_r($_POST);

然后你将能够发现是否有任何错误,但没有任何PHP代码,我们无法确切地告诉你问题是什么。

答案 2 :(得分:0)

$_POST将包含从表单中收到的数据:

$_POST['name']$_POST['email'],等等。

答案 3 :(得分:0)

$petname = $_POST['form controls name']

$ _ POST包含方法发布时表单收到的数据,默认情况下等效于$ _GET ['']。