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.
Related
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);
}
I know it is a frequently asked question, but let me explain a bit why I need to ask that again.
Most answers out there suggests using
$this->db->affected_rows()
This is not working in my situation as I may have situation that the update data is the same as the data in the database, so it will return 0 even if the update is success.
I know I can write another query to check if it is because the data are the same, but this sounds stupid to me.
Another way is to use the Active Record Class in code igniter, so the line will be something like:
$error_num = $this->db->update("users", array('firstName' => $newFirstName));
return $error_num;
which is a better solution (at least it works even if there is no update due to repeated data). However, I would like to use the database class, i.e. something like:
$this->db->query('update table set field = somedata where id=1');
It increases the readability of my code to coder who knows php + SQL but are not familiar with code igniter, but then this is why I am asking if anybody knows how do I get the error number if I query in this way?
i.e. Something like mysql_errno() when using mysql_query($query) in PHP.
Both will work like same only difference
if you using active record class all the input variables are escaped automatically
If you want security you better to go with active record class
If you use normal query execution you have to escape the input parameters manually using $this->db->escape_str()
$id = $this->db->escape_str($id)
You can check the query execution status.
$status = $this->db->update("users", array('firstName' => $newFirstName));
if($status)
{
//here you can check the number of affected rows if required
echo $this->db->affected_rows();
}else{
//here you can prcess for failure case
log_message('Error', $this->db->_error_message());
}
//return the status to required place
return $status
I have use this code for checking.
$status = $this->db->query('update table set field = somedata where id=1');
if($status)
return true;
else
return false;
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.
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.
Thanks for asking me to make my question clearer.
I have a form which is used to save content in a database.
The user is also allowed to update any content by editing the fields in the form and submitting it.
The update query looks something like this:-
$res = mysql_query("UPDATE freight SET .... WHERE id='$id'") or die ( mysql_error());
if(mysql_affected_rows() > 0){
//do some stuff
return true;
}
else{
return false;
}
Now my problem is this: If the user changes some field while updating the form, mysql_affected_rows() is > 0 so I am able to "//do some stuff".
But if the user does not change any field in the form and simply submits, mysql_affected_rows return 0 and I reach the false block.
I was under the impression that UPDATE should overwrite every time thereby mysql_affected_rows() should return 1 irrespective of whether any fields have been changed or not.
I use the return value to display an appropriate success or error message in the screen. Hence this is a serious bug.
Can you suggest whats the right way to achieve the correct functionality?
Thanks,
Vish
mysql_affected rows() -1 will be returned if the query itself can not be issued to the server, possibly because of syntax error AND if the last query was not either an INSERT or UPDATE statement.
When using UPDATE, MySQL will not update columns where the new value
is the same as the old value. This creates the possibility that
mysql_affected_rows() may not actually equal the number of rows
matched, only the number of rows that were literally affected by the
query.
so your solution can be achieved by below logic ( but this is not safe)
if(mysql_affected_rows()!=-1){
}
if( $_POST['message'] != '' ) {
// add the field to your query
} else { // this is optional
// don't add it to your query
}
// do the mysql update here