Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys, im wanna update a records, and i wanna post the specific record using my ID. here' my code

HTML
<form class="form-horizontal" action="update.php?id=<?php echo $id?>" method="post">
 <div class="control-group <?php echo !empty($nameError)?'error':'';?>">
<label class="control-label">Name</label>
<div class="controls">
<input name="name" type="text"  placeholder="Name" value="<?php echo !empty($name)?$name:'';?>">
<?php if (!empty($nameError)): ?>
<span class="help-inline"><?php echo $nameError;?></span>
<?php endif; ?>
</div>
 </div>
<div class="control-group <?php echo !empty($emailError)?'error':'';?>">
 <label class="control-label">Email Address</label>
<div class="controls">
<input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email)?$email:'';?>">
<?php if (!empty($emailError)): ?>
<span class="help-inline"><?php echo $emailError;?></span>
<?php endif;?>
</div>
</div>
<div class="control-group <?php echo !empty($mobileError)?'error':'';?>">
<label class="control-label">Mobile Number</label>
<div class="controls">
<input name="mobile" type="text"  placeholder="Mobile Number" value="<?php echo !empty($mobile)?$mobile:'';?>">
<?php if (!empty($mobileError)): ?>
<span class="help-inline"><?php echo $mobileError;?></span>
<?php endif;?>
 </div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>



this is my function
PHP
public function postUpdateStud(){
	require_once ('database.php');
	$id = null;
	if ( !empty($_GET['id'])) {
		$id = $_REQUEST['id'];
	}
	
	if ( null==$id ) {
		header("Location: index.php");
	}
	
	if ( !empty($_POST)) {
		// keep track validation errors
		$nameError = null;
		$emailError = null;
		$mobileError = null;
		
		// keep track post values
		$name = $_POST['name'];
		$email = $_POST['email'];
		$mobile = $_POST['mobile'];
		
		// validate input
		$valid = true;
		if (empty($name)) {
			$nameError = 'Please enter Name';
			$valid = false;
		}
		
		if (empty($email)) {
			$emailError = 'Please enter Email Address';
			$valid = false;
		} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
			$emailError = 'Please enter a valid Email Address';
			$valid = false;
		}
		
		if (empty($mobile)) {
			$mobileError = 'Please enter Mobile Number';
			$valid = false;
		}
		
		// update data
		if ($valid) {
			$pdo = Database::connect();
			$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "UPDATE customers  set name = ?, email = ?, mobile =? WHERE id = ?";
			$q = $pdo->prepare($sql);
			$q->execute(array($name,$email,$mobile,$id));
			Database::disconnect();
			header("Location: index.php");
		}
	} else {
		$pdo = Database::connect();
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "SELECT * FROM customers where id = ?";
		$q = $pdo->prepare($sql);
		$q->execute(array($id));
		$data = $q->fetch(PDO::FETCH_ASSOC);
		$name = $data['name'];
		$email = $data['email'];
		$mobile = $data['mobile'];
		Database::disconnect();
	}
	}



how can i convert it to laravel blade
PHP
action="update.php?id=<?php echo $id??>"
Posted
Updated 21-Jul-16 21:06pm
v2

1 solution

Hi, you need to add routes file along with the blade template changes to work with Laravel. Please try below code.

// *.blade.php

{{ link_to_route('customer.update', 'Link Text', [
    'id' => $id // this will be the customer ID which will be passed as QueryString
]); }}

// routes.php
Route::get('/update', [
    'as'   => 'customer.update',
    'uses' => 'CustomerController@postUpdateStud'
]);

// CustomerController.php

namespace ....; // add the namespace for the CustomerController Class
class CustomerController extends Controller {
    public function postUpdateStud(){
	require_once ('database.php');  // you need to use "USE" for including any files
	$id = null;
	if ( !empty($_GET['id'])) {
            $id = $_REQUEST['id'];
        }
	
	if ( null==$id ) {
            header("Location: index.php");
        }
	
	if ( !empty($_POST)) {
            // keep track validation errors
            $nameError = null;
            $emailError = null;
            $mobileError = null;
		
            // keep track post values
            $name = $_POST['name'];
            $email = $_POST['email'];
            $mobile = $_POST['mobile'];
		
            // validate input
            $valid = true;
            if (empty($name)) {
                $nameError = 'Please enter Name';
                $valid = false;
            }
		
            if (empty($email)) {
                $emailError = 'Please enter Email Address';
                $valid = false;
            } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
                $emailError = 'Please enter a valid Email Address';
                $valid = false;
            }
		
            if (empty($mobile)) {
                $mobileError = 'Please enter Mobile Number';
                $valid = false;
            }
		
            // update data
            if ($valid) {
                $pdo = Database::connect();
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $sql = "UPDATE customers  set name = ?, email = ?, mobile =? WHERE id = ?";
                $q = $pdo->prepare($sql);
                $q->execute(array($name,$email,$mobile,$id));
                Database::disconnect();
                header("Location: index.php");
            }
        } else {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "SELECT * FROM customers where id = ?";
            $q = $pdo->prepare($sql);
            $q->execute(array($id));
            $data = $q->fetch(PDO::FETCH_ASSOC);
            $name = $data['name'];
            $email = $data['email'];
            $mobile = $data['mobile'];
            Database::disconnect();
        }
    }
}



Note : All the codes present in "postUpdateStud" method is just the plain PHP codes. You need to work on it to change into Laravel standard.

Follow this link :- Introduction - Laravel - The PHP Framework For Web Artisans[^]

I hope this will help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900