Jump to content


Photo

Help in Forms


  • Please log in to reply
3 replies to this topic

#1 john666

john666

    Member

  • Members
  • PipPip
  • 21 posts

Posted 04 December 2013 - 07:28 AM

I having Problem in Forms i have some Fields and i want if Some 1 try to update a Record and due to some error the Record could not update i want that Data should not Remove from Forms it should be in the input  fields... here is my Code

<?php
include('header.php');
include('config.php');
include('navigation.php');

// get only integer value from request
$id = intval(@$_REQUEST['student_id']);

// we are going to show only one student so we need to 
// restrict the query to find one record only
$query="select *from students where id='$id' limit 1";
$result=mysql_query($query);

// check if record not found then no need to open the page
if (!$result) {
	// this check is implement, just because of we are on update page
	// if id is invalid it's mean no record or invalid record
	// in this case no need to stay on this page.
	header("Location: students.php");
	exit(); // if header generate warning exit will stop page rendering.
}

// we have only one record we know, because we set the LIMIT 1
// so no need for while loop. While loop is use when records are more then one.
// Here we have one only
	$data		= mysql_fetch_assoc($result);	
	
	$stdname	= $data['stdname'] ; 
	$stdrollno	= $data['stdrollno'] ;
	$stdfname	= $data['stdfname'] ; 
	$stdclass	= $data['stdclass'] ; 
	$stdcell	= $data['stdcell'] ; 
	$stddob		= $data['stddob'] ; 
	$stdaddress	= $data['stdaddress'] ; 
	$stdfee		= $data['stdfee'] ; 
	$class_id	= $data['class_id']; // here we have class_id
   
	 
	// now we need list of all classes to display in dropdown
	$query="select * from classes ";
	$classes_result = mysql_query($query) or die(mysql_error());


?>

<div class="data">
<form method="POST" action="stdupdate-validation.php">
<?php echo $stdaddress; ?>
<table class="update" border="1px" align="center">
<tr><td>Student Name</td><td><input type="text"value="<?php echo $stdname; ?>" name="stdname"></td></tr>

<tr> <td> Roll No</td> <td><input type="text" value="<?php echo$stdrollno; ?>" name="stdrollno" > </td></tr>

 <tr> <td> Father Name</td> <td><input type="text" value="<?php echo$stdfname; ?>" name="stdfname"> </td></tr>

  <tr>
		<td>Class </td>
		<td> <select name="stdclass">
				<option value="0">No Class</option>
				<?php
					// check if classes are available
					if(mysql_num_rows($classes_result)) {						
						
						// because we may have more then one class in query result
						// so now we need LOOP
						while ($class = mysql_fetch_assoc($classes_result)) {
							
								// now we have to check the class ID in classes while loading the list into dropdown
								$selected = ($class_id == $class['id']) ?  'selected="selected" ' : "";
								echo '<option value="'.$class['id'].'" '.$selected.' >'.$class['name'].'</option>';
								
							}
						}
					
				?>
			</select> 
		</td>
	</tr>

   <tr> <td>Cell No</td> <td><input type="text" value="<?php echo$stdcell; ?>" name="stdcell"> </td></tr>

    <tr><td>Date OF Birth</td> <td> <input type="text" value="<?php echo$stddob; ?>" name="stddob"> </td></tr>
     <input type="hidden" value="<?php echo$class_id; ?>" name="class_id">

   <tr><td>Address</td><td> <textarea cols="30" rows="6" value="<?php echo $stdaddress; ?>" name="stdaddress"> </textarea> </td></tr>

<tr><td>Ammount Of Fee</td><td> <input type="text" value="<?php echo$stdfee; ?>" name="stdfee"> </td></tr>
<tr> <td colspan="2" align="center"><input type="submit" value="submit" name="submit"></td></tr>
<input type="hidden" value="<?php echo $id ; ?>" name="id">
</table>


</form>



</div>

<a href="students.php"> Back to Students</a>
<?php
include('footer.php');

 here Updation update-validation.php start
<?php
include('config.php');

if(isset($_POST['submit']))
{
$id=@$_REQUEST['id'];
$stdclass=$_SESSION['stdclass']= @$_REQUEST['stdclass'];
$stdname=$_SESSION['stdname']=@$_REQUEST['stdname'];
$stdrollno=@$_REQUEST['stdrollno'];
$stdfname=@$_REQUEST['stdfname'];
$stdcell=@$_REQUEST['stdcell'];
$stddob=@$_REQUEST['stddob'];
$stdaddress=@$_REQUEST['stdaddress'];
$stdfee=@$_REQUEST['stdfee'];
$class_id=@$_REQUEST['class_id'];
    $stdname=trim($stdname);
	$stdrollno=trim($stdrollno);
	$stdfname=trim($stdfname);
	$stdclass=trim($stdclass);
	$stdcell=trim($stdcell);
	$stddob=trim($stddob);
	$stdaddress=trim($stdaddress);
	$stdfee=trim($stdfee);
	$query="select *from students" ;
	$result=mysql_query($query)or die(mysql_error());
    while ($row=mysql_fetch_assoc($result)) {
    	if ($row['id'] !=$id and $row['stdclass'] == $stdclass and $row['stdrollno'] == $stdrollno) {
    		
    		$msg='Record could Not Updated Record Already Exist';
    		header('location:std_update.php?msg=Roll Number And Class Already Exist');
    	
    		
    		exit();
    	}
    }

	$query="UPDATE students SET stdname='$stdname', stdrollno='$stdrollno', stdfname='$stdfname', stdclass='$stdclass', stdcell='$stdcell', stddob='$stddob', stdaddress='$stdaddress', stdfee='$stdfee', class_id='$stdclass' where id='$id'";
	
	mysql_query($query);
	
	
	mysql_query($query) or die(mysql_error());
	if ($query) {
		$msg="Record Updates SuccessFully";
		header('location:students.php?'.$msg);
	} else {
	$msg=" Record Not Updated";
		header('location:std_update.php?'.$msg);
	}
		
	
	
}

