After executing the query below I use the PHP function mysql_insert_id() and it always gives me 0.
UPDATE
tbl_training_types
SET
fld_serial = $serial_no,
fld_name = $training_name,
fld_description = $training_description
WHERE
fld_id = $id
When you update a note in database you already have something using what you can identify it. In your example you already have $id which should be containing the value of data you have recently updated. If you don't have id and you try to update with name or something different after update you can simply retrieve updated data with simple query:
select * from table_name where your condition
Note: mysql_* functions are deprecated and won't be supported in future versions. You should be using either mysqli_* or PDO.
Return Values
The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.
Source PHP Manual
Related
I am inserting values in a table but it's not working in sequence, see screenshot:
Code:
$query = "insert into msgs values(NULL,'".$sender."','".$receiver."','".$message."','".$state."','".$time."')";
mysql_query($query);
First of all don't use mysql, use mysqli. Please check this: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
Using null when inserting autoincrement is fine (see Devon's first comentary).
When looking at your timestamp (msg_time) i can see that your table is short by another column because they are not in order anyway. This is only an visual shorting and don't affect entries structure, so don't worry.
I am trying to update a record in my database and SQL query return successful but my database is not changing.
representative_id = '$login_session'
Is the representative_id column in your table a VARCHAR, or an INT? You're passing a string to MySQL, there. If the column in your DB is stored as an INT, try removing the single quotes around $login_session.
When posting questions relating to database queries, it helps to also include your table schema.
Seems like you're using PDO (hopefully)
When you're using this Data Objects, when a query is executed it will return TRUE, but it doesn't care if it changed something or not, it will return true when there is not a critical MYSQL Error.
What you want to do is to know IF A ROW AS AFFECTED / CHANGED by that query, in that case instead of using;
$PDOObject->query($thequery) == true
you should use:
$result = $PDOObject->query($thequery);
if($result->rowCount() > 0) ...
More info about how to know if a row was affected by a query with PDO
http://php.net/manual/en/pdostatement.rowcount.php
sql query return successful but my database is not changing
Are you sure that there exists at least one record that matches your filter criteria WHERE full_name = '$full_name' AND representative_id = '$login_session'. If no record updating means no any record matching the WHERE condition and so no updates occurring.
Am trying to making a specific query as follow
$query = "SELECT * FROM users WHERE uid IN (1,10,50,60,94,102)";
how can i use a mysql escape string for where clause or if i just use it like that, is it ok?
The way you are doing now is not an UPDATE query. You are only retrieving records from the database. In order to update records from the database, you need to use the UPDATE DML. The syntax for that is
UPDATE tableName
SET columnName = newValue
WHERE columnN = value // <== this is your condition
Returning back to your question, the way you are doing the query doesn't need mysql_escape_string because you are only passing integer values. Currently, more PHP developers are not using mysql_escape_string anymore because it's already been discourage and as the PHP Manual site says this function is already depreciated. Alternatively, PDO_MYSQL or MYSQLi extensions should be used instead.
Wouldn't there be a problem with it if for example when a user clicks on a link, a new row is automatically inserted and then the php code requests the last inserted id, and at the same time another row is inserted by another user, so the returned id is actually not the one I'm expecting..?
Am I wrong? Is there a way to do the same without that 'security' hole?
(like maybe from within the prepared statement or something...)
P.S the id is automatically generated.
Thank you.
As mentioned in the manual:
LAST_INSERT_ID() (with no argument) returns a BIGINT (64-bit) value representing the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this:
mysql>SELECT LAST_INSERT_ID();
->195
The currently executing statement does not affect the value of
LAST_INSERT_ID(). Suppose that you generate an AUTO_INCREMENT value
with one statement, and then refer to LAST_INSERT_ID() in a
multiple-row INSERT statement that inserts rows into a table with its
own AUTO_INCREMENT column. The value of LAST_INSERT_ID() will remain
stable in the second statement; its value for the second and later
rows is not affected by the earlier row insertions. (However, if you
mix references to LAST_INSERT_ID() and LAST_INSERT_ID(expr), the
effect is undefined.)
If the previous statement returned an error, the value of
LAST_INSERT_ID() is undefined. For transactional tables, if the
statement is rolled back due to an error, the value of
LAST_INSERT_ID() is left undefined. For manual ROLLBACK, the value of
LAST_INSERT_ID() is not restored to that before the transaction; it
remains as it was at the point of the ROLLBACK.
So, LAST_INSERT_ID() is always transaction-safe (even though you don't use transaction).
The MySQL Server transfers the insert ID as part of the OK message after a successful INSERT. This ID is stored in PDO, therefore without a round-trip to the server PDO can return you the correct ID for your connection in a safe way.
Reference: http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#OK_Packet
To counteract this you would use a transaction.
This would essentially isolate your insert from others, so as long as your Insert/lastInsertId() call is within the same transaction, it will work just fine.
If I execute an insert query with a stored procedure with php and the mysqli_* functions, is there a way to retrieve the value of an autoincrement id field?
mysqli->insert_id does not seem to work.
Are you sure the last query you preformed was an INSERT?
mysqli->insert_id seems the proper answer:
Return Values
The value of the AUTO_INCREMENT field that was updated
by the previous query. Returns zero if
there was no previous query on the
connection or if the query did not
update an AUTO_INCREMENT value.
You could try to make a query to MySql like so:
SELECT LAST_INSERT_ID()
Not sure if it works with stored procedures though.
You could add this statement in your stored procedure after the insert:
SET #saved_id = LAST_INSERT_ID()
Then, execute this query after calling the procedure:
SELECT #saved_id
mysqli->insert_id (where mysqli represents your database connection)
must be used directly after the insert. If you run other queries on the same connection
before attempting to read insert_id you get 0 returned.