I Implemented my project in Yii. i showing search results. based on differed table and different field value from table. it displaying very well.
i sent from controller post values and displaying values in view part.
it displaying only the id if i give name its not displaying and showing error. i added my code here. just check and tell where i need to be change accordingly my code.
<?php
if(isset($getval)){
$as=$getval;
}else{
$as="";
}
if(isset($course)){
$as1=$course;
}else{
$as1="";
}
if(isset($type)){
$as3=$type;
}else{
$as3="";
}
if(isset($cuisine)){
$as4=$cuisine;
}else{
$as4="";
}
if(isset($calorie)){
$as5=$calorie;
}else{
$as5="";
}
<?php
echo '<p align="center"> Showing results for : '.'Name'.$getval.', '.'Course-'.$course.', '.'Cuisine-'.$cuisine.', '.'Type-'.$type;', ';'</p>';
echo ', '.'Calories-'.$calorie;
its working fine and displying only id :
if i give like this id to concern table values name not displaying
$query= Course::model()->find("course_id=$as1");
$course2=$query['course_name'];
$query1= Cuisine::model()->find("id=$as4");
$cuisine2=$query1['cuisinename'];
$query3= RecipeType::model()->find("id=$as3");
$type2=$query3['recipeType_name'];
please suggest my suitable answer
I'm not sure if I undertand your question, but, the find method return a CActiveRecord object with the first value who matches the condition.
Accessing to a column (field) is done by the "magic" properties of the object. There's a property for each column, so you can access to the 'course_name' column this way:
$query= Course::model()->find("course_id=$as1");
$course2=$query->course_name;
See This guide, specially the "Reading record" section
$Criteria = new CDbCriteria;
$Criteria->compare('course_id',$as1);
$query= Course::model()->find($Criteria);
if(!empty($query))
$course2=$query->course_name;
$post=Post::model()->find('postID=:postID', array(':postID'=>10));
http://www.yiiframework.com/doc/guide/1.1/en/database.ar
Related
I have a table where I store all the machine details, I need to delete all data and drop all tables of a specific user.
I'm trying to DROP tables based on the result from Select query, but it's not working as it is inside WHILE of SELECT query. Kindly help me with this.
$sql="SELECT M_ID FROM machine_details WHERE Username='".$inname."'";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result))
{
$delmid=$row['M_ID'];
$delsta=$delmid."_status";
$delpar=$delmid."_parameter";
$sql1="DROP TABLE IF EXISTS $delpar, $delsta";
if(!mysqli_query($conn,$sql1))
{
echo "Error in drop".mysqli_error($conn);
}
else
{
$sql2="DELETE FROM machine_details WHERE M_ID=".$delmid;
if(!mysqli_query($conn,$sql2))
{
echo "Error in machine delete".mysqli_error($conn);
}
}
I'm not getting any errors from PHP or MariaDB, but the code doesn't DROP the table(It exists in phpmyadmin).
Any alternate or more efficient methods are also welcome.
UPDATE : The above code works perfectly fine now, kindly see the edit logs for better understanding about the issue I faced.
ID is int type! Dont place it in quotes. Right method is:
$sql2="DELETE FROM machine_details WHERE M_ID=".$delmid;
Try with Following Code to Delete.
For Delete you can use this code.
<?php
//delete.php
include("connection.php");
if(isset($_POST["_id"]))
{
$query = "DELETE FROM app WHERE _id = '".$_POST["_id"]."'";
if(mysqli_query($connection, $query))
{
echo 'Data Deleted';
}
}
?>
Note: Don't Delete any user data from the table, because if you need any history or detail report you can use this, for this you can put one flag.
Just remove the backquotes(`)
With its use M_ID is being converted to String type. Which should not happen... Because the primary key is int in your case
DELETE FROM machine_details WHERE M_ID=".$delmid;
I am working on an existing HTML form used to collect data about a project and then inserts that project record into a MySQL database using PHP.
Inside the form, there is an input field named "staff[]". This field is a multi select element, that allows users to select more than one team member to handle the project.
<form action="" method="post">
<select multiple name="staff[]">
<option value="1">Mary</option>
<option value="2">Tyrone</option>
<option value="3">Rod</option>
<option value="4">Marcus</option>
<option value="5">David</option>
</select>
</form>
For example purposes, the user selects Tyrone, Rod and David for this particular project. If we insert the record at this point, the database only stores the first record value, which would be Tyrone's ID of 2. General practice is to store each instance in a separate table, however this is not our system and due to a restriction of 4 members for each project, management would prefer we insert a comma delimited array into each project's staff column for convenience.
In order to handle this issue, we've created a foreach loop that loops through the selected values from the dropdown menu, while ensuring a trailing comma doesn't exist:
// Add array into one variable
$staff_count = count($_POST['project_staff']);
$i = 0;
foreach($_POST['project_staff'] as $staff) {
if (++$i === $staff_count) {
$member_variable .= $staff;
} else {
$member_variable .= $staff . ", ";
}
}
After pressing the submit button, the above script is ran (which produces an array value of (2, 3, 5)) and the record is inserted into the 'projects' table with no issues.
HEREIN LIES THE PROBLEM.
Finally we have a view page, where we will call all employees assigned to a project, based on the query parameter, which would be the project ID. For example, if the previous project ID was 6, the following URL would be used:
site.com/project/view/?project=6
From this page, I am able to save the staff list using the following variable assignment:
$project = "SELECT * FROM projects WHERE project = 6";
$employee_chosen = $project['project_employee']
If the 'staff' column only accepted one employee (for example, just one value of 4), the variable would have a value of one number:
$project['project_employee'] (4)
I would then be able to run a secondary query for employees as such:
$employee_chosen = $project['project_employee']; (4)
query2 = "SELECT * FROM employees WHERE employee_ID = $employee_chosen";
This would very easily bring back the one employee that was entered in the "staff" column. However, we are dealing with an array in this column value (2, 3, 5) and so I have queried the following statement:
$employee_list = $project['project_staff']; (2,3,5)
$query_employees = "SELECT * FROM employees WHERE employee_id IN ($employee_list)";
When I run this query, I receive only the first result from the employee ID 2 (as initially stated with the HTML form).
However, if I use phpMyAdmin to directly type in the three numbers as a string:
$query_employees = "SELECT * FROM employees WHERE employee_id IN (2,3,5)";
I receive all three employee records.
Just to ensure that the column ARRAY was in fact behaving as a STRING, I initiated a var_dump on the value:
echo var_dump($project['project_staff']);
After which I received the following information:
string(7) "4, 5, 6"
Does anyone have any ideas?
I am satisfied with the idea that I am able to query the value, as before I received several non-object and array errors.
Thanks in advance for any assistance you may be able to provide.
I'm pretty sure from what you are saying that you are storing a string $employee_list that might be '2,3,4'. Then your IN ($employee_list) is really IN ('2,3,4') but what you really want is IN (2,3,4). There are various ways to get there but you could do
$employee_list = implode(','(explode(',', $employee_list));
I am trying to write a value to a database. Everything seems to be fine except one value is mysteriously incorrect. I just can't figure this out.
Using codeigniter here is my controller:
$sample_id = $this->input->post('sample_id');
$culture_id = $this->input->post('culture_id');
$sample_name = $this->vial_model->get_name($culture_id);
$box_id = $this->input->post('boxid');
$db_data['boxid'] = $box_id;
$db_data['taskid'] = $sample_id;
$db_data['projectid'] = $culture_id;
$vial_id = $this->vial_model->create($db_data);
$vial_link = '<a href="'.base_url('freezer_vial/view/'.$culture_id.'/'.$sample_id.'/'.$vial_id).'" >'.$sample_name.'</a>';
Lets imagine that the value for $sample_id is 158 (or any number really) and that I can echo this value to the view to confirm.
The anchor link that is output to the view is as expected and contains 158. However, the value for $db_data['taskid'] is always 127 and is inserted into the database as this. I have no idea why. Everything else works fine.
Here is the model:
public function create($data)
{
$insert = $this->db->insert('vial', $data);
if($insert)
return $this->db->insert_id();
else
return false;
}
The column in your database table holding the sample id value might have been defined as a tinyint. See also https://stackoverflow.com/a/16684301/282073.
You might want to alter the column type to ensure no data is altered for each new insertion or update. Instances can be found in another answer https://stackoverflow.com/a/13772308/282073
Official documentation to alter tables can be read from
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
I am trying to implement an action that can get me the last inserted id before I insert a record.
The action is basically supposed to get the last inserted id, then i add 1 to it then the value will be used in the current data been inserted.
This how far I have gotten and the error am getting
//the action to get the last inserted id
public function getLastID(){
$lastcourseid = $this->tableGateway->select(function (Select $select){
$select->columns(array('id'));
$select->order('id ASC')->limit(1);
});
var_dump($lastcourseid);
return $lastcourseid;
}
I call the function here before saving
if($id == 0){
$data['course_code'] = $this->getLastID();
$this->tableGateway->insert($data);
}else{
if($this->getAlbum($id)){
$this->tableGateway->update($data, array('id' => $id));
}else{
throw new \Exception("Form id does not exist");//an error is thrown in case the id is not found
}
}
This is the error am getting
Catchable fatal error: Object of class Zend\Db\ResultSet\ResultSet
could not be converted to string
I do not know where am going wrong.
Any help will be appreciated. Thanks
There is no such thing like "last id before insert".
And you don't need it.
First insert a record and then get your id. This is how it works.
You do not mention the underlying database (postgressql, mysql, etc.). I am more familiar with MySQL. The Perl and php AIP's have "last row id" functions. For example, php has "mysqli_insert_id()". This assumes that your rowID is an AUTO_INCREMENT column. Other DBs may have different requirements.
I have a form that lists module id and also sub_module_id, for example
ADMIN === is parent module ID
users=== sub module id
here admin appears in the menu and users inside admin
now inside the form i have used checkbox like
[]Admin
[]Users
for parent module ID admin
<input id='module_permission[]' onclick=\"selectall()\" type='checkbox' value='".$key."' name='module_permission[]' $val_checked ><b>".$val."</b><br> ";
for sub modules
<input type='checkbox' id='sub_module_permission[$l][]' name='sub_module_permission[$l][]' value='".$key1 ."' onclick=\"selectParent($l);\" $val_checked>".$val1."<br> ";
when i click in check box its id get post and i need to insert to databse but i am unabale to insert the sub _module id in database
to post
$module_id=$_post[module_permission]
foreach($module_id as $key=>$value){
$sql2 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id) values
(PERMISSION_ID_seq.nextval,'$employee_cd','$value')";
$this->db->db_query($sql2);
}
for sub _modules
$sub_module_id =$_POST['sub_module_permission'];
print_r($sub_module_id);
foreach($sub_module_id as $sub_key=>$sub_value)
{
echo $sub_value[1];
$sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id) values
(PERMISSION_ID_seq.nextval,'$employee_cd','$sub_value')";
HERE parent module id value get inserted in database but not the sub_module
please help
so what prints out when you print_r($sub_module_id); and echo $sub_value[1];?
And what does your query string look like after the vars are subbed in? echo $sql4;
Try running the the query string directly in SQL rather than through PHP. This will let you know if you have a SQL error or a PHP error. If the SQL is fine, add some error checking to your PHP so you can see where it fails. Usually I wrap all queries in something like:
if(!$result=$mysqli->query($query))
throw new Exception($query. " " .$mysqli->error);
From your comments and the now properly formatted code, it's possible to see that you are not referring to the 2nd dimension of your submoduleid. In otherwords you are trying to sub an array into your SQL statement.
This is what you have:
$sub_module_id =$_POST['sub_module_permission']; //a 2d array
print_r($sub_module_id);
foreach($sub_module_id as $sub_key=>$sub_value){
echo $sub_value[1];
$sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id)
values(PERMISSION_ID_seq.nextval,'$employee_cd','$sub_value')";
}
And this is what you need:
$sub_module_id =$_POST['sub_module_permission']; //a 2d array
print_r($sub_module_id);
foreach($sub_module_id as $sub_key=>$sub_value_arr){ //values are an array not a scalar
echo $sub_value_arr[1];
$sub_value= $sub_value_arr[1]; //get scalar for SQL
$sql4 = "INSERT INTO user_permissions(permission_id, employee_cd,module_id)
values(PERMISSION_ID_seq.nextval,'$employee_cd','$sub_value')";
}
Unrelated, but if you're getting that input from a form (and really, even if you're not), it's always a good idea to sanitize your SQL.