mysql query not working no error ot anything - php

$result53543534 = mysql_query("UPDATE users SET credit=credit+1 WHERE email= '{$battle_get['username']}'")
or die(mysql_error());
But does not update. checked $battle_get['username'] and the username is in there.
i am not getting any errors or anything just not adding...
Any help would be very nice thanks in advance

See what the result from mysql_affected_rows() is:
if ( ! $result53543534 = mysql_query( "UPDATE users SET credit=credit+1 WHERE email= '{$battle_get['username']}'") ) {
die( mysql_error() );
} else {
echo "Number of rows affected: " . mysql_affected_rows() . "<br>";
}
I may not have the syntax completely right but I hope you get the idea. If the result is 0, you're not specifying the WHERE syntax so that it actually refers to any row(s).
If the result is greater than 0, then you're mistaken if you think it's not affecting any rows. It may not be affecting the rows you think it should, but that's another issue.
Also, echo your sql statement so you can actually see exactly what it's doing.

Try testing
$email = $battle_get['username'];
UPDATE users SET credit=credit+1 WHERE email= '$email'

you forgot about committing the query to database, try transaction
mysql_query("START TRANSACTION");
mysql_query("UPDATE users SET credit=credit+1 WHERE email= '{$battle_get['username']}'");
mysql_query("COMMIT");

I would suggest changing the code to as follows, it will allow you to examine the sql query. Try testing the sql query to see if it runs at all. You may also want to run DESCRIBE users on your mysql console to see what sort of information you get.
$sql = "UPDATE users SET credit=credit+1 WHERE email= '{$battle_get['username']}'"
echo $sql;
if ( ! $result53543534 = mysql_query($sql) ) {
die( mysql_error() );
} else {
echo "Number of rows affected: " . mysql_affected_rows() . "<br>";
}

Grant UPDATE permission to the user in the connection you're using for this. Just because the user is able to SELECT and INSERT, does not mean they can automatically UPDATE as well, you need to explicitly grant them the permission to UPDATE. And also mysql_error() will not show any error because your SQL statement is correct, hence no error shown.

Happen to me the same thing, what I did first was to reproduce it on MySQL Workbench then use the SQL sentences from there, I found out (not sure why) it works adding parenthesis after the WHERE clause:
UPDATE users SET credit = credit + 1 WHERE (`email`= '{$battle_get['username']}')

Please check you mention name=email
And name=password attributes in all input field and also mention in name=submit in submit if it's input.

Related

PHP MySQL delete where values are equal to

I have a PHP chat script that calls a MySQL database when a user signs out to delete them from the database.
My script is:
if(isset($_GET['logout'])){
mysql_query("DELETE FROM users WHERE username='" .$user['username']. "' AND rank='0'");
header("Location: login_mini.php?logout=1");
}
What I want to do is delete the user if they have a rank of 0 when they leave. Why isn't this script working?
DELETE doesn't take column arguments
Remove the *
The syntax for MYSQL Delete example:
DELETE FROM somelog WHERE user = 'jcole'
ORDER BY timestamp_column LIMIT 1;
So you're query is wrong that's the reason why it is not running:
It should be
//without * and add quotes in your $user['username']
mysql_query("DELETE FROM users WHERE username=" .$user['username']. " AND rank='0'");
mysql_query("DELETE FROM users WHERE username='$user[username]' AND rank=0");
There is no * or any columns in DELETE operation because you are deleting the whole row(s).
Are you sure that users table have a record that have rank == 0 ?
Check it by
SELECT COUNT(*)
FROM users
WHERE rank='0'
then if there is check your variable $user['username'] if it has value.
var_dump($user);
then if both has value then try to execute this manually on your mysql
SELECT *
FROM users
WHERE username = #the value of the username
AND rank = '0'
If there is a result then maybe your PHP is throwing an error while executing the mysql_query. try to insert this code after the mysql_query
if (mysql_error()) {
die(mysql_error());
}

Every time person will go to a page, int will increase in the database

