megha_3000
06-10-2013, 09:09 AM
Dear All,
I got a tutorial http://www.codeofaninja.com/2013/05/crud-with-php-jquery.html
which gives a clear idea about CRUD operation with a simple user form using PHP Data Objects (PDO) extension.
I added a select field and a option field. Then the
insertion happens successfully. But the difficulty occurs on update.
I Tried at many ways to update. But the update form only gets the id. and can't recognize the
language field and the Read level field.
How can i overcome this.
The sample code are as follows-
Update a Record :
update_form.php contains
<?php
session_start();
try {
include 'libs/db_connect.php';
// include 'includes/queryfunctions.php';
include_once('includes/functions2.php');
//prepare query
$query = "select
id, employeeid, language, readlevel
from
language
where
id = ?
limit 0,1";
$stmt = $con->prepare( $query );
//this is the first question mark
$stmt->bindParam(1, $_REQUEST['user_id']);
//execute our query
if($stmt->execute()){
//store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//values to fill up our form
$id = $row['id'];
$employeeid = $row['employeeid'];
$language = $row['language'];
$readlevel = $row['readlevel'];
}else{
echo "Unable to read record.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
<!--we have our html form here where new user information will be entered-->
<form id='updateUserForm' action='#' method='post' border='0'>
<table>
<tr>
<td>Employee ID</td>
<td><input name="employeeid" type="text" id="employeeid" value="<?php echo $employeeid; ?>"/></td>
</tr>
<tr>
<td>Language</td>
<td> <select name="language" id="language">
<option value="">--Select Language--</option>
<option value="Bangla" <?php if($language->language=='Bangla') echo 'selected'?>>Bangla</option>
<option value="English" <?php if($language->language=='English') echo 'selected'?>>English</option>
</select>
</td>
</tr>
<tr>
<td>Read</td>
<td>
<label>
<input type="radio" name="readlevel" value="Beginner" <?php if($language->readlevel=='Beginner') echo 'checked'?>>
Beginner</label>
<label>
<input type="radio" name="readlevel" value="Intermediate" <?php if($language->readlevel=='Intermediate') echo 'checked'?>>
Intermediate
</label>
<label>
<input type="radio" name="readlevel" value="Advanced (Fluent)" <?php if($language->readlevel=='Advanced (Fluent)') echo 'checked'?>>
Advanced (Fluent)
</label>
</td>
</tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type="hidden" name="id" value=<?php echo $language->id; ?>>
<input type='submit' value='Update' class='customBtn' />
</td>
</tr>
</table>
</form>
update.php contains-
<?php
//include database connection
include 'libs/db_connect.php';
try{
//write query
//in this case, it seemed like we have so many fields to pass and
//its kinda better if we'll label them and not use question marks
//like what we used here
$query = "update
language
set
employeeid = :employeeid,
language = :language,
readlevel = :readlevel,
where
id = :id";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
$stmt->bindParam(':employeeid', $_POST['employeeid']);
$stmt->bindParam(':language', $_POST['language']);
$stmt->bindParam(':readlevel', $_POST['readlevel']);
$stmt->bindParam(':id', $_POST['id']);
// Execute the query
if($stmt->execute()){
echo "Language was updated.";
}else{
echo "Unable to update language.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
Thanking You
Megha
I got a tutorial http://www.codeofaninja.com/2013/05/crud-with-php-jquery.html
which gives a clear idea about CRUD operation with a simple user form using PHP Data Objects (PDO) extension.
I added a select field and a option field. Then the
insertion happens successfully. But the difficulty occurs on update.
I Tried at many ways to update. But the update form only gets the id. and can't recognize the
language field and the Read level field.
How can i overcome this.
The sample code are as follows-
Update a Record :
update_form.php contains
<?php
session_start();
try {
include 'libs/db_connect.php';
// include 'includes/queryfunctions.php';
include_once('includes/functions2.php');
//prepare query
$query = "select
id, employeeid, language, readlevel
from
language
where
id = ?
limit 0,1";
$stmt = $con->prepare( $query );
//this is the first question mark
$stmt->bindParam(1, $_REQUEST['user_id']);
//execute our query
if($stmt->execute()){
//store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//values to fill up our form
$id = $row['id'];
$employeeid = $row['employeeid'];
$language = $row['language'];
$readlevel = $row['readlevel'];
}else{
echo "Unable to read record.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
<!--we have our html form here where new user information will be entered-->
<form id='updateUserForm' action='#' method='post' border='0'>
<table>
<tr>
<td>Employee ID</td>
<td><input name="employeeid" type="text" id="employeeid" value="<?php echo $employeeid; ?>"/></td>
</tr>
<tr>
<td>Language</td>
<td> <select name="language" id="language">
<option value="">--Select Language--</option>
<option value="Bangla" <?php if($language->language=='Bangla') echo 'selected'?>>Bangla</option>
<option value="English" <?php if($language->language=='English') echo 'selected'?>>English</option>
</select>
</td>
</tr>
<tr>
<td>Read</td>
<td>
<label>
<input type="radio" name="readlevel" value="Beginner" <?php if($language->readlevel=='Beginner') echo 'checked'?>>
Beginner</label>
<label>
<input type="radio" name="readlevel" value="Intermediate" <?php if($language->readlevel=='Intermediate') echo 'checked'?>>
Intermediate
</label>
<label>
<input type="radio" name="readlevel" value="Advanced (Fluent)" <?php if($language->readlevel=='Advanced (Fluent)') echo 'checked'?>>
Advanced (Fluent)
</label>
</td>
</tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type="hidden" name="id" value=<?php echo $language->id; ?>>
<input type='submit' value='Update' class='customBtn' />
</td>
</tr>
</table>
</form>
update.php contains-
<?php
//include database connection
include 'libs/db_connect.php';
try{
//write query
//in this case, it seemed like we have so many fields to pass and
//its kinda better if we'll label them and not use question marks
//like what we used here
$query = "update
language
set
employeeid = :employeeid,
language = :language,
readlevel = :readlevel,
where
id = :id";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
$stmt->bindParam(':employeeid', $_POST['employeeid']);
$stmt->bindParam(':language', $_POST['language']);
$stmt->bindParam(':readlevel', $_POST['readlevel']);
$stmt->bindParam(':id', $_POST['id']);
// Execute the query
if($stmt->execute()){
echo "Language was updated.";
}else{
echo "Unable to update language.";
}
}
//to handle error
catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
?>
Thanking You
Megha