Insert data and Images using same php form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Insert data and Images using same php form

    I want to add form data and images using one form in php. I'm using php,ajax and mysql.When I run images insert code and data insert code seperately ; they worked. But when I put in the same php form data insert part doesn't work. Please tell me where I goes wrong
    This is my php page
    Code:
    [LEFT]<?php 
    session_start();
    require_once ('dbconnect.php');
    require_once ('commonFunctions.php');
    
    <script language="javascript" src="ajax.js"></script>
    
    
    <script type="text/javascript">
    function add_file_field(){
    var container=document.getElementById('file_container');
    var file_field=document.createElement('input');
    file_field.name='images[]';
    file_field.type='file';
    container.appendChild(file_field);
    var br_field=document.createElement('br');
    container.appendChild(br_field);
    }
    </script>
    
    </head>
    
    <?
    
    $make= "";
    
    
    if(isset($_GET['Submit'])==2){
    	$make=$_GET['make'];
    	getVehicleModel($make);
    	return;
    }	
    
    if(isset($_GET['S'])==1){
    
    echo $make;
    	$make=$_GET['make'];
    	$model=$_GET['model'];
    	$type=$_GET['type'];
    	$year=$_GET['year'];
    	$month=$_GET['month'];
    	$fuel=$_GET['fuel'];
    	$transmission=$_GET['transmission'];
    	$displacement=$_GET['displacement'];
    	$color=$_GET['color'];
    	$milage=$_GET['milage'];
    	$price=$_GET['price'];
    	$other=$_GET['other'];
    //---------------------insert data to vehicles table-------------------------------------------	
    	$sql = "INSERT INTO vehicles VALUES('','$make','$model','$type','$year','$month','$fuel','$transmission','$color','$price','$displacement','$milage','$other')";
    	
    	$result = mysql_query($sql);
    	echo "SUCCESS";
    	
    	
    	//$vehicles_pk = mysql_insert_id();
    	
    	if (!$result) 
    		{
    			echo "Error in SQL query: " . mysql_error();
    		}
    		else{
    		
    			$message = "<p class=message>Successfully Inserted !</p>";
    		 }
    
    	return;
    	}
    	
    	
    	
    	if (isset($_POST['Submit'])) {
        $number_of_file_fields = 0;
        $number_of_uploaded_files = 0;
        $number_of_moved_files = 0;
        $uploaded_files = array();
        $upload_directory = dirname(__file__) . '/vehicles/'; 
       
    
         
        for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
            $number_of_file_fields++;
            if ($_FILES['images']['name'][$i] != '') { 
                $number_of_uploaded_files++;
                $uploaded_files[] = $_FILES['images']['name'][$i];
                if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                    $number_of_moved_files++;
                }
     
            }
     
        }
    
    	$name= implode(',', $uploaded_files);
    	echo $name;
    	
    	
    }
    ?>
    
    
    
    <table width="566" height="100%" border="0" align="center" cellpadding="0" cellspacing="0"  >
      
      <!--------------body-------------------------------------------------------------->
     	<tr>
    		<td>
    		<form name="frm"  method="post" enctype="multipart/form-data" action="">
    		<!--<form id="addvehicle" method="post" enctype="multipart/form-data"  name="addvehicle">-->
    			<table cellspacing="5"><br />
    			<tr><td colspan="2"><?   //echo $message; ?></td></tr>
    				<tr>
    					<td>Make :</td> 
    					<td>
    					<?php getVehicleMake(); 
    					?>
    					
    				  </td>
    					 <!--<td>Add New</td>
    					 <td><input name="" type="radio" value="" /><input name="c" type="text" value="" /></td>-->
    			  </tr>
    					<tr>
    <? //------------------ this is reciveing ajax sending selected vahicle make-------------------------- ?>					
    					
    					<td>Model :</td> 
    					<td>
    					<div id='txtHint'>
    					<?
    					getVehicleMakeForStockAdmin($make);
    					?>
    					</div>		
    					</td>
    				</tr>
    				<tr>
    					<td>Type :</td> 
    					<td colspan="3">
    					<? getVehicleType();?>
    					</td>
    				</tr>
    
    				<tr>
    					<td>Year :</td>
    					 <td><? getVehicleYear(); ?>Month :<? getVehicleMonth(); ?>
    					 </td>
    				</tr>
    				
    				
    				<tr><td>Images</td>
    				<td>
    				  <div id="file_container">
        			<input name="images[]" type="file"  />
       				 <br />
      				</div>
     				 <a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
     				</td>
    				
    				</tr>
    				</form> 
    				<tr>
    					<td align="right" colspan="2">
    					  
    <button type="submit" name="Submit" style="border: 0; background: transparent; cursor: pointer;" onclick="addVehicle();">   			<img src="http://bytes.com/images/searchBtn.jpg"  />
    			</button>
    			</td>
    				</tr>
    			</table>
    			
    		</td>
    	</tr>
     	
    	<tr>
        	<td><?php require_once 'footer.php'; ?>
      		</td>
      	</tr>
    </table>
    
    <!-------------------------end stock search---------------------------------------- -->
    </body>[/LEFT]
    </html>
    This is ajax code to get data
    Code:
    function addVehicle(){
    	
    	var dropdownIndex = document.getElementById('Make').selectedIndex;
    	var make = document.getElementById('Make')[dropdownIndex].value;
    	var dropdownIndex = document.getElementById('Model').selectedIndex;
    	var model = document.getElementById('Model')[dropdownIndex].value;
    	var dropdownIndex = document.getElementById('Type').selectedIndex;
    	var type = document.getElementById('Type')[dropdownIndex].value;
    	var dropdownIndex = document.getElementById('Year').selectedIndex;
    	var year = document.getElementById('Year')[dropdownIndex].value;
    	
    
    				
    	var request = GetXmlHttpObject();
    	var url="stockAdmin.php";
    	url=url+"?make="+make+"&model="+model+ "&type="+type+ "&year="+year+"&S="+1;
    				
    	
    	url=url+"&sid="+Math.random();
    	request.onreadystatechange=stateChanged;
    	request.open("GET",url,true);
    	
    ;
    	request.send(null);
    
    	function stateChanged()
       {
    	 if (request.readyState==4)  
    		{
    			
    			document.getElementById("a").innerHTML=request.responseText;
    			document.frm.reset();
    			  
    		}
     	}
    	
    }
Working...