Simple MySQL UPDATE only affects certain rows [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
SOLVED
Rows 1, 4, and 10 have a field with a single quote character in them, so I was SQL injecting myself. More reason to switch to prepared statements!
I have a very simple bulk UPDATE query sent from my PHP form which, oddly enough, only affects certain rows. Rows 1, 4, and 10 doesn't get updated; but all other rows get updated.
Here is the query:
$query = "SELECT * FROM SkillDescriptions";
$result = mysql_query($query);
for ($i=1; $i<=mysql_num_rows($result); $i++){
$skillKey = 'Skill' . (string)$i;
$categoryKey = 'Category' . (string)$i;
$descriptionKey = 'Description' . (string)$i;
$sql="UPDATE SkillDescriptions SET
Skill='$_POST[$skillKey]',
Category='$_POST[$categoryKey]',
Description='$_POST[$descriptionKey]'
WHERE id='$i'
";
$result2=mysql_query($sql);
}
I've got a parallel form that does the exact same PHP form processing as this one but has a different database table, which works properly: so the problem most likely lies in the database table (configuration?) and not the code.
Why do only certain rows get updated, in a seemingly random pattern?
UPDATE
Before:
http://i.stack.imgur.com/2hk2l.png
After: (I appended test to the columns)
http://i.stack.imgur.com/ObMn5.png

Related

PDO Prepared Statements How to Select From Database Where Id = Value or Tell = Value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to Select one record from a table Named 'sms_students' by using Student Id or Student Telephone
Here is My Code
$student_rand_id=$_GET['std_rand'];
$std_tell=$_GET['std_tell'];
$view_student = $config->prepare("SELECT * FROM sms_students WHERE st_rand = :random_id || st_tel =: st_tel");
$view_student->execute(['random_id' => $student_rand_id]);
$view_student->execute(['st_tel' => $std_tell]);
$row = $view_student->fetch();
Since you call execute twice, this executes twice, both times with an incomplete set of arguments. It's an easy fix though:
$view_student = $config->prepare("SELECT * FROM sms_students WHERE st_rand = :random_id OR st_tel = :st_tel");
$view_student->execute(['random_id' => $_GET['std_rand'], 'st_tel' => $_GET['std_tell'] ]);
$row = $view_student->fetch();
Try and get rid of single-use variables, they're almost always unnecessary, and do try and steer towards having names that match precisely. Seeing st_tel and std_tell together is a sign something's not quite right. Get your code to agree on names and stick with them.

This code is not decreasing the value by 1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to update next table named 'book' but this UPDATE code is not working
"UPDATE books SET Quantity=Quantity-1 Where Book_ID='$Book_ID'";
this code should update the books table by decreasing the value of Quantity by 1
You need to decrease from column value, not PHP variable. Remove the dollar sign and quotes, of you have there integers.
UPDATE books SET Quantity = Quantity - 1 Where Book_ID = '$Book_ID'
Don't forget that you are vulnerable to SQL injection in WHERE clause.

Query doesn't update correctly in mySql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm having problems with a query that should "rank an user to 3" but instead in the MySQL it gives to the user X rank 0.
Here is the code
if($_POST['rank'] == '3')
{
mysql_query("UPDATE users SET rank='3' AND LPT='1' where username='".$_post['u_name']."' LIMIT 1")or die(mysql_error());
$query = true;
}
Waiting for answers
Use comma instead of AND when updating multiple columns:
UPDATE users
SET rank='3',
LPT='1'
where username= ?
You should:
Use parametrized query instead of concatenating SQL string
$_post is a superglobal and must be in uppercase $_POST
Reference: http://php.net/manual/en/language.variables.superglobals.php
Additional reference:
UPDATE http://dev.mysql.com/doc/refman/5.7/en/update.html

Mysql query cant run in mssql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This query runs well in mysql but cannot run in mssql. Why?
Don't judge me, I am new in mssql.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
ORDER BY columnindex DESC
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'order'.
You just need to remove ORDER clause. ORDER is pointless in an UPDATE statement.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
You don't need at all the ORDER BY clause. this is only when SELECTing data from the database.
You may want to note that even though SQL is a language with a specified structure, different SQL engines may parse the query differently and use different syntax since a lot of the SQL functionality is not defined by the language.
for example in mysql you may use the LIMIT keyword for limiting the number of records that are returned and in mssql you may use ROWNUMBER and oracle uses the ROWNUM system.
UPDATE cvcolumnlist
SET columnindex = columnindex+1
WHERE cvid = 40
AND columnindex > 2
this should work
UPDATE [cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3
I'm not sure if this applies. Most of my queries use something like this.
UPDATE [database].[dbo].[cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3

Mysql: Subtract one column's data from another [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I would like to subtract one column of data from another, namely gdded from gppy; both are stored in a MySQL database.
Here is my code but it seems there is something wrong. I've searched anywhere but I cannot find the right answer.
My script below
INSERT INTO denmrk
(EMPID, NAME, DATE_FR, DATE_TO, Tangkay, Alis, gppy, gdded , NET_PAY, SSS_comboE, IBG_EMPLYE, PROCESSED_DATE)
VALUES
(6666, 'JUAN DELACRUZ', '2014-02-01 00:00:00', '2014-02-01 00:00:00', 'BBBB CO', 'DRIVER', 3383.04, 300, gppy- gdded , 150, 150, GETDATE() );
With the given code and implied database structure, how can I can achieve this?
BTW there is no need to store the manipulated result in the database. You can do this when you are select the data from the database. SO it would be better to drop that col and the idea to store the calc result in the database as well.

Categories