PDA

View Full Version : How do i process the list and option field form element in CRUD with jQuery and PHP



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

Beverleyh
06-10-2013, 11:51 AM
Please enclose code in:



code goes here


tags so it looks like so:


code goes here

You may use the editor's # button (not the #i button) to generate the tags. Or just type them manually into the editor. If typing them, don't forget to use the square brackets and also to use the / in the closing tag. Type exactly as shown in the above example.

traq
06-10-2013, 03:08 PM
couple things...

What is happening? Are any exceptions thrown? Do you get other error messages? What is the output from your script?

You're using $language as though it was an object. When you assign $language = $row['language'], it will actually be a string (or null).

You might try dumping the contents of the $_POST array to see if they actually contain the values you expect.

megha_3000
06-11-2013, 05:17 AM
No i didn't get any error message.as i stated before it's finding the employeeid at the field of employeeid .
But can't get the language at the select list field on update form. and didn't make the button checked which value is given.Also don't make any change in database after changing values. seems that values are not passing.
i did a little bit change to check values at the update_form.php script i.e.



//values to fill up our form
$id = $row['id'];
$employeeid = $row['employeeid'];
$language = $row['language'];
echo $language;
$readlevel = $row['readlevel'];
echo $readlevel;


and i found that the values are showing.

traq
06-11-2013, 03:19 PM
Here, you assign $language to the value in $row['language']:

$language = $row['language'];
If your DB query was successful, that will be a string. If it failed, it will be null (and you will have an "undefined variable" notice).

However, later on, you treat $language as though it were an object:

$language->language
From the code you posted, I would expect that to give a warning about "trying to read member property of a non-object" or something similar.

Do you have error reporting enabled?

megha_3000
06-12-2013, 04:52 AM
Yes i have error reporting enabled.i used error_reporting(E_ALL & ~E_NOTICE); at first of the script. Still it doesn't show any error. but in case of


$language->language


I used a function which is


function populate_select($table,$fields_id,$fields_value,$selected){
$conn=db_connect(HOST,USER,PASS,DB,PORT);
$sql="Select $fields_id,$fields_value From $table Order By $fields_value";
$results=query($sql,$conn);
while ($row = fetch_object($results)){
$SelectedField=($row->$fields_id==$selected) ? " selected" : "";
echo "<option value='" . $row->$fields_id ."'". $SelectedField . ">" . $row->$fields_value . "</option>";
}
free_result($results);
}


This function is called at the try block.and this function gets the table name as $language and put their value as language.
The attached image shows what the update script actually looks. it gets only employee id but doesn't get the value of select field and option field.
5091

traq
06-12-2013, 05:47 AM
Yes i have error reporting enabled.i used error_reporting(E_ALL & ~E_NOTICE); at first of the script. Still it doesn't show any error.
you should be using E_ALL | E_STRICT instead. ~ is bitwise not - it means "do NOT show Notices" - and, in this case, Notices are the errors you need to see most. Alternatively, you can do error_reporting( -1 ), which will show every possible error.


in case of

$language->language

I used a function which is


function populate_select($table,$fields_id,$fields_value,$selected){
$conn=db_connect(HOST,USER,PASS,DB,PORT);
$sql="Select $fields_id,$fields_value From $table Order By $fields_value";
$results=query($sql,$conn);
while ($row = fetch_object($results)){
$SelectedField=($row->$fields_id==$selected) ? " selected" : "";
echo "<option value='" . $row->$fields_id ."'". $SelectedField . ">" . $row->$fields_value . "</option>";
}
free_result($results);
}


This function is called at the try block.and this function gets the table name as $language and put their value as language.
No; first, this function does nothing with your $langauge variable (it does not even appear to use a value like the one $language holds - it expects a table name and three column names). It does not "get a language" - it selects arbitrary fields from a table in the DB and prints their values in an <option> element. (Perhaps you're using it to print a select list of available languages from the DB...? but this has nothing to do with the variable we're discussing.)

Second, it does not return any value, so it cannot be used to assign a new value to $langauge. All the data the function retrieves is lost after the function completes.

Third, it is not used anywhere in the code in question anyway.

Look at this section of the code you posted. It starts by assigning $language to the value of a field retrieved from the DB. As I said earlier, this value is almost certainly a string. Unless re-assigned, it will _never_ be an object.

From this point, you start writing an HTML form. You do nothing else with $language until you start writing the <select>, at which point, you're treating it like it is an object (trying to access $language->language). Now, at this point, $language might hold the value "Bangla", but $language->language does not even exist.

<?php
/* snip */
$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')
/* snip */

Is the code you posted the actual code you are using? Have you removed any parts you thought were not relevant?

megha_3000
06-12-2013, 09:37 AM
Jazak ALLAHU Khair (May ALLAH give you best reward in feedback). Thank You. Alhamdulillah (All Praise goes to ALLAH).
I did changes as you recommended. After that the edit form works fine 5092.

But update is not happening!

traq
06-12-2013, 06:42 PM
I didn't suggest any changes; I was just trying to understand your problem. But I'm happy to hear it works now!

I will look at your update code later today (I'm out of the office right now).

traq
06-13-2013, 03:39 AM
Alright: are you talking about this code now?

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

First thing I'd ask is if you're sure you even get to this page. Your form's action is set as "#", meaning the form will submit to the current page (which you identify as update_form.php), not the page which contains this code (which you identify as update.php).

megha_3000
06-13-2013, 05:20 AM
Too much basic error i did in the update.php script. That is in

$query = "update
language
set
employeeid = :employeeid,
language = :language,
readlevel = :readlevel,
where
id = :id";
Which will be like this:

$query = "update
language
set
employeeid = :employeeid,
language = :language,
readlevel = :readlevel
where
id = :id"; :)

novko
03-28-2014, 01:24 PM
I had the exact same problem....
What I had wrong in my code was the when I was updating I was getting the "employeeid" but not the ID of the actual row.



$query = "update
language
set
employeeid = :employeeid,
language = :language,
readlevel = :readlevel,
where
id = 1"; // try set a value of an actual ID row that you have for example 1


If it works then the solution is easy :)

Enjoy