just having some trouble with an SQL update in PHP. Listed below is an extract of the function:
$captain = $this->getUserName();
$member = $textParts[1];
$memberNo = 'member1';
$sqlUpdate = 'UPDATE ajax_chat_draft_teams SET '.$memberNo.'='.$member.' WHERE captain='.$captain.'';
$result = $this->db->sqlQuery($sqlUpdate);
When the Query is reached it throws the following error:
Query: UPDATE ajax_chat_draft_teams SET member1=user WHERE captain=Oolius
Error-Report: Unknown column 'Oolius' in 'where clause'
Error-Code: 1054 error occured!
The table ajax_chat_draft_teams has 5 fields: captain, member1, member2, member3, member4
(Note: There is a record in the table where the captain is Oolius and all members are NULL).
I'm failing to see what is wrong with my SQL statement. Thanks for your time.
Try this:
$sqlUpdate = 'UPDATE ajax_chat_draft_teams SET '.$memberNo.' = "'.$member.'" WHERE captain = "'.$captain.'"';
String literals need to be surrounded in single quotes. The query should look like this:
UPDATE ajax_chat_draft_teams SET member1='user' WHERE captain = 'Oolius'
Also, consider using PDO and bind variables.
You need to put Oolius in quotes other MySQL thinks it is a column name.
Use this
$sqlUpdate = 'UPDATE ajax_chat_draft_teams SET
'.$memberNo.'="'.$member.'" WHERE captain="'.$captain.'"';
i hope it will help you.
Related
UPDATE dondathang
SET noigiaohang=diachi
FROM khachhang
WHERE dondathang.makhachhang=khachhang.makhachhang AND noigiaohang IS NULL;
I have a SQL code like above, but this is the error.
syntax error 'from' identifier is not valid input at this position.
What's wrong with my code?
FROM clause is irrelevant in UPDATE sintax. Please take a look at the documentation.
FROM Clause is not a part of UPDATE syntax, you can change the UPDATE as below
UPDATE dondathang
SET noigiaohang=diachi
JOIN khachhang ON dondathang.makhachhang=khachhang.makhachhang
WHERE noigiaohang IS NULL;
The correct syntax in MySQL is:
UPDATE dondathang d JOIN
khachhang k
ON d.makhachhang = k.makhachhang
SET d.noigiaohang = k.diachi
WHERE d.noigiaohang IS NULL;
Can anyone tell me why this doesnt work, and returns syntax error please?
$sql4 = "update apiStreetCheckGeneral
set (BBAverageSpeed, BBSuperFastBBAvailable, BBCommentary)
values ('$averagespeed', '$superfast', '$bbcommentary')
where PostCode='".$values['PostCode']."'";
CustomQuery($sql4);
This is your query:
update apiStreetCheckGeneral set (BBAverageSpeed, BBSuperFastBBAvailable, BBCommentary)
values ('$averagespeed', '$superfast', '$bbcommentary')
where PostCode='".$values['PostCode'].
I am not aware of update syntax that uses a column list or values. Set each one individually:
update apiStreetCheckGeneral
set BBAverageSpeed = '$averagespeed',
BBSuperFastBBAvailable = '$superfast',
BBCommentary = '$bbcommentary'
where PostCode='".$values['PostCode']."'"
You should learn, however, to use parameterized queries especially for update statements.
Use query like below
$sql4 = "update apiStreetCheckGeneral
set BBAverageSpeed = '$averagespeed',
BBSuperFastBBAvailable = '$superfast',
BBCommentary = '$bbcommentary'
where PostCode='".$values['PostCode']."' ";
CustomQuery($sql4);
Hey I'm tring the following sql query :
$sql = mysql_query("INSERT INTO feeds (FileLocation,Title,feeddate,nameofuploader,type)
VALUES('".mysql_real_escape_string($putItAt)."','".mysql_real_escape_string($_POST['title'])." ',now(),". $_SESSION['name'] .",'file')")
but its giving me the error:
Unknown column 'Ankit2' in 'field list'
where Ankit2 is the value to be inserted
Any way around this?
You forgot to put single quotes around the $_SESSION variable!
$sql = mysql_query("INSERT INTO feeds (FileLocation,Title,feeddate,nameofuploader,type)
VALUES('".mysql_real_escape_string($putItAt)."','".mysql_real_escape_string($_POST['title'])." ',now(),'". $_SESSION['name'] ."','file')")
Is the sql string right? Please try this to check your sql string:
$sql_str = "INSERT INTO feeds (FileLocation,Title,feeddate,nameofuploader,type) VALUES('".mysql_real_escape_string($putItAt)."','".mysql_real_escape_string($_POST['title'])." ',now(),". $_SESSION['name'] .",'file')";
print $sql_str;
to check the sql_st
I need to insert some data into a mysql database. Db is connected and working.
I am running the following code :
a = sprintf('%s',hashedStr);
sqlQueryStr = 'insert into products (security_code) values (a)'
QueryDB(sqlQueryStr);
I have a database called test and a table named products with 2 fields id and security_code.
When I run this, I get :
Unknown column 'a' in fieldlist ...
Why is this happening ? i dont have and dont need this column ...
Any help ?
Try with:
sqlQueryStr = sprintf('insert into products (security_code) values ("%s")',hashedStr);
QueryDB(sqlQueryStr);
problem is that you are not replacing "a" variable into sql expression
I'm new to CodeIgniter and I get an error I cannot understand.
This is the code that give the error:
$data = array('adr' => $address);
$this->db->where('id', $id);
$this->db->update('domains', $data);
The error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://www.example.com WHERE id = '10'' at line 1
This is the query:
UPDATE `domains` SET `adr` = http://www.example.com WHERE `id` = '10'
If I change this to
UPDATE `domains` SET `adr` = 'http://www.example.com' WHERE `id` = '10'
it works. Why is CodeIgniter creating this erroneous query?
Try escaping the single quotes in the $address variable before you call the update method.
Generally the CodeIgniter will automatically surround the value of $address with a single quote. I do not know why did you get this error message?
Curious, see if it works when you escape the string use $this->db->escape()
$data = array('adr' => $this->db->escape($address));
$this->db->where('id', $id);
$this->db->update('domains', $data);
I have the same problem and codeigniter do not add single qoutes to where clause.
When you enter integer value, sql do not give error but when you put string value (as a variable) to where clause, it gives error. But when you add single quotes to query and run it on phpmyadmin, it works.
So the solution is adding (string) statement to your variable: as in this (string)$id
I wrote before to add single quotes to variable as '$id', but this will not going to work (I'm new to codeigniter&php, thanks to commenter Mitchell McKenna, I checked out what I wrote before)