mysql insert statement is inserting a blank row instead of the data - mysql

I am trying to add new users to my database. For some reason while using the same exact statement in two different scripts and trying to debug, mysql inserts a blank row instead of the actual data if I try to run the query from the php script in which I need it to run from.
On the other hand, when I try to run that exact query in from a random php script, passing the same exact variables (using session variables) to the database, it inserts the data into the database as it should with no problems. I have not idea how to fix such a problem and have been trying for quite a while now. I would greatly appreciate any advice on what I could do to fix this problem.
$usr_email = $_SESSION['email'];
$usr_company_name = $_SESSION['compame'];
$usr_city = $_SESSION['city'];
$usr_state = $_SESSION['state'];
$usr_phone = $_SESSION['phone'];
$usr_password = $_SESSION['password'];
$usr_first = $_SESSION['first'];
$usr_last = $_SESSION['last'];
mysql_query("INSERT INTO users (id, email, compname, city, state, phone, password, first, last)
VALUES('','$usr_email','$usr_company_name','$usr_city','$usr_state','$usr_phone','$usr_password','$usr_first','$usr_last')",$conn);

Related

JSP page is running MySQL query twice on Tomcat

I am getting this weird problem where a MySQL insert is happening twice. I kept pairing down the code until I had the simplest version, but it's still happening. Has anyone seen this before? This is on a Tomcat server.
Below is my super-simplified code. When I run this, two entries appear in the table withe same value. The strange thing is it seems to run the whole page twice, because if I do a select quickly enough I can see the first entry by itself, then seconds later the second entry appears. When I timestamp the rows, they are 1-ish seconds different.
"jdbc:mysql://127.0.0.1/mydatabase",
"myusername",
"mypassword"
);
java.sql.Statement statement = connection.createStatement();;
statement.execute("INSERT INTO tbl_test (Value) VALUES ('Test value')");
%>
Additional info:
Also if I execute a select statement after the insert and select the complete contents of the table, only the first entry shows up. But when I look in the database, there are two entires. So it's like the whole jsp page finishes running, then runs again silently in the background.
Here is an expanded version of the code that allows you to choose the input value for the database, so you can try this out on the fly and see what's happening:
<%
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection connection = java.sql.DriverManager.getConnection(
"jdbc:mysql://***/***",
"***",
"***!"
);
java.sql.Statement statement = connection.createStatement();
String Value = request.getParameter("Value");
statement.execute("INSERT INTO tbl_test (Value) VALUES ('" + Value + "');");
java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM tbl_test;");
while (rs.next()) {
%><%=rs.getString("ID")%>=<%=rs.getString("Value")%><BR><%
}
%>
You can try out this code at:
http://familychurch.life/familychurch/test.jsp?Value=Whatever
(Replace "Whatever" with the value you want to insert.)
Run it once and you'll see the value inserted once. But run it a second time, and you will see that the value you entered before is now in the table twice.
It turns out the problem was caused by a Chrome extension (Video Downloader for FaceBook™). It must have been reloading the page silently in the background.
Special shout-out to #RiggsFolly for helping me figure it out!

find the path to the script which is running the query

is there any query to return information about the current script that is running it ?
like for example if i have a file in
/home/domain/public_html/script.php
i want to put a query in it that file to return the filename and path i.e :
/home/domain/public_html/script.php
or at least the base path to it ]
/home/domain/public_html/
pleas note i know lots of method to do this but i specifically want to get these information back from database in response and after running a query
No, there is no way for the MySQL Server to know the name or path to the PHP script that is executing queries unless you tell it.
I've seen some projects that establish a coding practice to append a comment to the SQL queries with information to help you identify the source.
SELECT * FROM MyTable WHERE id = 123; /* File: script.php, Function: myFunction() */
You then see these comments appearing in the MySQL processlist, and in query logs.
You have to write code to put the comment into the SQL yourself:
$sql = sprintf("SELECT * FROM MyTable WHERE id = 123; /* File: %s, Function: %s() */",
__FILE__, __FUNCTION__);
If you don't want to change the code, another great solution is to use New Relic Application Monitoring, which tracks SQL queries in code for you, without any need to modify your code. It's costly to pay for the New Relic service, but it's the best solution. If you want to explore cheaper alternatives, search google "alternatives to new relic".

How to update a row in a MySQL database using Ruby's Sequel toolkit?

