Deleting single item with submit from database results in two ID's and strings - sql

When I try and delete an entry from the code below it seems to spew out two array values with different index numbers and will not delete the entry. If I click multiple times it finally gets the correct ID but only by some random chance.
Using echo var_dump($deleted) results in something like:
String(2) "10"
String(1) "7"
These can vary and seems to be random.
I have reduced my whole plugin to just the code below thinking that maybe some other submit query might be interfering with it. And still I get two ID's with unknown string assignments.
Any ideas?
function myplugin_update_page(){
global $wpdb;
$update_page_list = $wpdb->get_results('SELECT * FROM wp_update_page');
$active = $wpdb->get_var("SELECT * FROM wp_update_page WHERE active='on'");
echo '<form method="post" action=""><table>';
foreach ( $update_page_list as $update ) { ?>
<tr>
<td><input type="image" src="<?php echo plugin_dir_url(__FILE__).'images/delete.png'; ?>" name="delete[]" value="<?php echo $update->ID; ?>" /></td>
</tr>
</table>
</form>
</div>
<?php
}
if(isset($_POST['delete'])) {
$delete = $_POST['delete'];
foreach ($delete as $deleted) {
$wpdb->query("DELETE FROM wp_update_page WHERE ID = $deleted");
}
echo "<script type='text/javascript'>window.location=document.location.href;</script>";
}
}

Related

Insert Multiple Rows in Laravel (Image Upload Issues)

In my application, I have faced several issues when I try to insert multiple rows of data into a database in laravel 8. Basically this issues is related to image upload. I have add related code to get help.
Controller View:
foreach($request->param_img as $key => $param_img){
$data = new PortfolioParam;
$data->param_desc = $request->param_desc[$key];
if($request->hasfile('param_img')){
$file = $request->file('param_img');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('upload/portfolio_param/', $filename);
$portfolio->param_img = $filename;
}
$data->portfolio_id = $portfolio->id;
$data->save();
}
Blade View:
<td class="pl-0 border-0">
<textarea class="form-control" name="param_desc[]" id="" cols="30" rows="3"></textarea>
</td>
<td class="border-0">
<input type="file" class="form-control" name="param_img[]">
</td>
Error
Call to a member function getClientOriginalExtension() on array
How can I get rid of this problems??
You have looped through the image but you have not used it in your image uploading code. You are still using the request data. Check this out, hopefully it helps.
// $file = $request->file('param_img');
$file = $param_img;
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('upload/portfolio_param/', $filename);
$portfolio->param_img = $filename;

How to display user_nicename and usermeta values by custom query in Wordpress?

I am trying to display user_nicename and one of usermeta values where meta_key is description. My query is given below:
<?php
$querystr = "
SELECT *
FROM $wpdb->users, $wpdb->usermeta
WHERE $wpdb->users.ID = $wpdb->usermeta.user_id
AND $wpdb->usrmeta.meta_key = 'description'
";
$retrieve_data = $wpdb->get_results($querystr, OBJECT);
?>
<?php global $retrieved_data; foreach ($retrieve_data as $retrieved_data){ ?>
<table>
<tr>
<th>Customer Name</th>
<th>Customer Details</th>
</tr>
<tr>
<td><?php echo $retrieved_data->user_nicename;?></td>
<td><?php echo $retrieved_data->meta_value;?></td>
</tr>
</table>
<?php } ?>
My final output should be displayed inside a table which will look something like below:
But I am in confusion how to do that. I am facing problem for both sql syntax and displaying data inside the table. Sorry for this easy question, I am still a learner. Can anyone help me in here? Thanks

Auto navigate (scroll) to certain table row

I have a table with few thousand records on few pages in a simple html table.. I made a search function that works fine apart from one thing... It displays only one result in a table (which is great cause it means it works!).But... I was wondering is there a way to display back the table with all records, with the one that i was searching for in the middle and highlighted? Here's a simplified table that I have :
<table class="nogap" cellpadding="1" bgcolor="#00000" cellspacing="1" style="margin:110px 0 0 5px; width:100%; border-color:#B6D6F6;" >
<tbody>
<?php include 'dbconn.php';?>
$con = mysqli_connect($host,$user,$pass,$db) or (header( 'Location: errorpage.php' ));
if (mysqli_connect_errno($con)) { header( 'Location: errorpage.php' ); }
$sql = "SELECT * FROM $tb1 ORDER BY (Serial_num +1) LIMIT $offset, $rowsperpage";
$result = mysqli_query($con, $sql) or (header( 'Location: errorpage.php' ));
$row = mysqli_num_rows($result);
while ($row = $result->fetch_assoc())
{
$product = $row['Prod_type'].$row['Serial_num'];
<tr id="mstrTable" class="lovelyrow">
<td width="5%"><?php echo $product;?></td>
<td width="5%"><?php echo $row['Customer'];?></td>
<td width="7%">
<a href="#"
onmouseover="ajax_showTooltip(window.event,'getptn.php?prd=<?php echo $p;?>',this);return false"
onmouseout="ajax_hideTooltip()">
<?php echo$row['Prod_info'];?>
</a>
</td>
</tr>
}
</table>
Thanks!
First of all don't give the same html id attribute to each row (mstrTable). Html Ids should be unique per page.
Instead mark table rows with unique ids, eg:
$html .= "<td id='row_".$row['id']."'>"
Then do a search query first, remember item id, figure out what page should be queried, query the whole page, attach classess 'greyedout' and 'highlighted' to rows accordingly and then you might try this javascript function to scroll down to the item:
https://developer.mozilla.org/en-US/docs/Web/API/element.scrollIntoView

