i am writing a php code involving mysql. At one point, i have to update a table in my database for which i am using the below 2 statements. But these are not working.
$temp = $row['tracking_id'];
mysql_query("UPDATE order_products SET state=4.00 WHERE tracking_id = '$temp'");
Note that i don't get an error message. The table is not updated though. Also note, the column names, table names are correct. I have also tried without the single '' quotes around $temp in WHERE clause.
The connection to database is fine. I know this cos select queries are working fine.
Any ideas?
Thanks
Try to var_dump temp
var_dump($temp);
and also check errors from your query
mysql_query("UPDATE order_products SET state=4.00 WHERE tracking_id = '$temp'") or trigger_error(mysql_error()." <here is that problem");
It will give you your answer
Firstly check what is the value in $temp.
If value is fine the do it like this to echo your query.
echo $sql = "UPDATE order_products SET state=4.00 WHERE tracking_id = '$temp'";
$result = mysql_query($sql);
its only for testing and check the query is it fine? try running it directly and see if any error comes
How did you fetch the row?
using mysql_tetch_row or mysql_fetch_array?
if you used fetch_array it's not an associative array and you can't do row['something'], only row[index], so use fetch_row instead.
print the value of temp to verify it's ok and if still can't find the problem try using the mysql_error() function to prinT the last mysql error.
Note that i don't get an error message.
No wonder. You have to ask a database for the error message.
$tmp = mysql_real_escape_string($row['tracking_id']);
$sql = "UPDATE order_products SET state=4.00 WHERE tracking_id = '$tmp'";
mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
var_dump(mysql_affected_rows); //to see if rows were changed.
if it prints only 0 - then you have a row with exactly same values in your table already
Related
I'm almost sorry to ask this question but I'm drawing a complete blank. I'm getting the following error:
"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 'WHERE number='7'' at line 1"
It seems whenever I try to use just an integer in the following code, I get the syntax error;
$go = mysql_query("UPDATE $db1 SET count='$t1c', WHERE number='$input2'") or die(mysql_error());
As you can see the page gets the value, that's not the issue.. it just doesn't seem to like the WHERE = 7 part. I've tried with and without the quote marks, I've tried changing that column in the table from a int to a varchar. Still get the same thing yet the code BEFORE this piece that runs:
$check1 = mysql_query("SELECT * FROM $db1 WHERE number='$input2'");
Run's absolutely fine. It finds the value where number equals $input2...
Can someone help me PLEASE? I'm drawing a complete blank here :/
Remove the , in the query:
mysql_query("UPDATE $db1 SET count='$t1c' WHERE number='$input2'");
Remove comma(,) which is placed before WHERE in UPDATE query
$go = mysql_query("UPDATE $db1 SET count='$t1c' WHERE number='$input2'") or die(mysql_error());
Change
"UPDATE $db1 SET count='$t1c', WHERE number='$input2'"
to
"UPDATE $db1 SET count='$t1c' WHERE number='$input2'"
The comma shouldn't be there (before WHERE) and is causing an error.
number is a reserved word in mysql sql
it is better not to name columns with that words or you need to backtick them in query
example:
`number`=3
mysql reserved words
I am using a list of Insert and Update queries to process the turns for my strategy game. Here are some that worked and others that didn't.
mysql_query("UPDATE gc3025_games_prime SET `turns` = `turns`+1 WHERE `active`='1'");
mysql_query("UPDATE gc3025_processing_check, gc3025_games_prime SET gc3025_processing_check.turn = gc3025_games_prime.turns WHERE gc3025_processing_check.game_id = gc3025_games_prime.id");
mysql_query("UPDATE gc3025_processing_check SET `turn_update`='yes' WHERE `game_id`='1' AND `turn`='$current_turn'");
These are right after and didn't process. They do process when I manually enter them on the server.
mysql_query ("UPDATE `gc3025_cbills_remaining` SET `cbills_remaining` = `unspent_cbills`");
mysql_query ("UPDATE `gc3025_cbills_remaining` SET `starting_cbills` = `cbills_remaining` + `base_cbills`");
mysql_query ("UPDATE `gc3025_cbills_remaining` SET `unspent_cbills` = starting_cbills");
Is there a way to make sure they actually process? Any help would be appreciated.
Just use mysql_affected_rows to obtain what you need.
You can obtain two different values from this call
-1 (tells you that something didn't worked)
integer value >= 0 (tells you the exact number of row affected)
BTW I would suggest not to use mysql_* functions: use mysqli_* or PDO
I have got this code so insert values into a table in MySQL through PHP. I have tried all the possible Insert syntax, it does not insert the data... this are the codes that i used.
$param = "xyzxyz";
$param1 = "sdfdfg";
$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('".$param."','".$param1."')";
$result = $mysql->query($sql);
if($result)
echo "successful";
else
echo mysql->error;
if(mysql->errno==0)
echo "successful"
else
echo mysql->error;
I even tried the following sql syntax
"INSERT INTO trail (User_Name, Quiz_ID) VALUES ('$param1','$param1')";
"INSERT INTO `trail` (`User_Name`, `Quiz_ID`) VALUES ('$param1','$param1')";
and i tried several other none of them inserts anything into the table. and this is the table in MySQL;
trail
User_Name varchar(35)
Quiz_ID varchar(35)
It does not insert anything nor does it display any error. And I have the correct DB connection because i am able to Select from the table. Its just the insert that is tricky.
Any help would be much appreciated.
Thanks
Just a note if someone is running on similar problems:
I had a similar issue --- Insert query working on PHPMyAdmin but not working on PHP and not issuing any errors (result was true all the time).
The reason is that I was starting a transaction but forgetting to commit it...
$mysqli->autocommit(FALSE);
$mysqli->query( "START TRANSACTION" );
Never forget this:
$mysqli->commit();
It is a silly error, I know, but I was so focused on the query mistery that I forgot the transaction statements a few lines above.
Check the mysqli::$errno first.
if(mysql->errno==0)
echo "successful"
else
echo mysql->error;
What I have done is if you don't have a debugger installed, just have it email you the query. This way you can see what the final query is and if you have access to something like phpMyAdmin try manually running the query and see what happens. Another thing, make sure that you are searching for your inserted record correctly, if you are using a search query because of the number of records make sure the WHERE condition is right, that has burned me a few times.
EDIT
Missing symbol around names maybe. I have to run all my MySQL queries like
`nameOfThing`
instead of just nameOfThing
$param = "xyzxyz";
$param1 = "sdfdfg";
$sql = "INSERT INTO `trail` (`User_Name`, `Quiz_ID`) VALUES ('".$param."','".$param1."')";
$result = $mysql->query($sql);
if($result)
echo "successful";
else
echo mysql->error;
if(mysql->errno==0)
echo "successful"
else
echo mysql->error;
FYI, you are inserting $param1 twice.
You also don't have a ';' after echo "successful".
I'd suggest you clean up the code example, and try things again, and let us know.
Things to clean up
$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('$param','$param1')";
You don't need to concatenate the variables in a string concatenate, you can interpolate. However, you actually should use PDO with a prepared statement to avoid the potential for SQL injection.
Add that missing ;
put that first check of if(mysql->errno==0) in (unless you are going to switch to PDO for this stuff).
Fix mysql->error to be mysql->error()
Maybe some other things from the comments.
Well, if the following code produce no error and shows 1 affected row, most likely you are looking for the result in the wrong database.
ini_set('display_errors', 1);
error_reporting(E_ALL);
$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('testing','1')";
$mysql->query($sql);
var_dump($mysql->error,$mysql->affected_rows);
My tables were InnoDB tables and when i changed my tables to MyISAM the insert worked fine. Well i have never encountered this problem before. Well that did the trick for the time being.
If i want to use InnoDB engine for transactions? How can i get php to be able to insert values in InnoDB table? Any one got any suggestion? And i am using WAMP server and the MySQL is version 5.5.24. And i did change the InnoDB conf in my.ini but that did not seem to work either?
try this
$param = "xyzxyz";
$param1 = "sdfdfg";
$sql = "INSERT INTO trail (User_Name, Quiz_ID) VALUES ('".$param."','".$param1."')"; $result = $mysql_query($sql); if($result){ echo "successful";} else { echo " not successful;}
I'm trying to add new entries to an sql database using php,
i can add a new entry, but it adds 2 blank lines to the database.
The query i use is
$query = "INSERT INTO dbo.Products (Name,Id,Price) VALUES ('$NewName','$NewId','$Price')";
$result = sqlsrv_query($conn3, $query);
any ideas why it'd do that?
Try using VALUE instead of VALUES to make sure only a single entry is added.
This should limit the insertion to a single item only. It then depends if you are keen to debug the issue more thoroughly to see what was the problem.
Lastly, it never hurts to check if the variables are all properly escaped.
You can try
$sql = "INSERT INTO dbo.Products (Name,Id,Price) VALUES('%s','%d','%f')";
$result = sqlsrv_query($conn3, sprintf($sql,mysql_real_escape_string($NewName),$NewId,$Price));
Hi guys I was hoping from some help here, please.
I have a INSERT query to a table, after this is done I am calling:
mysql_insert_id();
In order to send the last ID inserted into the table to the next page like this:
$insertGoTo = "confirm_booking.php?booking_ID=" .$_POST['booking_ID']. "";
Unfortunately it does not work, all I get is a zero.
The table I am inserting into has an auto increment number and values are inserted into it.
I have also tried SELECT MAX(id) FROM mytable. This dosn't work neither.
I know that this problem has been talked about already. I read all posts but nothing came useful.
Many thanks Francesco
You have to use the value returned by MySql_Insert_Id () when you generate your link:
// your query
$newId = MySql_Insert_Id ();
$insertGoTo = "confirm_booking.php?booking_ID=" . $newId;
It is possible that your table does not have any AUTO_INCREMENT field!
It could also happen because you have two or more mysql connections at the same time.
In this case you should use a link identifier.
$link = mysql_connect( ... );
mysql_select_db('mydb', $link);
mysql_query('INSERT mytable SET abc="123"', $link);
$inserted_id = mysql_insert_id($link);
Some key points from the PHP Manual:
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.
If not having an AUTO_INCREMENT field is not your problem, you might want to try storing the result of the mysql_query call and using that as an argument to the id function
$result = mysql_query("...");
$id = mysql_insert_id($result);
Had an issue using a query like this:
INSERT INTO members (username,password,email) VALUES (...)
reason being that the id (which is my primary key and Auto Increment field) is not part of the query.
Changing it to:
INSERT INTO members (id,username,password,email) VALUES ('',...)
using a an empty value '' will have MySQL use the Auto Increment value but also allow you to use it in your query so you can return the insert id
mysql_insert_id may return 0 or false if your insert fails right?
So if you have trouble with mysql_insert_id not retunring what you expect confirm that you don't have a unique constraint or some other problem with your sql that would cause the insert to fail. Using max is a terrible idea if you consider this.
Make sure to put mysql_insert_id()after the
mysql_query($sql, $con); //Execute the query
Above query responsible for execute your Insert INTO ... command.
After you can get the last ID inserted
I have also suffer from this problem. Finally I found that the problem occur in my connection to the database. You can use this following connection code to connect the database then you can easily use mysqli_insert_id().
$db_connect = mysqli_connect("localhost", "root", "", "social");
Then you can use mysqli_insert_id() as
$id = mysqli_insert_id($db_conx);
I hope this will help you. I you have any problem then leave your comment.
The mysqli_insert_id function has been deprecated. This may be your problem.
Instead, try $mysqli->insert_id. See the documentation for more info.