467,895 Members | 1,406 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

Insert data and Images using same php form

100+
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
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start();
  3. require_once ('dbconnect.php');
  4. require_once ('commonFunctions.php');
  5.  
  6. <script language="javascript" src="ajax.js"></script>
  7.  
  8.  
  9. <script type="text/javascript">
  10. function add_file_field(){
  11. var container=document.getElementById('file_container');
  12. var file_field=document.createElement('input');
  13. file_field.name='images[]';
  14. file_field.type='file';
  15. container.appendChild(file_field);
  16. var br_field=document.createElement('br');
  17. container.appendChild(br_field);
  18. }
  19. </script>
  20.  
  21. </head>
  22.  
  23. <?
  24.  
  25. $make= "";
  26.  
  27.  
  28. if(isset($_GET['Submit'])==2){
  29.     $make=$_GET['make'];
  30.     getVehicleModel($make);
  31.     return;
  32. }    
  33.  
  34. if(isset($_GET['S'])==1){
  35.  
  36. echo $make;
  37.     $make=$_GET['make'];
  38.     $model=$_GET['model'];
  39.     $type=$_GET['type'];
  40.     $year=$_GET['year'];
  41.     $month=$_GET['month'];
  42.     $fuel=$_GET['fuel'];
  43.     $transmission=$_GET['transmission'];
  44.     $displacement=$_GET['displacement'];
  45.     $color=$_GET['color'];
  46.     $milage=$_GET['milage'];
  47.     $price=$_GET['price'];
  48.     $other=$_GET['other'];
  49. //---------------------insert data to vehicles table-------------------------------------------    
  50.     $sql = "INSERT INTO vehicles VALUES('','$make','$model','$type','$year','$month','$fuel','$transmission','$color','$price','$displacement','$milage','$other')";
  51.  
  52.     $result = mysql_query($sql);
  53.     echo "SUCCESS";
  54.  
  55.  
  56.     //$vehicles_pk = mysql_insert_id();
  57.  
  58.     if (!$result) 
  59.         {
  60.             echo "Error in SQL query: " . mysql_error();
  61.         }
  62.         else{
  63.  
  64.             $message = "<p class=message>Successfully Inserted !</p>";
  65.          }
  66.  
  67.     return;
  68.     }
  69.  
  70.  
  71.  
  72.     if (isset($_POST['Submit'])) {
  73.     $number_of_file_fields = 0;
  74.     $number_of_uploaded_files = 0;
  75.     $number_of_moved_files = 0;
  76.     $uploaded_files = array();
  77.     $upload_directory = dirname(__file__) . '/vehicles/'; 
  78.  
  79.  
  80.  
  81.     for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
  82.         $number_of_file_fields++;
  83.         if ($_FILES['images']['name'][$i] != '') { 
  84.             $number_of_uploaded_files++;
  85.             $uploaded_files[] = $_FILES['images']['name'][$i];
  86.             if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
  87.                 $number_of_moved_files++;
  88.             }
  89.  
  90.         }
  91.  
  92.     }
  93.  
  94.     $name= implode(',', $uploaded_files);
  95.     echo $name;
  96.  
  97.  
  98. }
  99. ?>
  100.  
  101.  
  102.  
  103. <table width="566" height="100%" border="0" align="center" cellpadding="0" cellspacing="0"  >
  104.  
  105.   <!--------------body-------------------------------------------------------------->
  106.      <tr>
  107.         <td>
  108.         <form name="frm"  method="post" enctype="multipart/form-data" action="">
  109.         <!--<form id="addvehicle" method="post" enctype="multipart/form-data"  name="addvehicle">-->
  110.             <table cellspacing="5"><br />
  111.             <tr><td colspan="2"><?   //echo $message; ?></td></tr>
  112.                 <tr>
  113.                     <td>Make :</td> 
  114.                     <td>
  115.                     <?php getVehicleMake(); 
  116.                     ?>
  117.  
  118.                   </td>
  119.                      <!--<td>Add New</td>
  120.                      <td><input name="" type="radio" value="" /><input name="c" type="text" value="" /></td>-->
  121.               </tr>
  122.                     <tr>
  123. <? //------------------ this is reciveing ajax sending selected vahicle make-------------------------- ?>                    
  124.  
  125.                     <td>Model :</td> 
  126.                     <td>
  127.                     <div id='txtHint'>
  128.                     <?
  129.                     getVehicleMakeForStockAdmin($make);
  130.                     ?>
  131.                     </div>        
  132.                     </td>
  133.                 </tr>
  134.                 <tr>
  135.                     <td>Type :</td> 
  136.                     <td colspan="3">
  137.                     <? getVehicleType();?>
  138.                     </td>
  139.                 </tr>
  140.  
  141.                 <tr>
  142.                     <td>Year :</td>
  143.                      <td><? getVehicleYear(); ?>Month :<? getVehicleMonth(); ?>
  144.                      </td>
  145.                 </tr>
  146.  
  147.  
  148.                 <tr><td>Images</td>
  149.                 <td>
  150.                   <div id="file_container">
  151.                 <input name="images[]" type="file"  />
  152.                     <br />
  153.                   </div>
  154.                   <a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />
  155.                  </td>
  156.  
  157.                 </tr>
  158.                 </form> 
  159.                 <tr>
  160.                     <td align="right" colspan="2">
  161.  
  162. <button type="submit" name="Submit" style="border: 0; background: transparent; cursor: pointer;" onclick="addVehicle();">               <img src="http://bytes.com/images/searchBtn.jpg"  />
  163.             </button>
  164.             </td>
  165.                 </tr>
  166.             </table>
  167.  
  168.         </td>
  169.     </tr>
  170.  
  171.     <tr>
  172.         <td><?php require_once 'footer.php'; ?>
  173.           </td>
  174.       </tr>
  175. </table>
  176.  
  177. <!-------------------------end stock search---------------------------------------- -->
  178. </body>
