Jump to content

Recommended Posts

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);
	}
		
	
	
}

?>

?>
Link to comment
https://forums.phpfreaks.com/topic/284520-help-in-forms/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/284520-help-in-forms/#findComment-1461242
Share on other sites

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
?>
Link to comment
https://forums.phpfreaks.com/topic/284520-help-in-forms/#findComment-1461244
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.