Updating the table by multiple where conditions in codeigniter - php

I have to update the table if only the three values gets matched. I get a syntax error on trying this. What could be the reason?
$sql1="update target set updvalue='".$val3."' where Id ='".$b."'" AND MId ='".$c."'" AND DID ='".$f."'";
$res1=$this->db->query($sql1);

$sql1="update target set updvalue='".$val3."' where Id ='".$b."' AND MId ='".$c."' AND DID ='".$f."'";
$res1=$this->db->query($sql1);
use this

You can use Codeigniter query builder.
$this->db->where('Id',$b);
$this->db->where('MId',$c);
$this->db->where('DID',$f);
$upd_data['updvalue'] = $val3;
$res1=$this->db->update('target',$upd_data);
Let me know if it not works.

I think there are to many " between where Id and AND, that is, you have made a typo.

Related

Execute 2 mysql Queries with the second being based on the first

Hi I have two tables that I need to insert into.
the issue is that the first table has an ID field that is automatically generated and I need this field in the second query
members (table1):
|id|name|eyeColour|
assignedMembers (table 2):
|id|memberID|groupID|
I am currently using the below:
$addMember = $dbHandle->prepare("INSERT INTO members(name,date) VALUES(?,?)");
$addMember->bind_param("ss",$name,$eyeColour);
$addMember->execute();
$getID = $dbHandle->("SELECT id from members where name = ? LIMIT 1");
$getID->bind_param("s",$name);
$getID->execute();
$getID->bind_param($MID);
$assignMember= $dbHandle->prepare("INSERT INTO assignedMembers memberID,groupID) VALUES(?,4)");
$assignMember->bind_param("i",$MID);
$assignMember->execute();
This fails at the $assignMember->bind_param(); after troubleshooting I noticed that the $MID variable is empty.
it seems as though the row from the first INSERT is not added before the execution of the next statement is there a way to force this?
Thank you for taking the time to read this post, any help would be greatly appreciated
mysqli:$insert_id is what you are looking for.
$addMember = $dbHandle->prepare("INSERT INTO members(name,date) VALUES(?,?)");
$addMember->bind_param("ss",$name,$eyeColour);
$addMember->execute();
$id = $dbHandle->insert_id;
I think you should use
$getID->bind_result($MID);
$getID->fetch();
instead of
$getID->bind_param($MID);
Due to usage of '...->bind_param' I assume, you use MySQLi.
Check out: mysqli_insert_id — Get the ID generated in the last query

Php syntax, database request with user input component

I need some help with the php syntax for when user input is a variable in the request to the database.
The below manually enters values:
"UPDATE customer SET first_name= 'Me2' WHERE id = ' 13 ' ";
However i want the user to be able to enter values such as this:
"UPDATE customer SET first_name=".$edit_first_name.", WHERE id=".$edit_id."\"";
When i run the above it doesn't work as the first example script does.
An i'm assuming it's a syntax problem, an there is no display on the page to indicate the issue location. Please help
Your query is wrong. Don't insert , before WHERE & put single quotes around string value.
"UPDATE customer SET first_name='".$edit_first_name."' WHERE id=".$edit_id;
This is the final query:
$update="UPDATE customer SET first_name=".$edit_first_name." WHERE id=".$edit_id;
You miss simple quotes '' around your $edit_first_name as it is a string.
try this statement, maybe it could solve your problem:
"UPDATE customer SET first_name ='$edit_first_name' WHERE id='$edit_id'";

mysql query does not work on different files -php

i might be doing some idiot mistake, but i could not figure that out. i have some values coming from html and wanna insert into mysql db. problem is, the very same query does not work in regular php file (that includes other queries), but when i try on an independent php file, it does. here is a sample of the code:
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '".$_POST['Article_Title']."') ";
mysql_query($sql15);
as i mentioned, the very same code works when i just copy this snippet to a new php file, and it works smoothly.. as you see, there are 20+ insert with the same php, because there are 25+ tables, but data is not much. first 14 query and following 7 queries do work by the way.
do you have any ideas?
There are some things to check and do.
Sanitize user input:
"('$article_id', '".mysql_real_escape_string($_POST['Article_Title'])."')";
You might also want to check if the value is what you expect.
Is your $article_id correct for column Article_ID?
Are your table and column names correct?
Check for errors:
$res = mysql_query($sql15);
if (!$res)
echo mysql_errno($link) . ": " . mysql_error($link);
Show us you complete query:
echo $sql15;
First of all i would suggest you to write your insert query like below
$sql15="insert into body SET Article_ID = '$article_id', Article_Title = '".$_POST['Article_Title']."'";
echo $sql15;
mysql_query($sql15);
so that each time when you add new column to database it would be easy for u to change insert query. echo your query and see it in browser. in it seems to o.k then copy it and paste it in SQL section under your phpmyadmin (see you are choosing proper database) and run it. if one row inserted successfully then your query is alright.
I hope this would help you a little.
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '".$_POST['Article_Title']."') ";
mysql_query($sql15) or die(mysql_error());
use like this u will be get the error. then u will be find the issue
I think using mysql_real_escape_string may solve your problem.I also recommend you to store your form data in a string.
$article_title= mysql_real_escape_string($_POST['Article_Title']);
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '$article_title') ";
mysql_query($sql15) or die(mysql_error());

UPDATE in mysql didn't work in my PHP page.

First of all I'm a rookie to Programming, I created a PHP page to update a value from my mysql(myadmin) database, but the value is not updating. I also tried to retrieve values from database it's working just fine but this UPDATE code is not working! I don't know why, please check out my code below.
$qs=mysql_query("update staff set review=$newrate where name=$rateuser");
$resu=mysql_query($qs);
All variables are double defined, assigned with proper values, checked and I tested variables using echo, table name is also checked, it's all fine, but I think the problem is with Update query, I searched internet for the syntax but it's not different than mine. Please help me out
How are $newrate and $rateuser set?
mysql_query("UPDATE staff SET review = '".mysql_real_escape_string($newrate)."' WHERE name = '".mysql_real_escape_string($rateuser) ."'");
http://php.net/manual/en/function.mysql-real-escape-string.php
Try:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
Do not use second line.
You probably just need some " around your values $newrate and $rateuser
But if you did an echo, why not actually echo for us what the query-string becomes?
You need single quotes around string values on your query:
$qs=mysql_query("update staff set review='$newrate' where name='$rateuser'");
(assuming both variables are strings)

Update MYSQL Query while using existing content

I have a mysql table with a row called "log". I need to update that log while using the content that was already in the file.
mysql_query('UPDATE messages SET log=log+"123" WHERE uid = "'.$config["user_id"].'"');
That one doesn't work :/ I think it's because it's a TEXT field, with INT i can normally do
SET row=row+1
I'm sure there is a clever solution for this, thanks in advance for all hints.
update messages set log = concat(log, "123") where...
You cannot use the + operator on strings, there is a function for that in mysql and is called concat.
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
You will maybe need concat_ws to add a space or newline in between :)
mysql_query('UPDATE messages SET log=log+123 WHERE uid = "'.intval($config["user_id"]).'"');

Categories