Deleting a value from sql database using jsp - sql

I'm trying to use jsp to rendering a simple database and have the ability to delete a row using a button. I'm running into the trouble of not being able to fetch the primary key from the row because I've been using to iterate through the table. I've tried passing just simple numbers to denote the primary key (integers) but its not grabbing it.
Lets say I want to delete the row with the primary key of 12.
<form action="deleteResponse.jsp">
<c:set var="numId" value="12"/>
<input type="submit" name="delete" value="Delete"/>
</form>
This generates a button which opens up deleteResponse.jsp which has the following to catch the value.
<sql:update var="counselorDelete" dataSource="jdbc/IFPWAFCAD">
DELETE FROM Counselor
WHERE counselor_id = ?<sql:param value="${param.numId}"/>
</sql:update>
It's not doing anything, and I'm not sure whats going on with it. If I manually put in the value 12 in the sql query it will delete the row but it won't do it if I try to fetch the value.
Thanks very much in advance!
Also as a side note. I'm having a hard time understanding how to properly write JSPs. I see that most of the time people use <% Java code %> but in the netbean tutorial, it uses the and functions. Could someone explain that to me? I'm also using glassfish as a localhost sql server. Netbean generates a glassfish xml but I also see people using DriverManager.getConnection methods. Which one is conventionally better?

I would try to separate the UI/JSP code from the DAO layer, using a seperate Bean to handle all the insert/uodate/delete operations.
Take a look at the logfiles of your Applicationserver, this might give you some hints.
Kr, R
Edit: Concerning the sql:
<sql:update var="counselorDelete" dataSource="jdbc/IFPWAFCAD"
sql="DELETE FROM Counselor WHERE counselor_id = ?" >
<sql:param value="${param.numId}"/>
</sql:update>

Related

Replace part of serialized data "resets" data to standard settings