</html>
  •  
  • This is ajax code to get data
    Expand|Select|Wrap|Line Numbers
    1. function addVehicle(){
    2.  
    3.     var dropdownIndex = document.getElementById('Make').selectedIndex;
    4.     var make = document.getElementById('Make')[dropdownIndex].value;
    5.     var dropdownIndex = document.getElementById('Model').selectedIndex;
    6.     var model = document.getElementById('Model')[dropdownIndex].value;
    7.     var dropdownIndex = document.getElementById('Type').selectedIndex;
    8.     var type = document.getElementById('Type')[dropdownIndex].value;
    9.     var dropdownIndex = document.getElementById('Year').selectedIndex;
    10.     var year = document.getElementById('Year')[dropdownIndex].value;
    11.  
    12.  
    13.  
    14.     var request = GetXmlHttpObject();
    15.     var url="stockAdmin.php";
    16.     url=url+"?make="+make+"&model="+model+ "&type="+type+ "&year="+year+"&S="+1;
    17.  
    18.  
    19.     url=url+"&sid="+Math.random();
    20.     request.onreadystatechange=stateChanged;
    21.     request.open("GET",url,true);
    22.  
    23. ;
    24.     request.send(null);
    25.  
    26.     function stateChanged()
    27.    {
    28.      if (request.readyState==4)  
    29.         {
    30.  
    31.             document.getElementById("a").innerHTML=request.responseText;
    32.             document.frm.reset();
    33.  
    34.         }
    35.      }
    36.  
    37. }
    38.  
    39.  
    Aug 24 '11 #1
    • viewed: 1071
    Share:

    Post your reply

    Sign in to post your reply or Sign up for a free account.

    Similar topics

    4 posts views Thread by Rafael Tejera | last post: by
    6 posts views Thread by VitaminB | last post: by
    By using this site, you agree to our Privacy Policy and Terms of Use.