virtuemart 2 product pdf does not display custom fields

I'm using VirtueMart 2.0.10 with Joomla! 2.5.6 and my problem is the autogenerated pdf documents for each product don't include my custom fields. I'm no php expert but I think theese lines in file /components/com_virtuemart/views/productdetails/tmpl/default_pdf.php has something to do with it, starting at line 145:
<?php // Product custom_fields TODO relation to Childs
if (!empty($this->product->customfields)) { ?>
<div class="product-fields">
<?php
$custom_title = null ;
foreach ($this->product->customfields as $field){
?><div style="display:inline-block;" class="product-field product-field-type-<?php echo $field->field_type ?>">
<?php if ($field->custom_title != $custom_title) { ?>
<span class="product-fields-title" ><strong><?php echo JText::_($field->custom_title); ?></strong></span>
<?php //echo JHTML::tooltip($field->custom_tip, $field->custom_title, 'tooltip.png');
} ?>
<span class="product-field-display"><?php echo $field->display ?></span>
<span class="product-field-desc"><?php echo jText::_($field->custom_field_desc) ?></span>
</div>
<?php
$custom_title = $field->custom_title;
} ?>
</div>
<?php
} // Product custom_fields END ?>
I tested adding an else statement echoing some text after the above if statement and it executetd. So apperently there are no custom fields... but there really are...
I haven't found anyone else experiencing this problem wich I think is weird, but I don't think I have screwed something up. I HAVE made a few changes in the /components/com_virtuemart/views/productdetails/tmpl/default.php file.
I removed all the code that I entered in my question and replaced it with the following code from the file /components/com_virtuemart/views/productdetails/tmpl/default.php:
<?php
$custom_title = null;
foreach ($this->product->customfieldsSorted['normal'] as $field) { // I set the position to normal
if ( $field->is_hidden )
continue;
if ($field->display) {
?>
<?php if ($field->custom_title != $custom_title) { ?>
<b><?php echo JText::_($field->custom_title); ?></b><br />
<?php
if ($field->custom_tip)
echo JHTML::tooltip($field->custom_tip, JText::_($field->custom_title), 'tooltip.png');
}
?>
<?php echo $field->display ?><br />
<?php echo jText::_($field->custom_field_desc) ?><br />
<?php
$custom_title = $field->custom_title;
}
}
?>
I'm no expert but this worked for me. The PDF now includes the custom fields.
As you see in the comment in the code I changed the position to 'normal'. The other positions seems to be 'ontop' and 'onbot'. But I won't change that setting so I leave it be.
EDIT: I forgot to mention I added some other html-code to that segment, like <br /> and <b> just for apperences. So nothing major, just to clarify that the code isn't EXACTLY like it is in the file default.php

Show variable outside while loop

Hy. I know there are answers similar, but i can't get it going. Here is my cod:
<?php include 'header.php'; ?>
<?php include 'connect.php';
$q = mysqli_query($link, $aaa = 'SELECT * FROM agentii LEFT JOIN orase ON agentii.oras=orase.oras_id WHERE orase.oras_id = \'' .$_GET['id']. '\'') or die(mysqli_error($link));
$row = mysqli_fetch_assoc($q);
?>
<h2>Title <em><?php echo $row['oras'];?></em></h2>
<div class="div_view">
<table cellspacing="0" cellpadding="5" border="0" width="100%">
<tr>
<th>Agentie</th>
<th>Adresa</th>
</tr>
<?php while ($row = mysqli_fetch_assoc($q)) { ?>
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
<?php }?>
</table>
</div>
<?php include 'footer.php'; ?>
This output(i can't put images because is a new account - so i put a link to a prt src):
http://postimage.org/image/w9zsexz91/
In my database i have 3 rows, and the code outputs only 2.
I know that is because of the 2 mysqli_fetch_assoc, but i want to show all the rows, and i want to show the title too (<?php echo $row['oras'];?>) outside the while.
Can someone please help me with this?
Thx in advance!
obligatory warning first: Remember little Bobby Tables!
(or: your code is vulnerable to sql injection attacks)
then: you get only two rows in the while loop, because you requested a row outside the loop first, thus increasing the internal cursor in the result.
If you want to get title and rows in one go, you need to cache the result, for example:
$resultArray = array();
while ($row = mysqli_fetch_assoc($q)) {
$resultArray[] = $row;
}
//....
?>
<h2><?php echo $resultArray[0]['oras']; ?></h2>
<?php // ... ?>
<?php foreach($resultArray as $row) {
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
} ?>
this is just a stub, of course you should implement checks if you got a row at all etc...
Try:
<?php do { ?>
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
<?php } while ($row = mysqli_fetch_assoc($q)); ?>
The first iteration of this loop will print the first fetched row. I have only changed the location of the while clause, forcing the code of the loop to be run before a new row is fetched.