I'm managing a couple of hundreds of websites and need to change part of a serialized data.
It's a wordpress child theme and inside of the theme's "Options" settings.
Using this script
UPDATE wp_e4e5_options
SET option_value = REPLACE(option_value, 'Copyright | by <a href="https://company.com"', ' ')
a:1:{s:3:"copyright";s:17:"Copyright | by <a href="https://company.com"";}
I was certain, it would just find that part of the serialized data and replace. But it doesn't.
It reset the setting to the theme's standard setting. Even when I edit it manually by using Adminer.php in the table, it resets.
I'm aware, that this might be in the wrong forum, since it's Wordpress related, but I believe it's SQL that's the issue here.
So my question is:
If i edit it manually using Adminer.php (simple version of
phpMyAdmin), it resets all the settings back to standard. How can I
edit only part of the serialized data and only the part shown above?
What makes it "reset" to standard settings?
UPDATE:
Thanks to #Kaperto I got this working code now, which gave me a new issue.
UPDATE wp_e4e51a4870_options
SET option_value = REPLACE(option_value, 's:173:"© Copyright - Company name [nolink] | by <a href="https://company-name.com" target="_blank" rel="nofollow">Company Name</a>";', 's:40:"© Copyright - Company name [nolink]";')
The problem is, it's gonna be used as a code snippet with ManageWP looping through several hundreds of websites which all have different company names. So the first part of the string is unique but the rest is the same on all sites, after the pipe |.
So I somehow need to do this:
Find whole series where this string is included | by <a href="https://company-name.com" target="_blank" rel="nofollow">Company Name</a>"
Get the whole series s:173:"© Copyright -
Company name [nolink] | by <a href="https://company-
name.com" target="_blank"
rel="nofollow">Company Name</a>
Replace with new series with updated character count, since company name is different
Is this even achievable with pure SQL commands?

save entity-find results and iterate throught it later

I'm doing a conditional entity-find and want to save the results somewhere to iterate through it later. I searched around and found 2 solutions. I'm using mySql btw.
1-create a temp table and insert results into it
2-saving results in a file (csv, ...)
now my question is:
1-which solution is preferable or maybe suggest another solution (common/trusted pattern)
2- how to do it? (for example I don't know how to create a table on the fly or drop it in moqui.Suggesting a resource/example source code etc. would be awesome)
thanks in advance
One possible approach would be to convert the list that your entity-find gives you to binary data and store it as a DbResourceFile using the moqui.resource.DbResource entities, and using the utilities from org.apache.commons.io etc.
EDIT - To expound in response to comment below, I was thinking along such lines as
<set field="fileData" from="yourList.toString().getBytes()" />
<service-call name="create#moqui.resource.DbResource" in-map="[filename:'ExampleListFrom01012019.bin', isFile:'N']" out-map="context" />
<service-call name="create#moqui.resource.DbResourceFile" in-map="context + [mimeType:'application/octet-stream', fileData:fileData]" />
Then bring it back after a find on the DBResource with something like
<set field="convertedBack" from="x.fileData.getBinaryStream()" type="NewList" />
I haven't tried this, and there are no near samples in the code that I know of.
This type of conversion between types is not best practice, but storing lists to iterate through them later is probably not either.
Perhaps it would help if you elaborated on your business requirement.

Is there a way to do string replacement/substitution in sql?

I have some records in a CMS that include HTML fragments with custom tags for a widget tool. The maker of the CMS has apparently updated their CMS without providing proper data conversion. Their widgets use keys for layout based on screen width such as block_lg, block_md, block_sm. The problem kicks in with the fact they used to have a block_xs and they have now shifted them all -- dropping the block_xs and instead placing a block_xl on the other end.
We don't really use these things, but their widget configurations do. What this means for us is the values for each key are identical. The problem occurs when the updated CMS code is looking for the 'block_xl' in any widget definition tags, it can't find it and errors out.
What I'm thinking then is that the new code will appear to 'ignore' the block_xs due to how it reads the tags. (and similarly, the old code will ignore block_xl) Since the values for each are identical, I need to basically read any widget definition and add a block_xl value to it matching the value of [any one of] the other width parameters.
Since the best place order-wise would be 'before' the block_lg value, it's probably easiest to do it as follows:
Replace any thing matching posix style regex matching /block_lg(="\d+,\d+")/ with: block_xl="$1" block_lg="$1"
Or whatever the equivalent of that would be.
Example of an existing CMS block with multiple widget definitions:
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="The Album" classes="highlight-bottom modish greenfont font52 fontlight"
enable_fullwidth="0" block_ids="127" lazyload="0"
block_lg="127,12," block_md="127,12," block_sm="127,12," block_xs="127,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
<!-- Image Block -->
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="What’s Your Favorite Cover Style?"
classes="zoo-widget-style2 modish grey font26 fontlight"
enable_fullwidth="0" block_ids="126" lazyload="0"
block_lg="126,12," block_md="126,12," block_sm="126,12," block_xs="126,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
What I would prefer to end up with from the above (adding block_xl):
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="The Album" classes="highlight-bottom modish greenfont font52 fontlight"
enable_fullwidth="0" block_ids="127" lazyload="0"
block_xl="127,12," block_lg="127,12," block_md="127,12," block_sm="127,12," block_xs="127,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
<!-- Image Block -->
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="What’s Your Favorite Cover Style?"
classes="zoo-widget-style2 modish grey font26 fontlight"
enable_fullwidth="0" block_ids="126" lazyload="0"
block_xl="126,12," block_lg="126,12," block_md="126,12," block_sm="126,12," block_xs="126,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
I know how to do it in php and if necessary, I will just replace it on my local DB and write an sql script to update the modified records, but the html blocks can be kind of big in some cases. It would be preferable, if it is possible, to make the substitutions right in the SQL but I'm not sure how to do it or if it's even possible to do.
And yes, there can be more than one instance of a widget in any given cms page or block. (i.e. there may be a need for more than one such substitutions with different local 'values' assigned to the block_lg)
If anyone can help me do it in SQL, it would be greatly appreciated.
for reference, the tables effected are called cms_page and cms_block, the name of the row in both cases is content
SW

Retrieving sql data in iframes

I have a php script (ini.php), that open in an iframe inside of "main.php":
<iframe src='ini.php' style='width:650px;' frameborder='0' id="IDMain" allowtransparency="YES" scrolling="NO"></iframe>
The SQL does not retrieve the data in "ini.php".
But if I incorporate the code of "ini.php" in "main.php", without using an iframe, the query retrieves all the information.
I must have an iframe in order to change all the different scripts in the same iframe.
Any help here?
Thanks
In addition more information:
The main.php is in session after logged. Previously I had the whole frame repeated in each script, and everything was ok.main.php, ini.php, and so on. Each script with header, left, right, main and footer. To make it faster and easier I decide to make the main script called main.php, that with an included script called config.inc.php that has several functions, also connects to data base, to retrieve some data to the header, left , right and footer sides of the main area (iframe) where the others different scripts, just to opens inside of a iframe, maintaining the information around. Each script, (like ini.php), that runs in the iframe also include the config.inc.php file, to connect as well to database. But does not retrieve any information
The connection to database in config script is:
<$connect=mysql_connect ("localhost","database","password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database", $connect) or die ("'I cannot connect to the database "); >
The query in ini,php (and others) is:
`
if ($id) {
$id_session=$_SESSION['id_session'];
$namesession=$_SESSION['name_session'];
$sql="select * from data where id='$id_sessino' limit 1 ";
$result=mysql_db_query("database",$sql);
$regist=mysql_fetch_array($result);
$id=$regist["id"];
$id_session=$regist["id"];
$namesession=$regist["name_session"];
$country=$regist["country"];
And soo on…
}
`
I hope with is information it makes it clear what do I mean. Thank you
Check PHP help for mysql_db_query. Since you are not giving it any open db link, it tries to do some default magic, which likely fails, because, as I mentioned in the comment, the two scripts (in parent window and inside the iframe) are run separately and have no idea about each other *unless you are doing some synchronizing, that isn't shown in the code snippets. That means that the iframe script doesn't know anything about the connection you've opened in the parent page, and fails. Check your logs, you should see some E_WARNING complaints (if you are logging this level).
Also, as the documentation says, you might be better off with MySQLi or PDO_MySQL extensions. But the principle will remain the same - the script in questoin has to have an open connection to the database (and I'm not sure whether you can easily and safely transfer an open from one script to the other one, so you'll likely have to do it all in the iframe script).
Try two things, first I would change the MySQL_fetch_array part to
While($regist = mysql_fetch_array($result))
{
//Code goes here
}
Secondly try referring to the array vales as numbers rather than names, so regist['id'] would become regist[0]. They will be in the order the are in the database.
Also since you select your database when creating connection why not just use mysql_query("QUERY HERE"); Rather than mysql_db_query

ASP Search and Results in a single page

I have a single Classic ASP page that I wish to display a search form and the associated results.
When a user first comes to this page, I want to display a search form and the 10 latest properties. If a user decides to use the search form to retrieve more relevant properties, then i want the default 10 latest properties to be replaced with the users' paged search results.
I was wondering if this is possible/practical within the confines of one page and if so, does anyone have any hints on how i could best achieve this?
This is my preliminary code for such a page;
http://gist.github.com/188770
Once again, i'm currently having to patch an existing ASP site until I can redevelop it in something more modern like PHP.
Thank you for any help offered.
Neil.
It's certainly very possible and practical. Typically the solution is to postback to yourself and have code in the page that detects if you arrived there from a post or a get. Get meant show the 10 latest properties, post means you do a search and show the results.
if (Request.ServerVariables("REQUEST_METHOD") = "POST") then
' arrived via post, get form values and do search
else
' arrived via get, show last 10 results
end if
You probably want to display what the user searched for in the form when you display the result:
<label>Street: <input type="text" name="searchStreet" value="<%=Server.HtmlEncode(Request("searchStreet") & "") %>" /></label>
Adding a empty string is for casting to string to not give an error when the key wasn't found, eg. on first visit.
If you want to you can make the loop prettier:
do until myRecordSet.EOF
%>
<div class='result'>")
<dl><%=myRecordSet("ContentTitle")%><dl>
<dt><%=myRecordSet("ContentStreet")%><dt>
<dt><%=myRecordSet("ContentTown")%><dt>
<dt><%=myRecordSet("ContentPostcode")%><dt>
</div><%
myRecordSet.MoveNext
loop
You probably want to Server.HtmlEncode there as well...
(ps ASP is actually one year younger than PHP... if you want something modern you might want to look at python, ruby or asp.net mvc before PHP, as it's easier to write bad code in PHP than in any of those. ds)