Not storing data in database(php)

Discussion in 'PHP' started by vivek_master146, Oct 19, 2012.

  1. #1
    <html>
    <script language="JavaScript">
    		function validated(){
    
    			var matric = document.s.matric.value;
    			var name = document.s.name.value;
    			var course = document.s.course.value;
    			var sem = document.s.sem.value;
    			var semint = parseInt(sem);
    			var tel = document.s.tel.value;
    			var telint = parseInt(tel);
    			var address = document.s.address.value;
    			var picture = document.s.picture.value;
    
    			if(matric==""){	
    				window.alert("Please enter matric number!");
    				document.s.matric.focus();
    				return false;
    				}
    			if(name==""){	
    				window.alert("Please enter student name!");
    				document.s.name.focus();
    				return false;
    			}
    			if(course==""){	
    				window.alert("Please enter student course!");
    				document.s.course.focus();
    				return false;
    				}
    			if(isNaN(semint)){	
    				window.alert("Please enter student semester!");
    				document.s.sem.focus();
    				return false;
    			}
    			if(isNaN(telint)){	
    				window.alert("Please enter contact number!");
    				document.s.tel.focus();
    				return false;
    			}
    			if(address==""){	
    				window.alert("Please enter student address!");
    				document.s.address.focus();
    				return false;
    			}
    			if(picture==""){	
    				window.alert("Please enter student picture!");
    				document.s.picture.focus();
    				return false;
    			}
    }
    </script>
    <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td align="center"><h1><font color="#0000FF" face="Arial">ADD STUDENT PROFILE</font></h1></td>
      </tr>
    </table>
    <br><br>
    <table width="100%" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td align="right"><font size="1" face="Arial"><a href='main.php'>Student List</a></font></td>
      </tr>
    </table>
    <br><br>
    <form method="post" action="student_form.php" enctype="multipart/form-data" name='s' onsubmit='return validated()';>
    <table width="70%" border="0" align="center" cellpadding="2" cellspacing="2">
      <tr>
        <td width="33%" align="right"><font size="2" face="Arial"><strong>Matric Number</strong></font></td>
        <td width="5%" align="center">:</td>
        <td width="62%"><input type='text' name='matric' size=30 maxlength=15></td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Name</strong></font></td>
        <td align="center">:</td>
        <td><input type='text' name='name' size=30 maxlength=50></td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Course</strong></font></td>
        <td align="center">:</td>
        <td><input type='text' name='course' size=30 maxlength=50></td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Semester</strong></font></td>
        <td align="center">:</td>
        <td><input type='text' name='sem' size=5 maxlength=2></td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Sex</strong></font></td>
        <td align="center">:</td>
        <td><input type='radio' name='sex' value='Male' checked> Male  <input type='radio' name='sex' value='Female'> Female</td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Contact Number</strong></font></td>
        <td align="center">:</td>
        <td><input type='text' name='tel' size=30 maxlength=50></td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Address</strong></font></td>
        <td align="center">:</td>
        <td><input type='text' name='address' size=50 maxlength=100></td>
      </tr>
      <tr>
        <td align="right"><font size="2" face="Arial"><strong>Picture</strong></font></td>
        <td align="center">:</td>
        <td>
    	<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
    	<input type="file" name="picture"  size="40"> <font size="1" face="Arial"> Maxsize 1MB</font>
    	</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><input type='submit' name="submit" value='Submit'> <input type='reset' value='Reset'></td>
      </tr>
    </table>
    </form>
    HTML:
    <?php
    	
    	if ($submit) 
    	{
    		
    		include 'db_connect.php';		
    		
    		$data = addslashes(fread(fopen($picture, "r"), filesize($picture)));
    
    		$pjpeg="image/pjpeg";
    		$jpeg="image/jpeg";
    		$gif="image/gif";
    		$png="image/png";
    		$bmp="image/bmp";
    
    		if ($picture_type == $pjpeg OR $picture_type == $jpeg OR $picture_type == $gif OR $picture_type == $png OR $picture_type == $bmp)
    		{
    		
    		$sql="INSERT INTO info (matric, name, course, sem, sex, tel, address ,bin_data,filename,filesize,filetype)
    			VALUES ('$matric', '$name', '$course', '$sem', '$sex', '$tel', '$address', '$data','$picture_name','$picture_size','$picture_type')";
    		$result=mysql_query($sql);
    	
    		header ("Location: main.php");
    		}
    		else{
    		echo "<script language='JavaScript'>";
    		echo "window.alert('Error! You only can upload jpeg, gif, png, bmp file type.')";
    		echo "</script>"; 
    		}
    	}
    	
    ?>
    PHP:
    </body>
    </html>
    HTML:

    The above code is not storing the data in the database. I don't get any error but it is not saving anything in the database. I have created database, table with the correct settings. Instead of action="student_form.php" i have also tried action="<?php echo $_SERVER['PHP_SELF'];"> but it does not work.

    Please suggest.
     
    vivek_master146, Oct 19, 2012 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Unless you have removed some code to post it here, your PHP script never sets the variable "$submit", so the MySQL code would never get executed. Further, your script never retrieves the form data from $_POST, so there's no data to be stored in the database anyway.
     
    rainborick, Oct 19, 2012 IP
  3. KsNitro

    KsNitro Greenhorn

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #3
    If all of your validation checks in your javascript function pass there is no return value, so the return is undefined which is a falsy value in javascript. If all checks go through return a truthy value.

    If that doesn't work, then test certain parts of your script. For instance, echo the $submit var in your php script somewhere so you can see its value. Or even echo anything in your php script to see if it is getting that far. You can test the javascript in the same way.

    As rainborick said above, you will need to extract the post data in your php script before $submit or other form data have values.
     
    KsNitro, Oct 19, 2012 IP
  4. php_developer

    php_developer Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Your code is running very properly i have cheked on my pc might be there is some problem with your data base or connection is not proper go through the connection of your data base.
     
    php_developer, Oct 20, 2012 IP
  5. vivek_master146

    vivek_master146 Peon

    Messages:
    134
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for reply. I have changed if($submit) to if ($_POST['submit']) and i have extracted the data using these lines of code :-
    
    $matric = $_POST['matric'];
    $name	= $_POST['name'];
    $sem=$_POST['sem'];
    $sex=$_POST['sex'];
    $tel=$_POST['tel'];
    $address=$_POST['address'];
    $data = $_POST['picture'];
    $course = $_POST['course'];
    PHP:
    But still i am not able to save picture in blob field. Everything else is being saved(course,address,matric,name etc.) in db except picture and its related info. Why image is not being saved in db ? I have stored blob in db while running another project.

    @php_developer

    How come the code is running perfectly on your machine. As rainborick and Ksnitro suggested my php code does not extract the data posted. Is it also saving blob ?
     
    vivek_master146, Oct 20, 2012 IP