1

我正在为宠物创建一个网站。我希望创建一个页面,宠物主人可以在其中注册和添加他的宠物。我制作了一个表单并放置了 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

4 回答 4

1

更新

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

在 PHP 方面,您应该在POST(或者GET如果您的表单方法是GET)数组中看到类似这样的内容:$_POST["petname"][0]$_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][]">

然后您可以执行以下操作:

foreach ($_POST["pet"] as $pet)
{
    // store $pet["name"] in the database
    // store $pet["type"] in the database
    // etc.
}    
于 2012-06-22T19:57:14.103 回答
1

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

print_r($_POST);

然后您将能够找出是否有任何错误,但没有任何 PHP 代码,我们无法准确告诉您问题所在。

于 2012-06-22T19:57:31.513 回答
0

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

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

于 2012-06-22T19:53:33.663 回答
0
$petname = $_POST['form controls name']

$_POST 包含表单在方法发布时接收到的数据,默认为 $_GET[' ']。

于 2012-06-22T19:53:49.440 回答