MySQL UPDATE statement is not updating the associated table - php

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.

Related

PHP select query not getting updated records

I am working on a php form using POST and use dynamic HTML tables. I truncate the DB table every time before inserting the data in to the table which seems fine.
Select query when the page loads (to update UI part)
$query = 'SELECT * FROM TABLENAME';
$result = mysqli_query($dbConnection, $query);
$rows = array();
if(!$result)
{
//log error
}
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
print_r( $row);
$rows[] = $row;
}
}
Insert part
if(isset($_POST['u']))
{
foreach($_POST['u'] as $key => $value)
{
$ky = $_POST['x'][$key];
$query = "INSERT INTO TABLENAME (ID,KY) VALUES ($value, '$ky')";
$result = mysqli_query($dbConnection, $query);
}
}
However, upon posting, when using select query, it seems to not get the latest records inserted, I made sure to check the records are inserted properly in PhpMyadmin.
When I reload the page, it works fine. Only thing is it's not getting the updated records when it's POSTed, but works the subsequent times.
I thought it could be due to connection object and tried to use different connections objects with no luck.
INFO: Not sure if it has any impact, the database is a wordpress and I created new table in this database for this.
Thanks in advance...
The problem is that you need to do a SELECT query after your INSERT query in order to get the full data set.
You might consider posting to a separate page, and then redirect the user back to the original page after the INSERT. This will cause the SELECT to run again, since the page reloads.

Mysql transaction not working as expected

I have read a lot of documentation about mysql and transactions and from what I have understood is that when I use repeatable read isolation level in MySQL InnoDB and I start a transaction and I add some rows that when I do a select on those rows they aren't seen by MySQL in that transaction. For example:
function dotransaction() {
$result = mysqli_query("START TRANSACTION");
if (!$result) return false;
$result = mysqli_query("INSERT INTO t (f1,f2) VALUES (1,2)");
if (!$result) {
mysqli_query("ROLLBACK");
return false;
}
$result = mysqli_query("INSERT INTO t (f1,f2) VALUES (3,4)");
if (!$result) {
mysqli_query("ROLLBACK");
return false;
}
$result = mysqli_query("UPDATE t2 SET f3=(SELECT COUNT(*) AS count FROM t WHERE f1=1) WHERE f4=2");
if (!$result) {
mysqli_query("ROLLBACK");
return false;
}
}
If i execute the following code in PHP it really updates field f3 to 1 in table t2. But I didn't expect that, because when I started the transaction the transaction reads a snapshot from the database and whereever I use a select statement it uses the first read snapshot and not the modifications to the table. When I take the select statement out of the UPDATE statement it also goves met count=1.
You understand wrong.
Changes to data made within transaction ARE visible within that transaction, so your SELECT statements will see updated rows.
These changes however are not visible outside of the transaction, so if another user connects to the server at the same time, and queries these rows, he will see them in unaltered state.

MySqli not rolling back appropriately

I have a database listed as $db under mysqli. This database is contains into two tables, I listed them below as table and table2 (just for this example). Table2's rows requires an id from table. This is fine, but there might be a problem adding the columns into table2 thus requiring a rollback routine. However, it doesn't seem to be working.
I started with turning off the auto-commit. I then tried to put in the rollback command even though I am using the die command to signal a failure. As far as I am concerned the transaction could be blasted into oblivion in mid operation and the database should still be stable. So I am not sure what is going on here unless the database is completely ignoring the fact that I am trying to turn off auto-commit.
The basic structure of my code is listed below:
function problem($str)
{
global $db;
mysqli_rollback($db);
die($str);
}
mysqli_autocommit($db,false);
//Basic check if exists
$sqlstr = "SELECT * FROM table WHERE name = '$name';";
$r = mysqli_query($db,$sqlstr);
if (mysqli_num_rows($r)>0){problem("A row already exists under that id");}
//Insert the row
$sqlstr = "INSERT INTO table (name,v1,v2,v3) VALUES ('$name','$v1','$v2','$v3');";
$r = mysqli_query($db,$sqlstr);
if (!$r){problem("Could not insert into the table. $sqlstr");}
//Get the generated id part 1
$sqlstr = "SELECT id FROM table WHERE name = '$name';";
$r = mysqli_query($db,$sqlstr);
if (!$r){problem("Could not add into the table. $sqlstr");}
//Get the generated id part 2
$row = mysqli_fetch_assoc($r);
$eid = $row['id'];
//A simple loop
$count = count($questions);
for ($i=1;i<=$count;$i++)
{
//This is where it typically could die.
$r = mysqli_query($db,"INSERT INTO table2 VALUES (...);");
if (!$r){problem("Could not add to the table2. $sqlstr");}
}
mysqli_commit($db);
Is there something I am missing? I tried to follow the examples I found for the auto-commit as closely as I could.
Transactions only work if the table engine supports them, e.g. InnoDB.

mysql query not working no error ot anything

$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.

mysqli_multi_query function not updating all table rows in database

I have been attempting to use mysqli_multi_query to update multiple table rows at once. I have found that this function always leads to the update of all rows except for 1 row. For instance, if I had 5 rows of data that I designated to be updated, then only 4 rows are actually updated. Even when I increased the numbers of rows to 6 or 7 etc, there is only 'n-1' rows actually updated ('n' being the numbers that I designated to be updated).
some of the code is below:
<?php $jag = mysqli_connect($host, $user, $pass, $db); // connects to the database
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query2 .= "UPDATE inst_prod_stor SET age_type = '2' WHERE payer_mail = '$pmail' AND file_name = '$fname';";
print_r($query2);
// execute the 'update' multi query
$result2 = mysqli_multi_query($jag, $query2) or die(mysqli_error($jag));
mysqli_close($jag); ?>
I have also checked the source code on my webpage after I execute the php file that cotains this code and it is fine. I receive no errors. I actually used the 'print_r' function to check that no data was being left out before the 'mysqli_multi_query' function executed. And in fact, no data was left out. The result of the function is almost perfect every single time I execute the code. The only imperfection is the fact that one row of the table is never of updated. And each time it's a different row.
I really need help on this one, it is pretty much the last leg of a 2 or 3 week coding journey before I finish up a project that I am currently working on. Thanks!
use mysql_real_escape_string and remove . near $query2
$pmail = mysql_real_escape_string($pmail);
$fname= mysql_real_escape_string($fname);
$query2 = "UPDATE inst_prod_stor SET age_type = '2' WHERE payer_mail = '$pmail' AND file_name = '$fname';";
$result2 = mysqli_multi_query($jag, $query2) or die(mysqli_error($jag));
will only die() if the first query in $query2 has an error.
I suggest that you use a do-while loop to establish a means to debug non-first query sql failures using mysqli_error() & mysqli_affected_rows().
Strict Standards: mysqli_next_result() error with mysqli_multi_query

Categories