This should be the simplest thing but for some reason it's eluding me completely.
I have a Sequel connection to a database named DB. It's using the Mysql2 engine if that's important.
I'm trying to update a single record in a table in the database. The short loop I'm using looks like this:
dataset = DB["SELECT post_id, message FROM xf_post WHERE message LIKE '%#{match}%'"]
dataset.each do |row|
new_message = process_message(row[:message])
# HERE IS WHERE I WANT TO UPDATE THE ROW IN THE DATABASE!
end
I've tried:
dataset.where('post_id = ?', row[:post_id]).update(message: new_message)
Which is what the Sequel cheat sheet recommends.
And:
DB["UPDATE xf_post SET message = ? WHERE post_id = ?", new_message, row[:post_id]]
Which should be raw SQL executed by the Sequel connector. Neither throws an error or outputs any error message (I'm using a logger with the Sequel connection). But both calls fail to update the records in the database. The data is unchanged when I query the database after running the code.
How can I make the update call function properly here?
Your problem is you are using a raw SQL dataset, so the where call isn't going to change the SQL, and update is just going to execute the raw SQL. Here's what you want to do:
dataset = DB[:xf_post].select(:post_id, :message).
where(Sequel.like(:message, "%#{match}%"))
That will make the where/update combination work.
Note that your original code has a trivial SQL injection vulnerability if match depends on user input, which this new code avoids. You may want to consider using Dataset#escape_like if you want to escape metacharacters inside match, otherwise if match depends on user input, it's possible for users to use very complex matching syntax that the database may execute slowly or not handle properly.
Note that the reason that
DB["UPDATE xf_post SET message = ? WHERE post_id = ?", new_message, row[:post_id]]
doesn't work is because it only creates a dataset, it doesn't execute it. You can actually call update on that dataset to run the query and return number of affected rows.

insert row before the first row

I'm trying to write to a database at the beginning so that the new row is the first row.The code i have here will write to it, but it won't start before the first row. I've tried using resultset and tried to use beforeFirst(), but it tells me that i can't use either one. I'm stumped as to how to do it. any help would be appreciated! :D
String host = "jdbc:derby://localhost:1527/Scores";
String uName = "root";
String uPass= "root";
Connection con = DriverManager.getConnection( host, uName, uPass );
String SQLw = "INSERT into ROOT.HISCORES (Intials,Score) values (?,?)";
pst = con.prepareStatement(SQLw);
pst.setString(1,jtxtIntials.getText());
pst.setString(2,Score);
pst.execute();
The rows in a relational database are indeed stored in some physical order, but using SQL, you are unable to control this order directly. Thinking of the order in which the rows are physically stored is absolutely against the philosophy of SQL, and hence such a functionality is not included in it.
If you are interested in ordering your rows, you should add an extra attribute, for numbering the rows, by example, and use the ORDER BY clause whenever retrieving them, as mentioned in the comments.

Why does Salesforce prevent me from creating a Push Topic with a query that contains relationships?

When I execute this code in the developer console
PushTopic pushTopic = new PushTopic();
pushTopic.ApiVersion = 23.0;
pushTopic.Name = 'Test';
pushTopic.Description = 'test';
pushtopic.Query = 'SELECT Id, Account.Name FROM Case';
insert pushTopic;
System.debug('Created new PushTopic: '+ pushTopic.Id);
I receive this message:
FATAL ERROR System.DmlException: Insert failed. First exception on row
0; first error: INVALID_FIELD, relationships are not supported:
[QUERY]
The same query runs fine on the Query Editor, but when I assign it to a Push Topic I get the INVALID_FIELD exception.
If the bottom line is what the exception message says, that relationships are just not supported by Push Topic objects, how do I create a Push Topic object that will return the data I'm looking for?
Why
Salesforce prevents this because it will require them to join tables, joins in salesforces database are expensive due to the multi-tenancy. Usually when they add a new feature they will not support joins as it requires more optimization of the feature.
Push Topics are still quite new to the system and need to be real time, anything that would slow them down I'd say needs to be trimmed.
I'd suggest you look more closely at your requirement and see if there is something else that will work for you.
Workaround
A potential workaround is to add a Formula field to the Case object with the data you need and include that in the query instead. This may not work as it will still require a join to work.
A final option may be to use a workflow rule or trigger to update the account name to a custom field on the Case object this way the data is local so doesn't require a join...
PushTopics support a very small subset of SOQL queries, see more here:
https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/unsupported_soql_statements.htm
However this should work:
PushTopic casePushTopic = new PushTopic();
pushTopic.ApiVersion = 23.0;
pushTopic.Name = 'CaseTopic';
pushTopic.Description = 'test';
pushtopic.Query = 'SELECT Id, Account.Id FROM Case';
insert pushTopic;
PushTopic accountPushTopic = new PushTopic();
pushTopic.ApiVersion = 23.0;
pushTopic.Name = 'AccountTopic';
pushTopic.Description = 'test';
pushtopic.Query = 'SELECT Id, Name FROM Account';
insert pushTopic;
It really depends on your use case though, if it is for replicating into RDBMS this should be enough, you can use a join to get the full data.