?>

?>


#2 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,585 posts

Posted 04 December 2013 - 08:54 AM

What are the exact errors?


Blogging about PHP and other web development topics at ScorpBytes.com

#3 john666

john666

    Member

  • Members
  • PipPip
  • 21 posts

Posted 04 December 2013 - 09:21 AM

What are the exact errors?

Brother There is No error in Code But i want some additional code in this ...the problem is that when i update record if same roll number and same class record is already exist it show an Error mesg and All input fields get Empty i want that these Fields should not empty data should exist in these fields..so i need code for it



#4 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,018 posts

Posted 04 December 2013 - 09:39 AM

Separate your form into another file, then include it when you need it.

 

Add your form code to a new file called update_form.php

<div class="data">
<form method="POST" action="stdupdate-validation.php">
<?php echo $stdaddress; ?>
<table class="update" border="1px" align="center">
<tr><td>Student Name</td><td><input type="text"value="<?php echo $stdname; ?>" name="stdname"></td></tr>

<tr> <td> Roll No</td> <td><input type="text" value="<?php echo$stdrollno; ?>" name="stdrollno" > </td></tr>

 <tr> <td> Father Name</td> <td><input type="text" value="<?php echo$stdfname; ?>" name="stdfname"> </td></tr>

  <tr>
        <td>Class </td>
        <td> <select name="stdclass">
                <option value="0">No Class</option>
                <?php
                    // now we need list of all classes to display in dropdown
                    $classes_result = mysql_query("select * from classes") or die(mysql_error());

                    // check if classes are available
                    if(mysql_num_rows($classes_result)) {                       
                        
                        // because we may have more then one class in query result
                        // so now we need LOOP
                        while ($class = mysql_fetch_assoc($classes_result)) {
                            
                                // now we have to check the class ID in classes while loading the list into dropdown
                                $selected = ($class_id == $class['id']) ?  'selected="selected" ' : "";
                                echo '<option value="'.$class['id'].'" '.$selected.' >'.$class['name'].'</option>';
                                
                            }
                        }
                    
                ?>
            </select> 
        </td>
    </tr>

   <tr> <td>Cell No</td> <td><input type="text" value="<?php echo$stdcell; ?>" name="stdcell"> </td></tr>

    <tr><td>Date OF Birth</td> <td> <input type="text" value="<?php echo$stddob; ?>" name="stddob"> </td></tr>
     <input type="hidden" value="<?php echo$class_id; ?>" name="class_id">

   <tr><td>Address</td><td> <textarea cols="30" rows="6" value="<?php echo $stdaddress; ?>" name="stdaddress"> </textarea> </td></tr>

<tr><td>Ammount Of Fee</td><td> <input type="text" value="<?php echo$stdfee; ?>" name="stdfee"> </td></tr>
<tr> <td colspan="2" align="center"><input type="submit" value="submit" name="submit"></td></tr>
<input type="hidden" value="<?php echo $id ; ?>" name="id">
</table>


</form>

Change the redirect here in update-validation.php

header('location:std_update.php?'.$msg); 

 to

 include 'update_form.php'; // include the update form

Change std_update.php to

<?php
include('header.php');
include('config.php');
include('navigation.php');

// get only integer value from request
$id = intval(@$_REQUEST['student_id']);

// we are going to show only one student so we need to 
// restrict the query to find one record only
$query="select *from students where id='$id' limit 1";
$result=mysql_query($query);

// check if record not found then no need to open the page
if (!$result) {
    // this check is implement, just because of we are on update page
    // if id is invalid it's mean no record or invalid record
    // in this case no need to stay on this page.
    header("Location: students.php");
    exit(); // if header generate warning exit will stop page rendering.
}

// we have only one record we know, because we set the LIMIT 1
// so no need for while loop. While loop is use when records are more then one.
// Here we have one only
    $data       = mysql_fetch_assoc($result);   
    
    $stdname    = $data['stdname'] ; 
    $stdrollno  = $data['stdrollno'] ;
    $stdfname   = $data['stdfname'] ; 
    $stdclass   = $data['stdclass'] ; 
    $stdcell    = $data['stdcell'] ; 
    $stddob     = $data['stddob'] ; 
    $stdaddress = $data['stdaddress'] ; 
    $stdfee     = $data['stdfee'] ; 
    $class_id   = $data['class_id']; // here we have class_id

    include 'update_form.php'; // include the update form
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com