Basically I want vote_count to increase everytime a member views the page.
from 1 to 2, from 2 to 3, etc.
I have done this but it doesn't really work:
mysql_connect('localhost', 'vote', '') or die (mysql_error());
mysql_select_db('counter') or die (mysql_error());
mysql_query("INSERT INTO `vote_count` (`vote_number`) VALUES (NULL)");
mysql_close();
it stays empty.
My row looks like this:
What have I done wrong?
Stop using mysql_ functions; they are deprecated
Use error handling to catch errors:
mysql_query("INSERT INTO vote_count (vote_number) VALUES (NULL)") or die( mysql_error() );
The column does not allow NULL.
Use UPDATE to update/increment the count:
UPDATE vote_count SET vote_number = vote_number + 1
Use a password.
You already have the value and just want to update it. Hence, you just need to increment its value in 1.
Hence, you can use
mysql_query("UPDATE `vote_count` SET vote_number=vote_number+1");
You should probably look into stopping people from upping the views/votes by refreshing:
if(!isset($_SESSION['voted']) && $_SESSION['voted']){
mysql_query("UPDATE `vote_count` SET vote_number=vote_number+1") or trigger_error(mysql_error());
$_SESSION['voted'] = true;
}
else{
//Already voted.
}

MySQL UPDATE statement is not updating the associated table

I have the following code:
$query = "UPDATE jobs SET `ipt` = '$ipt', `prejobform` = '$prejobform', `fileddate` = '$fileddate' WHERE `job_id` = '$jobid'";
$result = mysql_query($query);
if (!$result) {
//ERROR LOGGER HERE
echo mysql_error();
}
else {
header('Location: view_job.php?jobid='.$jobid);
}
This code is redirecting like it is behaving correctly, but when I check the database, the fields have not been updated. I'm sure the problem is something simple that I'm missing, but I'm at a loss to find the problem.
For an UPDATE query, mysql_query returns true if the query succeeded (was parsed and executed correctly), not only if it did really update any rows.
If the underlying table is InnoDB and you have started a transaction earlier, the query won't commit transaction implicitly and it will roll back when you exit the script or disconnect.

mysql_query returns nulls

I've got a php script which queries mysql for a certain row based on it's id.
This works fine for the first 3 rows , but on the 4th it returns nulls on all fields.
Here's the query:
mysql_query("SELECT ind,title,body,img,tags,live FROM project WHERE ind = '".$curid."' ")
let me know if you need to see more code.
I'm going to take a guess that the forth row doesn't have the id you think it does. Return them all (remove the where statement) and output them to the screen to check, or use a database browsing tool, if you have one.
Try some debugging..
$q = "SELECT ind,title,body,img,tags,live FROM project WHERE ind = '".$curid."' ";
$rs = mysql_query($q) or die("MySQL error in Query: ". $q ."<br><br>The error is:<br>".mysql_error());
when you execute query, change $curid by value that you consider is correct. verify result, if yet get null values, you can do it:
mysql_query("SELECT ind,title,body,img,tags,live FROM project WHERE ind = '".$curid."' " and
ind IS NOT NULL and title IS NOT NULL
)
for example

MySQL Update Fix Two rows with one query

This is the code giving me issue - I'm trying to update multiple records with one insert. The values are put in an array and using a foreach I've prepared the mysqli update. But it's not working. Just gives a MySqli error about the syntax on the update.
foreach($users as $user){
if(empty($course)) continue;
$query_string .= " SET group_id='$group_id' WHERE user_id='".$user."'; ";
}
$query_string = substr($query_string,0,-1);
$query = "UPDATE users" . $query_string;
$result = mysqli_query($dbc, $query) or trigger_error("Query: $query");
The error it gives 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 'SET group_id='10' WHERE user_id='5''. I think it's the ';' in the middle that mysqli isn't accepting.
Assuming you've got more than one user, your query will look like
UPDATE users SET ... SET ... SET ... SET ...
which is incorrect. You cannot do updates to multiple rows in this fashion. Either do multiple queries, each updating one student, or you'll have to build a huge case/if block to do this in a single query.
You'd be better off doing the multiple queries, as you'll probably spend more time BUILDING the monolithic query than it'd take to run the individual updates.
How about WHERE...IN
UPDATE foo SET bar = 0 WHERE baz IN (1,2,3,4,5,6)
(presuming that you are setting them all to the same group ID, which is not clear in the context provided)
try this code:
<?php
$queries = array();
foreach($users as $user){
if(empty($course)) continue;
$queries[] = "update users set group_id = '" . mysql_real_escape_string($group_id) . "' where user_id = '" . mysql_real_escape_string($user) . "'";
}
array_map('mysql_query', $queries);
?>
Your problem is that you don't separate the different users with ;. Since you're updating all users to have the same group (I'm not sure this is the case, otherwise it will get much more complex) you can simply expand the criteria with OR. Your resulting query would look something like the following:
UPDATE users SET group_id='42' WHERE user_id='1' OR user_id='2' OR user_id='3';
Another solution would be to use WHERE ... IN. Here's an example of that:
UPDATE users SET group_id='42' WHERE user_id IN (1, 2, 3);

Categories