AES_ENCRYPT with MYSQL UPDATE - php

I am trying to do a basic mysql update but using AES_ENCRYPT - can anyone explain why i'm getting an error message? Below is the query :-
UPDATE MailList
SET Email = AES_ENCRYPT( arandomemail#hotmail.com, 'jkfdsfsaKJjdsf' )
WHERE ID = '138142'
I get the following error:
#1064 - 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 '#hotmail.co.uk,'jkaKJjkH87') WHERE MailListID = '138142'' at line 1

UPDATE MailList
SET Email = AES_ENCRYPT( 'arandomemail#hotmail.com', 'jkfdsfsaKJjdsf' )
WHERE ID = '138142'

You forgot the ' around your mail you want to encrypt.
UPDATE MailList
SET Email = AES_ENCRYPT( 'arandomemail#hotmail.com', 'jkfdsfsaKJjdsf' )
WHERE ID = 138142

You need to put the email address, arandomemail#hotmail.com, in quotes.
UPDATE MailList
SET Email = AES_ENCRYPT( 'arandomemail#hotmail.com', 'jkfdsfsaKJjdsf' )
WHERE ID = '138142'

Related

Using mysql user defined variables with php

I have an SQL statement which runs perfectly when used on phpmyadmin,
$q1 = " SET #pos = 0; UPDATE `songs` SET `tweek` = ( #pos:= #pos+1) WHERE `approved` = 1 ORDER BY votes DESC ";
my connection is fine, every other thing is fine but I keep getting an error when I use it in my php code.
mysql_query($q1, $link) or die(mysql_error());
Error:
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 'UPDATE `songs` SET `tweek` = 1 WHERE `approved` = 1 ORDER BY
votes DESC' at line 1
Please help.
SET and UPDATE are two queries and you must split it into two different calls of mysql_query

Updata mysql table using a form with variable for unique table

I have a form which i fill in a number and then the right database is updated. this is not working!
The variable gets posted (i had inserted 3 you see this in the error but its not updating)
i get this error? Error updating record: 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 ''3'_xyz_ips_short_code set content = replace(content,'eten','string_to_replace' at line 1
<textarea name="dbnaamsite" placeholder="3" cols="10" rows="1"></textarea>
$dbnaamsite = $_POST['dbnaamsite'];
$sql = "update wp_'$dbnaamsite'_xyz_ips_short_code
set content = replace(content,'eten','string_to_replace')";
//mysql_real_escape_string($formPostTitle);
Try it
$sql = "update `wp_".$dbnaamsite."_xyz_íp_short_code` set content = replace(content,'eten','string_to_replace')";

how to update columns with empty value

Im using mysql, when I want to update a column with this query
UPDATE books
SET ISBN = $ISBN
, Title = '$BookTitle'
, PublicationDate = '$PublicationDate'
, Publisher = '$Publisher'
, Edition = $Edition
, Volume = $Volume
, books.Author_AuthorId = $AuthorId
WHERE ISBN = $GETISBN;
with php it works well while all the input are filled in HTML FORM but if one input is empty already or I clear the previous data in HTML Form and Submit the Form it issues this error
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 'Volume=1, books.Author_AuthorId=3 WHERE ISBN=5456165156' at line 2
What Should I do?
Here is the query which I have echoed
UPDATE `books` SET `ISBN`=5456165156,`Title`='500 Notice About Java',`PublicationDate`='1390-05-25', `Publisher`='Qods Publication',`Edition`=,`Volume`=1, `books`.`Author_AuthorId`=3 WHERE `ISBN`=5456165156
As long as The $Edition is empty Then The Query Changes like this
`Edition`=
Thats why Mysql can not Understand what value should be set to Edition

Prevent duplicates being added to database table via PHP form

I want to log what a user enters into a PHP form, and make sure they are not entering data that already exists in a database table.
I have the code already that enters the data into the table from user input, but I'm not sure how to check for duplicates. For example I want to check that there is no product under the same name being added again.
$sql = "
INSERT INTO user_date
SELECT
product_name = '$_POST[product_name]'
,code = '$_POST[code]'
,comments = '$_POST[comments]'
WHERE
NOT EXISTS(SELECT * FROM user_data WHERE product_name = '$_POST[product_name]') ";
But I get an error:
Error: 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 '= 'fdgfdg' code = 'fdgdfg' WHERE NOT EXISTS(SELECT *' at line 4
I'm aware of the security issues. Its not a live system but just to learn from it.
If you don't want to have duplicate insert then use IGNORE at end of insert statement
$sql = "
INSERT INTO user_date
values
('$_POST[product_name]'
,'$_POST[code]'
,'$_POST[comments]')
ON DUPLICATE KEY IGNORE";
So this way might help you
$result = mysql_query("SELECT * FROM user_data WHERE product_name = '$_POST[product_name]'");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
// do something
}
else {
// do something else
}

PHP/MySQL Concat to a single column and Update other columns in table

Am trying to only concat new updates to column updates and UPDATE the values in the rest of the columns but I've hit bit of a snag that I can't seem to workout.
My SQL looks like this:
$query="Update tickets SET product='$product',
p='$p',
i='$i',
summary='$summary',
workaround='$workaround',
concat(updates,'$additional_update'),
status='$status',
raised_by='$raised_by',
updated_by_user='$updated_by' WHERE id='$id'";
the updates column is like a comments column, where new updates are meant to be appended to the existing text.
The error I'm getting on the web server:
Update tickets SET product='T-Box', p='00000817766', i='-', summary='Testing update field
\r\nAdding an update\r\ntesting if null works for update', workaround='n/a', concat(updates,' ','test2#18:53:17:second update/n'), status='Open', raised_by='No', updated_by_user='test2' WHERE id='223'
Running the query directly in MySQL:
#1064 - 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 '(updates,'test2#18:53:17:second update/n'), status='Open', raised_by='No', updat' at line 1
Help is much appreciated!
You need to specify where the value of this statement concat(updates,'$additional_update') to be set.
Update tickets
SET product = '$product',
p = '$p',
i = '$i',
summary = '$summary',
workaround = '$workaround',
updates = CONCAT(updates,'$additional_update'), // <== see this
status = '$status',
raised_by = '$raised_by',
updated_by_user = '$updated_by'
WHERE id = '$id'
try this:
$query="Update tickets SET product='$product',
p='$p',
i='$i',
summary='$summary',
workaround='$workaround',
updates=concat(updates,'$additional_update'),
status='$status',
raised_by='$raised_by',
updated_by_user='$updated_by' WHERE id='$id'";

Categories