Query doesn't update correctly in mySql [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 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

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.

SQL in PHP seems to not work [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 want to write some Data in my MySQL database but it seems to not work.
All the variables are filled, tested it by echoing them. Now I want to insert them into my sip table but it just doesnt do it.
Where did I go wrong on this?
echo $a_dw.$a_name.$a_an.$a_pw."<br>";
$sql = "INSERT INTO sip VALUES ('".$a_dw."','".$a_name."', '".$a_an."', '".$a_pw."')";
mysql_query($sql);
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
You forgot the column names.

Simple MySQL UPDATE only affects certain rows [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
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

Mysql error 1064.PHP when select data with forward slashes [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 try to execute this query with PHP. but mysql server is giving to error like this.
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 'index='CEA/EO/MA/0001'' at line 1. What is the reason for that?
My PHP code part is
$index = ($_POST['index']);
$sql = "SELECT * FROM results WHERE index='CEA/EO/MA/0001'";
$query = mysql_query($sql) or die(mysql_error());
index is a reserved keyword in MySQL. If you're going to name a column index you wrap it in backticks:
$sql = "SELECT * FROM results WHERE `index`='CEA/EO/MA/0001'";
Refer to the following page for the full list of MySQL reserved words:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Categories