i am trying to runa a multiquery inside a for loop. The post variables come from a dynamic table
if ($resultset->num_rows == 0) {
for ($i = 0; $i < count($_POST["savi_account_number"]); $i++) {
$insert = "INSERT INTO `savings_transactions_table` (`id`, `receipt_id`, `sub_code`, `savings_account`, `user_id`, `amount`, `dr`, `cr`, `actual_balance`, `available_balance`, `created_user`, `created_date`)
VALUES (NULL, '$receipt_id', '" . $_POST['savings_product'][$i] . "', '" . $_POST['savi_account_number'][$i] . "', '$member_id', '" . $_POST['savi_dep_amount'][$i] . "', '0', '" . $_POST['savi_dep_amount'][$i] . "', '0', '0', '$user_id', '$create_date');";
$insert.="INSERT INTO `gl_transactions_table` (`id`, `member_id`, `posting_product`, `product_account`, `gl_account`, `dr`, `cr`, `trx_code`, `trx_type`, `trx_reference`, `posted_date`, `posted_by`)
VALUES (NULL, '$member_id', '" . $_POST['savings_product'][$i] . "', '" . $_POST['savi_account_number'][$i] . "', '" . $_POST['savings_cap_gl'][$i] . "', '0', '" . $_POST['savi_dep_amount'][$i] . "', '$deposit_type', '$transaction_type', '$transaction_reference', '$create_date', '$user_id')";
$res = mysqli_multi_query($conn, $insert);}
but the query does not insert all values, but only one line in db.
try with this
if ($resultset->num_rows == 0) {
for ($i = 0; $i < count($_POST["savi_account_number"]); $i++)
{
$insert .= "INSERT INTO `savings_transactions_table` (`id`, `receipt_id`, `sub_code`, `savings_account`, `user_id`, `amount`, `dr`, `cr`, `actual_balance`, `available_balance`, `created_user`, `created_date`)
VALUES (NULL, '$receipt_id', '" . $_POST['savings_product'][$i] . "', '" . $_POST['savi_account_number'][$i] . "', '$member_id', '" . $_POST['savi_dep_amount'][$i] . "', '0', '" . $_POST['savi_dep_amount'][$i] . "', '0', '0', '$user_id', '$create_date');";
}
$res = mysqli_multi_query($conn, $insert);
run query outside the for loop
Related
I just started using C# and I a have a textbox on a form. I want to display only numbers from ACCESS database, Like string stored in database is 12Kg but when I do update i should return only value i.e.12, I have searched but not getting any idea, I hope you can help me...
Query:
"Update Para Set PartNumber = '"+ txtPartNoL.Text + "', MacID = '" + cmb.SelectedValue.ToString() + "'"+" Para1 = '" + txtPara1L.Text + "', PType1 = '" + lblLPara1.Text + "'," + " val(LabourPara.Para4) = '" + txtPara4L.Text + "', PType4 = '" + lblLPara4.Text + "'," + " Qty = '" + txtQtyL.Text + "', PF = '" + txtPFL.Text + "', SF = '" + txtSFL.Text + "'," + " Amount = '" + txtAmt.Text + "'" + " WHERE QoNumber = '" + txtNo.Text + "' AND PartNumber = '" + txtPartNo.Text + "'";
You are updating everything as text, which can't be true for at least quantity and amount, so try:
sql = "Update Para Set PartNumber = '"+ txtPartNoL.Text + "', MacID = '" + cmb.SelectedValue.ToString() + "' " +
"Para1 = '" + txtPara1L.Text + "', PType1 = '" + lblLPara1.Text + "', " +
"LabourPara.Para4 = " + Val(txtPara4L.Text) + ", PType4 = '" + lblLPara4.Text + "', " +
"Qty = " + txtQtyL.Text + ", PF = '" + txtPFL.Text + "', SF = '" + txtSFL.Text + "', " +
"Amount = " + txtAmt.Text + " " +
"WHERE QoNumber = " + txtNo.Text + " AND PartNumber = '" + txtPartNo.Text + "'";
However, avoid this concatenating mess and - asap - go and learn how to use parameters.
I'm trying to fix a bug in a third party application (vagrant). I have 2 variables and for some reason they are not equal. I've checked and they are both strings and both have the same value. What can be the reason?
Debug string:
"'" + Vagrant::VERSION + "', '" + windows_version + "', " + Vagrant::VERSION.class.name + ", " + windows_version.class.name + ", " + (windows_version == Vagrant::VERSION ? "true" : "false")
Output:
'1.9.6', '1.9.6', String, String, false
Hi I am having a problem storing image to mssql server I tried both Memory stream and BLOB as well and in both scinarios I get the same error "cannot insert the value null into image column msg 515 level 16 state 2 line 1", any suggestion is more than welcome Thanks.
Dim sqlstring = " begin tran;"
sqlstring &= " INSERT INTO tbl_customers (stnname, cardnum,
family_name, city, fam_mem_nu, id_num, mrkz_num) VALUES
('" & stn & "','" & cd & "', '" & fmnm & "','" & ct & "', '"
& fnum & "', '" & idn & "', '" & cntr & "')"
sqlstring &= "INSERT INTO tbl_customers(imag) SELECT * FROM
OPENROWSET(BULK N'c:\temp\tempimg', SINGLE_BLOB) imag ; "
sqlstring &= "commit tran;"
sql.CommandText = sqlstring
sql.Connection = conn
conn.Open()
Dim ms As New MemoryStream
img = CameraControl1.SnapshotSourceImage
img.Save("c:\temp\tempimg", Imaging.ImageFormat.Png)
'CameraControl1.SnapshotSourceImage.Save(ms, Imaging.ImageFormat.Bmp)
' PictureBox3.Image.Save(ms, PictureBox3.Image.RawFormat)
' ms.ToArray()
' Dim data As Byte() = ms.GetBuffer()
' Dim p As New SqlClient.SqlParameter("#img", SqlDbType.VarBinary)
' p.Value = data
' sql.Parameters.Add(p)
Dim x As Integer = sql.ExecuteNonQuery
I am trying to validate data, and depends on the result checked or unchecked the related check box, but getting below error?
Microsoft VBScript compilation error '800a03ee'
Expected ')'
<%# Language=VBScript %>
<%
Function Select()
Dim apple, banana, true
Dim SQL
SQL = " SELECT code, name, value FROM fruits WHERE code = '" &("myCode")& "'"
If (SQL = SQL & "name <> '" banana "' AND value <> '" true "' ") or (SQL = SQL & "name = '" banana "' AND value <> '" true "' ") Then
checkedBox.Common.Name = "chkbanana"
checkedBox.Checked = false
Else
checkedBox.Common.Name = "chkbanana"
checkedBox.Checked = true
End If
If (SQL = SQL & "name <> '" apple "' AND value <> '" true "' ") or (SQL = SQL & "name = '" apple "' AND value <> '" true "' ") Then
checkedBox.Common.Name = "chkapple"
checkedBox.Checked = false
Else
checkedBox.Common.Name = "chkapple"
checkedBox.Checked = true
End If
End Function
%>
You have a lot of syntax errors. When concatenating strings you need to use & around banana and such. Also, the logic seems off as you are comparing something with itself and concatenating, basically you can get it to compile and never have it run any of your conditionals as the else statements will always be run. You may want to use contains or some other comparison.
But for syntax your first if statement should look like this:
If (SQL = SQL & "name <> '" & banana & "' AND value <> '" & true & "' ") Or (SQL = SQL & "name = '" & banana & "' AND value <> '" & true & "' ") Then
Also you should not use true as the name of a variable.
I have an update query in a servlet. The syntax is is correct, but when I execute the query, nothing happens. The execution stays frozen and the command never ends. Minutes later the message "ADVERTENCIA: GRIZZLY0023: Interrupting idle Thread: http-thread-pool-8080(5)." is printed every couple of seconds
I have tried with:
con.stmt.executeUpdate ("SQL");
con.rset = con.stmt.executeQuery ("SQL");
and with PreparedStatement.
This is the query:
System.out.println(""
+ " UPDATE JSP_TABLE SET "
+ " field1 = '" + request.getParameter("input1") + "',"
+ " field2= '" + request.getParameter("input2") + "',"
+ " field3= '" + request.getParameter("input3") + "',"
+ " field4= '" + request.getParameter("input4") + "',"
+ " field5= '" + request.getParameter("input5") + "',"
+ " field6= '" + request.getParameter("input6") + "',"
+ " field7= '" + request.getParameter("input7") + "',"
+ " field8= '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " field9= '" + request.getParameter("input") + " al " + request.getParameter("hasta9") + "'"
+ " WHERE ID_PK = " + request.getParameter("inputPK"));
All my Statements, ResultSets and Connections are closed at the end of use.
I have a second update which works fine.
package Funciones;
import conexion.conectar;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActualizarDinamica extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
conectar con = new conectar();
try {
/*
* TODO output your page here. You may use following sample code.
*/
con.stmt = con.conn.createStatement();
String indicador = "";
switch (Integer.parseInt(request.getParameter("indicador"))) {
case 1:
indicador = "Nivel de Servicio";
break;
case 2:
indicador = "Venta";
break;
case 3:
indicador = request.getParameter("txt_otro");
break;
case 0:
indicador = "Presencia";
break;
};
/* way 1
* con.rset = con.stmt.executeQuery(""
+ " UPDATE JSP_TABLE SET "
+ " FIELD1 = '" + request.getParameter("input1") + "',"
+ " FIELD2 = '" + request.getParameter("input2") + "',"
+ " FIELD3 = '" + request.getParameter("input3") + "',"
+ " FIELD4 = '" + request.getParameter("input4") + "',"
+ " FIELD5 = '" + request.getParameter("input5") + "',"
+ " FIELD6 = '" + request.getParameter("input6") + "',"
+ " FIELD7 = '" + request.getParameter("input7") + "',"
+ " FIELD8 = '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " FIELD9 = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
+ " WHERE FIELD_PK = " + request.getParameter("input11_ID"));
con.rset = con.stmt.executeQuery(""
+ " UPDATE JSP_TABLE2 SET"
+ " FIELD_DATE = TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
+ " WHERE FIELD_ID = " + request.getParameter("input11_ID"));
*/
/* way 2
con.stmt.executeUpdate(""
+ " UPDATE JSP_TABLE SET "
+ " FIELD1 = '" + request.getParameter("input1") + "',"
+ " FIELD2 = '" + request.getParameter("input2") + "',"
+ " FIELD3 = '" + request.getParameter("input3") + "',"
+ " FIELD4 = '" + request.getParameter("input4") + "',"
+ " FIELD5 = '" + request.getParameter("input5") + "',"
+ " FIELD6 = '" + request.getParameter("input6") + "',"
+ " FIELD7 = '" + request.getParameter("input7") + "',"
+ " FIELD8 = '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " FIELD9 = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
+ " WHERE FIELD_PK = " + request.getParameter("input11_ID"));
con.stmt.executeUpdate(""
+ " UPDATE JSP_TABLE2 SET"
+ " FIELD_DATE = TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
+ " WHERE FIELD_ID = " + request.getParameter("input11_ID"));
*/
//way 3
PreparedStatement updateSmallQuery = null;
PreparedStatement updateBigQuery = null;
String bigQuery=
""
+ " UPDATE JSP_TABLE SET "
+ " FIELD1 = '" + request.getParameter("input1") + "',"
+ " FIELD2 = '" + request.getParameter("input2") + "',"
+ " FIELD3 = '" + request.getParameter("input3") + "',"
+ " FIELD4 = '" + request.getParameter("input4") + "',"
+ " FIELD5 = '" + request.getParameter("input5") + "',"
+ " FIELD6 = '" + request.getParameter("input6") + "',"
+ " FIELD7 = '" + request.getParameter("input7") + "',"
+ " FIELD8 = '" + indicador + " VS " + request.getParameter("input8") + "',"
+ " FIELD9 = '" + request.getParameter("input9") + " al " + request.getParameter("input10") + "'"
+ " WHERE FIELD_PK = " + request.getParameter("input11_ID");
String smallQuery= ""
+ " UPDATE JSP_TABLE2 SET"
+ " FIELD_DATE = TO_DATE('" + request.getParameter("input12") + " " + request.getParameter("input14") + ":" + request.getParameter("input13") + "','DD/MM/YYYY HH24:MI')"
+ " WHERE FIELD_ID = " + request.getParameter("input11_ID");
try {
con.conn.setAutoCommit(false);
updateSmallQuery = con.conn.prepareStatement(smallQuery);
updateBigQuery = con.conn.prepareStatement(bigQuery);
updateBigQuery.executeUpdate();
updateSmallQuery.executeUpdate();
con.conn.commit();
} catch (SQLException e) {
System.out.println("catch!");
System.out.println(e.getMessage());
if (con != null) {
try {
System.err.print("Transaction is being rolled back");
con.conn.rollback();
} catch (SQLException excep) {
}
}
} finally {
if (updateSmallQuery != null) {
updateSmallQuery.close();
}
if (updateBigQuery != null) {
updateBigQuery .close();
}
con.conn.setAutoCommit(true);
}
response.sendRedirect("sol_env_c.jsp");
} finally {
con.conn.commit();
con.conn.close();
con.stmt.close();
out.close();
}
}
}
Hope this help to find the answer.
PD: The small one run with no problems. the big one it's the problem.
EDIT:
This is conectar()
public conectar() {
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Oracle JDBC driver loaded ok.");
conn = DriverManager.getConnection(params, user, password);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
Bill is almost certainly correct and you just have a lock on rows you're trying to update. You mentioned that 'the syntax is correct', which suggests you might have checked it directly in the database - e.g. with SQL*Plus or SQL Developer - and have not issued a rollback (or commit) in that session. Attempting to update the same rows from your code is then hanging waiting for the locks to be released.
If you can find the session where you ran the update manually, issue a rollback in it. If you can't, you can see which sessions are holding locks with a query like:
select vs.osuser, vs.process, vs.logon_time, vs.sid, vs.serial#, vs.program,
dl.lock_type, dl.mode_held, dl.blocking_others
from dba_locks dl
left join v$session vs on vs.sid = dl.session_id
where lock_type != 'Media Recovery'
and lock_type != 'Redo Thread';
While your code is running - and hung - you can see what's blocking it with something like:
select vsw.osuser, vsw.process, vsw.logon_time, vsw.sid, vsw.program,
dw.lock_type, dw.mode_held,
vsh.osuser, vsh.process, vsh.logon_time, vsh.sid, vsh.serial#, vsh.program
from dba_waiters dw
left join v$session vsw on vsw.sid = dw.waiting_session
left join v$session vsh on vsh.sid = dw.holding_session;
Or search for other examples, e.g. this. You need privileges to query the v$ performance views, and if you don't have them you may need DBA assistance. If you can't manually rollback the lock you might need DBA help to kill the locking session anyway.
You should really pay attention to the comments about parameterising your statements, too.