Codeigniter unknown column in where clause? - php

I am trying to create an update function in Codeigniter using a dropdown menu. Unfortunately, when I click submit I get an error that there is an "Unknown column 'stages.id' in my where clause. I've used the stages.id in other parts of my code to update other fields, and am not sure why this is happening here. Any assistance or thoughts on why this might be happening would be greatly appreciated!
function update_stage_name($id){
$stage_name=array(
'stage_name'=>$this->input->post('stage_name')
);
$this->db->where('stages.id', $id);
$this->db->update('stage_names', $stage_name);
}
This is what the database query looks like according to the error:
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stages`.`id` = '264'
I have several tables, and stages is sort of the main one. Stages each only have one stage_name but a stage_name can belong to multiple stages. Stage_name does not have a foreign key from stages, but stages has a stage_name_id.
EDIT: So it seems the issue was actually that I wanted to update the stages table, and not the stage_names table. But the problem is that the stages table has a stage_name_id but the input from my form is for stage_name. How do I get to the id in the same method? I'm still new to active record and codeigniter and i'm not sure how to proceed.

As the error says you dont have a column that can be referenced using stages.id. Mainly because its the wrong table..
Try..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_names`.`id` = '264'
//Note changed the table name in the where
If you need to use the stages.id and (as you say in your comment) you dont have an FK then you will need to create one as the database isnt smart enough to guess it. So once you have a field in stages_names called stage_id (or similar) you can do..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_id` = '264'

Try this:
function update_stage_name($id) {
$stage_name=array(
'stage_name'=> $this->input->post('stage_name')
);
$this->db->where('stage_names.id', (int)$id);
$this->db->update('stage_names', $stage_name);
}

Related

Update ENUM field in MySQL

I have a strange and incomprehensible situation. I can't update an ENUM field with my query in my php script. Executed in Heidi, it works perfectly. Can somebody help me to understand why?
This is the query:
UPDATE ps_specific_price
SET reduction='0.5', reduction_type='percentage'
WHERE id_product='249' and id_group='3'
The enum field is reduction_type (amount, percentage)
Here is the code for the operation:
if($valori['perc_riv']==0){
$sql_perc_riv="update ps_specific_price set reduction='0', reduction_type='amount' where id_product='".$valori[id_product]."' and id_group='3'";
} else {
$riduzione=(float)$valori['perc_riv']/100;
$sql_perc_riv="update ps_specific_price set reduction_type='percentage' WHERE id_specific_price='2031'"; // where id_product='".$valori[id_product]."' and id_group='3'";
}
$sttpercriv = $conn->prepare($sql_perc_riv);
$sttpercriv->execute();
Ok, I read better the documentation: I have to make an Alter table. I dont' want. I'll make a select, then I'll delete the row and I'll make a new insert with the different values.

$this->Agreements->save() - create only one record, why?

I need to read all records from table agreements, make changes in filed payments, and update all records, save to table. So, my problem is, that save() only create empty record. Do not update exists record. I show you how:
Reading from table:
$agreements = $this->Agreement->find('all');
$payments = $this->Payment->find('all');
Manipulation on fields (part of)(example):
$id=0;
foreach ($agreements as $agreement):
for ($i=$first_agreement; $i<=$last_agreement; $i++){
if ( $agreement['Agreement']['agreement_number']==$i){
$agreements[$id]['Agreement']['payment']=$payd[$i];
}
}
$id++;
endforeach;
Writting to table:
$this->Agreement->save();
A echo debug($agreements) shows correct array, i have tryed also :
$this->Agreement->save($agreements);
or
$this->Agreement->save($this->request->data);
Can you help/explain me how to write all record?
Cake 2.5.2 PHP : 5.4.4-14
Model::save() only saves a single record. If you want to save multiple records you need to use Model::saveAll() or Model::saveMany().

Add another data coloumn from arduino sketch into mysql database

I found someone do it on wireless from github. I want to insert another column using arduino via ethernet shield to php apache server into mysql database. In the arduino sketch, I add another coloumndata as shown below:
String yourdatacolumn1="yourdata1=";
String yourdata1;
int yourarduinodata1 = 55555555;
yourdata1 = yourdatacolumn1 + yourarduinodata1;
In the mysql table, I have a column call yourdata1. and in the insert_sql.php. I have modified the code a little bit. But when I run the sketch. On php side, no data is recorded. If I get rid of the new column yourdata1. that works. But when inserting another column , it doesn't work. (I have also created column into mysql called yourdata1)
foreach ($_REQUEST as $key => $value)
{
if ($key == "yourdata") {
$yourdata = $value;
}
if ($key =="yourdata1"){
$yourdata1=$value;
}
}
There are three steps to achieve what you want:
Create a column named yourdata1 in your mysql table: (You have already done as per your question but I am reiterating)I am guessing you followed the steps mentioned in the README of this particular github project to create the columns: yourdata and timestamp. For your problem, you need to create another column, yourdata1 similarly. Or alternatively you can login to the mysql client and issue the following command:
ALTER TABLE your-table-name ADD COLUMN yourdata1 varchar(100)
Edit your insert_mysql.php to start adding data to your new column: Apart from the edit you have shown in the question, you also need to edit the line 23 of the script which inserts data into the table. The syntax for doing so can be found here.I am mentioning it here so that you could use it
INSERT INTO your-table-name (yourdata,yourdata1,timestamp) VALUES ($yourdata,$yourdata1,now());
(Make sure that both $yourdata and $yourdata1 php variables exist, you can initialize both of them as empty, $yourdata='' and $yourdata1='' just before the for loop)
You need to make sure that arduino passes this new yourdata1 to your insert_php script. I guess you have already figured it out. You just need to add another client.println() statement.

codeigniter duplicate in table

i am having a issue with codeigniter form validation.
my table is as
Sr#, name , dob,pic
$this->form_validation->set_rules('name','Duplicate Name','trim|required|is_unique[mcb.name]');
now when i am trying to edit any record with sr# and check the form validation for name (as i dont want duplicate name too) it gives error.
what i am trying to do is update the record but i don't want duplicate name too.
now for example if i edit the record and don't change name but change
DOB
but it shows the duplicate name.
i want to check duplicate name but not in row i am going to update.
Thanks
Solution is kind of easy I think (if I got your question right)
make two sets of rules
$this->form_validation->set_rules('name','Duplicate Name','trim|required|is_unique[mcb.name]');
$this->form_validation->set_rules('name','Duplicate Name','trim|required');
make a if() before using validation->run()
like this
if ( strtoupper($this->input->post('name')) == strtoupper($old_name) ) { //pass old name in hidden field or load it before this condition via model
$this->form_validation->set_rules('name','Duplicate Name','trim|strtoupper|required');
} else {
$this->form_validation->set_rules('name','Duplicate','trim|required|strtoupper|is_unique[mcb.name]');
}
//all other form_validation checks here
if ($this->form_validation->run()) {}
//edit
added strtoupper() so your unique values are really unique

How to delete and update data in MySQL at the same time

Basically what I am trying to do is when I delete a client with an ID of lets say 6 and I have 50 clients, I then want to update the client with the ID of 50 to 6.
This is my code, in PHP, but it won't execute 2 mysql_query-s at the same time at least I think that's the problem. Otherwise the SQL syntax works fine.
public function delete () {
$last=$this->numrow; //contains last ID works fine
if (isset ($_GET['x'])) {
mysql_query('DELETE FROM proba WHERE ID ='.$_GET['x']);
mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].'WHERE ID='.(int)$last);
}
}
The $_GET['x'] contains the ID on which it was clicked . But only the first mysql_query gets executed how do i make it so the second one gets executed also ?
And another question is is it possible to get <a href="munka/index.php?x=5" > [-] </a> the x=5 with a $_POST ?
You might save yourself a lot of trouble by using mysql's replace query:
see http://dev.mysql.com/doc/refman/5.6/en/replace.html
for details.
most probably you are facing a php error on the first query. Check the php error log.
for the second question $_GET is used to take parameters from the URL for example
munka/index.php?x=5
$_POST is used to get parameters posted on http post (usually on form submits).
just change the update query with a space before the where clause
mysql_query('UPDATE proba SET ID='.(int)$_GET['x'].' WHERE ID='.(int)$last);
Better to use transactions support by using InnoDB Mysql DB Engine, so both delete and update execute together wuth COMMIT without miss , and in case anything goes wrong your delete changes get ROLLBACK
if (isset($_GET['x'])) {
mysql_query('DELETE FROM proba WHERE ID =' . $_GET['x']);
mysql_query('ALTER TABLE `proba` DROP `ID`;');
mysql_query('ALTER TABLE `proba` ADD `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');
}
Try in Phpmyadmin delete record no of 5 and drop id column and recreate id column.
It is work.

Categories