表单提交后,如果用户已登录,则表单将首先提交登录,然后表单应自动提交

时间:2015-11-19 06:26:34

标签: php mysql

我正在尝试实现一个表单,如果用户已登录,那么将提交,否则先登录然后表单应该在cookie的帮助下自动提交,错误在我的代码中播放到最后一个move_uploaded_file。

我的代码如下:



<?php
 if(isset($_POST['submit'])
 { 
$ad_title=$mysqli->real_escape_string($_POST['ad_title']);
$category=$mysqli->real_escape_string($_POST['category']);
$sub_category=$mysqli->real_escape_string($_POST['sub_category']);
$description=$mysqli->real_escape_string($_POST['description']);
$rent_amount=$mysqli->real_escape_string($_POST['rent_amount']);
$rent_security=$mysqli->real_escape_string($_POST['rent_security']);
$contact=$mysqli->real_escape_string($_POST['contact']);
$email=$mysqli->real_escape_string($_POST['email']);
$city=$mysqli->real_escape_string($_POST['city']);
$state=$mysqli->real_escape_string($_POST['state']);
$area=$mysqli->real_escape_string($_POST['area']);
$buy=(isset($_POST['buy'])?1:0);
$sell=(isset($_POST['sell'])?1:0);
$rent=(isset($_POST['rent'])?1:0);
$manufacture=$mysqli->real_escape_string($_POST['company_name']);
$conditions=$mysqli->real_escape_string($_POST['condition']);

$rent_option=$mysqli->real_escape_string($_POST['rent_option']);

$a=$_FILES['file']['name'];
$path="image/product/$a";

$b=$_FILES['file2']['name'];
$path2="image/product/$b";

$c=$_FILES['file3']['name'];
$path3="image/product/$c";

$d=$_FILES['file4']['name'];
$path4="image/product/$c";

$e=$_FILES['file5']['name'];
$path5="image/product/$c";


if(isset($_SESSION['user_id'])){
$query=$mysqli->query("insert into ads(product_name,category,sub_category,description,image_1,image_2,image_3,image_4,image_5,city,state,rent_amount,rent_option,security_amount,contact_no,email,area,buy,sell,rent,user_id,manufacture,conditions)values('$ad_title','$category','$sub_category','$description','$a','$b','$c','$d','$e','$city','$state','$rent_amount','$rent_option','$rent_security','$contact','$email','$area','$buy','$sell','$rent','$user_id','$manufacture','$conditions')");
move_uploaded_file($_FILES['file']['tmp_name'],$path) & move_uploaded_file($_FILES['file2']['tmp_name'],$path2) & move_uploaded_file($_FILES['file3']['tmp_name'],$path3) & move_uploaded_file($_FILES['file4']['tmp_name'],$path4) & move_uploaded_file($_FILES['file5']['tmp_name'],$path5);
if($query)
{

echo "success";
}
}else{ 


  $time = time() + 60;
  setcookie('email',$email,$time);
  setcookie('ad_title',$ad_title,$time);
  setcookie('category',$category,$time);
  setcookie('sub_category',$sub_category,$time);
  setcookie('description',$description,$time);
  setcookie('rent_amount',$rent_amount,$time);
  setcookie('rent_security',$rent_security,$time);
  setcookie('contact',$contact,$time);
  setcookie('city',$city,$time);
  setcookie('state',$state,$time);
  setcookie('area',$area,$time);
  setcookie('buy',$buy,$time);
  setcookie('sell',$sell,$time);
  setcookie('rent',$rent,$time);
  setcookie('manufacture',$manufacture,$time);
  setcookie('condition',$conditions,$time);
  
  setcookie('rent_option',$rent_option,$time);
   
   setcookie('file',$a,$time);
   setcookie('file2',$b,$time);
   setcookie('file3',$c,$time);
   setcookie('file4',$d,$time);
   setcookie('file5',$e,$time);
   
header("Location:product/login.php"); }
}
 
 if(isset($_COOKIE['email'])){
	 
	 $email =$_COOKIE['email'];
     $cookie2 = $_COOKIE['ad_title']; 
     $cookie3 = $_COOKIE['category'];
     $cookie4 = $_COOKIE['sub_category'];
     $cookie5 = $_COOKIE['description'];
     $cookie6 = $_COOKIE['rent_amount'];
     $cookie7 = $_COOKIE['rent_security'];
     $cookie8 = $_COOKIE['contact'];
     $cookie9 = $_COOKIE['city'];
     $cookie10 = $_COOKIE['state'];
     $cookie11 = $_COOKIE['area'];
     $cookie12 = $_COOKIE['buy'];
     $cookie13 = $_COOKIE['sell'];
     $cookie14 = $_COOKIE['rent'];
     $cookie15 = $_COOKIE['manufacture'];
     $cookie16 = $_COOKIE['condition'];
	 $cookie17 = $_COOKIE['file'];
	 $cookie18 = $_COOKIE['file2'];
     $cookie19 = $_COOKIE['file3'];
	 $cookie20 = $_COOKIE['file4'];
	 $cookie21 = $_COOKIE['file5'];
	 $cookie22 = $_COOKIE['rent_option'];
	 
	 
	 
$pat="image/product/$cookie17";
$pat2="image/product/$cookie18";
$pat3="image/product/$cookie19";
$pat4="image/product/$cookie20";
$pat5="image/product/$cookie21";
  
	 
	 
	 
	 
$query1=$mysqli->query("insert into ads(product_name,category,sub_category,description,image_1,image_2,image_3,image_4,image_5,city,state,rent_amount,rent_option,security_amount,contact_no,email,area,buy,sell,rent,user_id,manufacture,conditions)
values
('$cookie2','$cookie3','$cookie4','$cookie5','$cookie17','$cookie18','$cookie19','$cookie20','$cookie21',
'$cookie9','$cookie10','$cookie6','$cookie22','$cookie7','$cookie8',
'$email','$cookie11','$cookie12','$cookie13','$cookie14','$user_id','$cookie15','$cookie16')");
move_uploaded_file($cookie17,$pat)& 
move_uploaded_file($cookie18,$pat2)& 
move_uploaded_file($cookie19,$pat3)& 
move_uploaded_file($cookie20,$pat4)& 
move_uploaded_file($cookie21,$pat5);
	 
	 
	 if($query1){
		 
		 echo "Succes";
	 }
	 else{
		 echo "Something went wrong.";
	 }
	 
	 
	 }
	 
 ?> 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

不是您问题的解决方案:您的select菜单中没有指定名称。

除此之外,您需要检查是否存在会话变量 - 假设您在用户登录时设置某种会话变量?即:$_SESSION['username']='fred'等?您可以生成将在页面加载时提交表单的JavaScript。

<form name="appointment" method="POST" action="appointment.php" role="form">
    <!-- form contents -->
</form>

<?php
    if( isset( $_SESSION['username'] ) ){
        echo "
            <script type='text/javascript'>
                document.querySelectorAll( form['name=\"appointment\"]' )[0].submit();
            </script>";
    }
?>