$query = "UPDATE kids_entry SET entries=? WHERE parentsemail=?";
$stmt1 = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt1, 'is',$entries,$parentsemail);
mysqli_execute($stmt1);
if(mysqli_stmt_affected_rows($stmt1) != 1)
die("issueasdass");
mysqli_stmt_close($stmt1);
The above code does work for me on another page but i cannot seem to get it to work here. On the other page the set to update is a hardcoded in and is not dynamic so that might be why it isn't working for me. Can someone tell me what I am doing wrong? Thanks
Is it possible that entries is the same value as what's already in the database? I don't think *_affected_rows returns 1 if nothing changes. Is it possible that there's more than 1 entry for 'parentsemail'? So perhaps mysqli_stmt_affected_rows is returning something like 2? Instead of checking 1, maybe let us know what mysql_stmt_affected_rows is returning. For example, from the docs:
An integer greater than zero indicates
the number of rows affected or
retrieved. Zero indicates that no
records where updated for an
UPDATE/DELETE statement, no rows
matched the WHERE clause in the query
or that no query has yet been
executed. -1 indicates that the query
has returned an error.
The issue appears to have resolved itself. Nothing has changed but now it works!
Related
I am trying to run a couple of queries to swap sort order values within a database when up or down buttons are clicked however when he code below is executed on the 2nd query is ran.
if ($_POST['up']){
$sort_this = $_POST['sort'];
$sort_other = $_POST['sort'] - 1;
$sql_this = "UPDATE portfolio SET sort = $sort_this -1 WHERE sort = $sort_this";
mysqli_query($conn, $sql_this);
$sql_other = "UPDATE portfolio SET sort = $sort_other +1 WHERE sort = $sort_other";
mysqli_query($conn, $sql_other);
}
They both work perfectly fine on their own when i comment out the other, however when they are both display the problem is as above. I have also tried running it in a mysqli_multi_query however that didnt work either.
Any ideas?
Thanks
Given the limited amount of data my best guess would be that they DO both execute but that they're not doing what you think they should be doing.
Say $_POST['sort'] is the number 3, this means $sort_this is also 3.
The first query will go through the database and update all 3's to a 2.
$sort_other will be 3-1 (2) and so the second query will go through the database and update all 2's to a 3. Effectively undoing what the first query did. (and altering any other 2's to 3's)
You will never see the end result of the first query because the third query will overwrite all changes the first query made.
Also, simply pasting in a variable into a query like you're doing is bad practice. It is prone to SQL injection. You can avoid this by using prepared statements: http://php.net/manual/en/pdo.prepared-statements.php
I am experiencing something so basic, yet so annoying that I thought I had to put it out to the wider community to save my sanity.
I am using a table within a database to store some very basic data. There is only two columns, Id and Campaign. I only want to use a single row of the table, however, campaign will be updated at various points. I have set up the table as follows:
$sql = "CREATE TABLE IF NOT EXISTS TestCampaign(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Campaign CHAR(20))";
Initially I write to the table to insert a null CHAR in campaign:
$sql = "INSERT INTO TestCampaign (Campaign) VALUES ('None')";
The based on a specific text field being filled in on an html form followed by a submit button press I intended to do the update of the campaign field:
$sql = "UPDATE TestCampaign SET Campaign = '$Test' WHERE id = '1'";
$Test is the POSTED campaign name from the form. Unfortunately although the INSERT works fine the UPDATE doesn't. I have checked the permissions and I have ALL on this database. I have also checked the syntax with various sites and it seems that it is fine.
Interestingly I do not get an error when I echo:
echo " ".mysqli_error($con);
I'm sure I have made some basic error somewhere but I have been looking at it for so long and changing the syntax that I can't seem to spot it.
Any help would be appreciated.
UPDATE:
I have played around with the code and it seems as though the UPDATE code does work, however, It only works when it is the next line of code after the INSERT. In fact I have found that it works as long as it is not where I need it to be. I have it placed in 'if' statement that is run only on a specific button press on the form:
if(isset($_POST['TestID']))
{
Some Code;
$sql = "UPDATE TestCampaign SET Campaign = '$Test' WHERE id = '1'";
Some More Code;
}
I have checked the rest of the code in the 'if' statement and it seems solid.
Is this odd behaviour or have I missed something?
SOLVED
Finally found out what the problem was, it ended up that when exiting the first 'if' statement as expected the html form code was revisited which must have closed the connection to the database, when the button was pressed to run the second 'if' statement there was a connection to MySQL but no connection to the database I needed access to. A quick fix to re-connect and all works fine.
$sql = "UPDATE TestCampaign SET Campaign = '$Test' WHERE id = 1";
Is the only thing I think is wrong... What do you get with :
Select * from TestCampaign
have you set up any triggers based on this table name? After update, before update triggers may prevent you from storing the required data on the table.
Also, MySQL may use 0 as id value if you don't specify the value of the id at sending insert command. Are you sure the value of your id is 1 in the record you want to update? If you have run insert statements before on your table, the id may be a larger number than 1, because of the MySQL indices (I guess this may be the problem).
I don't know how exactly PHP statements are processed when sending them to MySQL, but I would recommend you to use PDO statements, the PHP syntax would look something like this:
$sql = $pdo->prepare("UPDATE TestCampaign SET Campaign = ? WHERE id = ?");
$sql->bindParam(1, $Test);
$sql->bindParam(2, 1);
$sql->execute();
Tutorial: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
I would also echo your $Test variable to check what is stored in it.
Hope it helps...
Finally found out what the problem was, it ended up that when exiting the first 'if' statement as expected the html form code was revisited which must have closed the connection to the database, when the button was pressed to run the second 'if' statement there was a connection to MySQL but no connection to the database I needed access to. A quick fix to re-connect and all works fine.
The following code runs without any errors but doesn't actually delete anything:
$update = $mysqli->prepare('DELETE FROM table WHERE RetailerID = ? AND Amount = ? AND FXRate = ?');
$update->bind_param('iii', $rID, $base_value, $fx_rate);
$update->execute();
$update->close();
I have numerous mysqli prepared statments in this same file that execute fine, but this one is the only one that doesn't modify the table. No errors or shown, but the row isn't deleted from the table either. I have verified that $rID, $base_value, and $fx_rate are the correct values, and a row is DEFINITELY present in table that matches those values.
The only difference between this statement and the others are the parameters and the fact that it's DELETE instead of SELECT or UPDATE. I also tried doing a SELECT or UPDATE instead of DELETE using the same WHERE parameters, but no luck. The issue seems to be that it's not finding a row that fits the WHERE parameters, but like I said, the row is definitely there.
Any ideas?
Is amount an integer or a double? You're converting to integer ('iii'), but I presume it'll be $0.34 or similar. Try 'idi' instead.
Edit: same applies for rate - is that an integer or double too?
I tried doing this,
//returns the last updated id
$val = $DB->GetOne("SELECT id FROM table_name ORDER BY id DESC LIMIT 0,1");
But it doesn't work unless I do $DB->GetRow(). So, does this mean that I need to have where clause for the GetOne() to work?
I would imagine that since you are querying for one result, the WHERE clause is essential. If you attempt to get one result without defining what you are looking for, then I don't imagine it will work in the way you intend. Refer to the PHP Code Examples from the ADOdb documentation.
The code in the question will work but there was a bug in the adodb mysql driver where it will return false (instead of NULL) when no results are found. The bug was fixed in 2010 . So you may be experiencing the bug if you're running an old version of adodb.
GetOne returns the content of the first field in the first row of the result set.
Also saying "it's not working" doesn't say much.
Try printing $DB->ErrorMsg() to see what's wrong.
Ok, let say we have two rows:
member_id, name
Let say member_id = 15 and name = 'John';
I want to UPDATE this data and do the following query:
mysql_query("UPDATE members SET member_id = 14, name = 'Peter' WHERE member_id = 15
This is just an example, but is it possible that mysql would fail and UPDATE for example only name row. So, after completing mysql_query above, it would become member_id = 15 and name = 'Peter';
It is just an example. Today, a similar situation happened in my website and I checked my code hundred times and I see no errors and there hadn't been no same errors before it at all.
So, should I recheck my code one hundred times more, or it can happen?
Thank you very much.
According to the spec, single UPDATE statements are atomic; ie: either it updates all columns or it doesn't update any of them.
So no, it shouldn't happen. But of course there could be a bug with MySQL.
Put or die(mysql_error()); after your query so that if this really is happening then you would at least know about it.
I think it should not happen that it would become member_id = 15 and name = 'Peter'. The SQL syntax is right as far as i can see and it seems all good. If something wrong happens to your database and your query is executed in that moment God only knows what could happen. Despite this, most of the time either the query is executed entirely or it is not, making mysql_query() return false.
You should, as already suggested at least check if there where any error with a code such as the following if you are in an online business website:
mysql_query($sql) or die('An error occurred');
or something like as follows if you are in debug mode:
mysql_query($sql) or die(mysql_error());
I'm not as sure as everyone else that single-row updates are atomic. MySQL differs from standard SQL in that it does the column assignments of a single-table UPDATE left-to-right. That means member_id = 14 is done before name = 'Peter'. If name = 'Peter' violates a constraint, or triggers another update that fails, then that assignment will fail. However, whether the statement as a whole fails or not may depend on various factors, including whether the table is using a transaction-safe engine.