Displaying table from MySQL and link to delete each row from database - php

I've stucked a bit when it comes to a small piece of my PHP code. The role of this script is to display whole table from mysql plus adding to each row a hyperlink to delete such row.
<?php
$connection=mysql_connect('localhost','root','') or die(mysql_error());
error
mysql_select_db('localhost_db',$connection) or die(mysql_error());
$query=mysql_query("SELECT * FROM cms_contest") or die(mysql_error());
if(mysql_num_rows($query)>0):
?>
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php
while($row=mysql_fetch_object($query)):?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">REMOVE</td>
</tr>
<?php endwhile;?>
</table>
<?php
else: ?>
<h3>No Results found.</h3>
<?php endif; ?>
It would be perfect if there appear a short javascript popup with question like: are you sure you want to remove this entry? OK / Cancel.
I have no clue how to do it.. thanks for any tips!

Simple/stupid method:
<td>nuke me</td>
But if a web spider or a browser link pre-fetch tool gets loose on this page, you'll be nuking ALL of your records.
Somewhat better:
<td>
<form method="post" action="deleteme.php">
<input type="hidden" name="id" value="<?php echo $row->ID ?>" />
<input type="submit" value="Nuke me" />
</form></td>
And then there's various options involving radio buttons/checkboxes, or JS to trap the click-on-the-link etc...
But, in the end, they ALL boil down to "you have to sent the ID of the row back to the server". How you go about that is up to you... just don't use the plain "click here" version.

Indeed there are several different ways to do this. One way I like is to wrap the entire table in a form that submits to the delete script, and use a button for each row with the row ID as its value.
<form method="post" action="delete.php">
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php while($row=mysql_fetch_object($query)): ?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">
<button name="delete-id" type="submit" value="<?php echo $row->ID; ?>">
REMOVE
</button>
</td>
</tr>
<?php endwhile;?>
</table>
</form>
There are also many different ways to confirm the deletion. The simplest I know of on the client side is to add onclick="return confirm('Are you sure?');" to the delete button.

click event:
DELETE
<script type="text(javascript">
function confirmDelete(id){
if(confirm('sure u want to delete entry with id: ' + id + '?')){
window.location.href = "ursite.php?id="+id+"&delete=true";
}
}
</script>
PHP
if(isset($_GET['delete'])){
$stmt = "DELETE FROM cms_contest WHERE ID = ".$_GET['id'];
mysql_query($stmt);
}
it's the worst way to do it.
I recommend you to look at:
jQuery $.post()jQuery $.ajax()
I hope it will help you.

Related

Reformatting a result from a json file

I have a json file I am getting returned from an API. I have learnt how to format it and lay it out in PHP but one bit I can't work out is how to reformat the date that is returned.
This is the page that displays the results:
<body>
<h1 align="center"><font face ="Arial"><u><br>Results<br><br></u>Postcode - <?php echo $resultData['postcode']; ?></font></h1>
<br>
<br>
<h2 align="center">Known Floor Sizes Listed By Report Date</h2>
<br>
<table class= "general" align= "center" bgcolor="#ffffff">
<tr>
<td align="center" rowspan="2"><b>Inspection Date</b></td>
<td align="center" rowspan="2"><b>Address</b></td>
<td align="center" colspan="2"><b>Size in</b></td>
</tr>
<tr>
<td align="center"><b>SqM</b></td>
<td align="center"><b>SqFt</b></td>
</tr>
<?php foreach ($resultData['known_floor_areas'] as $property) { ?>
<tr>
<td align="center"><?php echo $property['inspection_date']; ?></td>
<td align= "left" ><?php echo $property['address']; ?></td>
<td align="center"><?php echo (round($property['square_feet'] * 0.092903,0));?></td>
<td align="center"><?php echo $property['square_feet']; ?></td>
</tr>
<?php } ?>
</table>
</body>
This displays the date in the format: 2015-06-22T23:00:00.000000Z
I want to change the format to a basic: 2015-06-22
I'm sure it's simple but after a lot of searching I'm going round in circles.
Use DateTime() then format() it:
<td align="center"><?php echo (new DateTime($property['inspection_date']))->format('Y-m-d'); ?></td>

Hide Row if value in checkbox greater than zero

I have a table that is using calculated fields coming from 3 different tables in 2 distinct databases. I am trying to use a checkbox to hide the rows that have the calculated field greater than zero or students who owe money. I have added a checkbox with a value of zero.
<form id="formFilter" name="formFilter" method="post" action="">
<p>
<input name="selView" type="checkbox" id="selView" onChange="formFilter.submit()"
value="0" <?php echo (isset($_POST['selView'])?'checked':'');?> />
view only students with a balance<br />
</p>
</form>
I need to evaluate $balancez and if $balancez is greater than zero, when I click the checkbox I want the rows of students who do not have a number greater than zero to hide. This way I can only view the students who have a balance owed.
<table width="800" border="0" class="TFtable sortable">
<tr>
<td scope="row"><strong>Students</strong> (<?php echo $totalRows_Recordset1 ?>)</td>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Division</strong></td>
<td align="center"><strong>AP Fee</strong></td>
<td align="center"><strong>Credit Card Payment</strong></td>
<td align="center"><strong>Store Payment</strong></td>
<td align="center"><strong>AP Balance</strong></td>
</tr>
<?php do { ?>
<tr>
<td scope="row"><?php echo $row_Recordset1['Student']; ?></td>
<td align="center"><?php echo $row_Recordset1['ID']; ?></td>
<td align="center"><?php echo $row_Recordset1['Division']; ?></td>
<td align="center"><?php echo "$".$row_Recordset1['Total']; ?></td>
<td align="center"><?php
mysql_select_db($database_cc, $cc);
$studentid = $row_Recordset1['ID'];
$query_Recordset_CC = 'SELECT credit_card_activity.Student_ID, SUM(credit_card_activity.CC_Amount) AS total_cc2, credit_card_activity.Ref_ID, credit_card_activity.Settle_Date FROM credit_card_activity WHERE credit_card_activity.Ref_ID LIKE "AP%" AND credit_card_activity.Student_ID = "'.$studentid.'" GROUP BY credit_card_activity.Student_ID';
$Recordset_CC = mysql_query($query_Recordset_CC, $cc) or die(mysql_error());
$row_Recordset_CC = mysql_fetch_assoc($Recordset_CC);
$totalRows_Recordset_CC = mysql_num_rows($Recordset_CC);
$total_cc =-1*(0+$row_Recordset_CC['total_cc2']);
echo ($total_cc < 0 ? "($".abs($total_cc).")" : "$".$total_cc);
?></td>
<td align="center"><?php
mysql_select_db($database_cc, $cc);
$studentid = $row_Recordset1['ID'];
$query_Recordset_Store = "SELECT checkout.student_id, SUM(`transaction`.total_amount) AS total_store2 FROM checkout, `transaction`, school_store WHERE `transaction`.activity_id=school_store.Tag AND `transaction`.transaction_id=checkout.transaction_id AND (`transaction`.refund_status='0' OR `transaction`.refund_status='1') AND school_store.Activity LIKE 'AP Fee%' AND checkout.student_id='".$studentid."' AND checkout.payment_type NOT IN ('Cash Refund') GROUP BY checkout.student_id";
$Recordset_Store = mysql_query($query_Recordset_Store, $cc) or die(mysql_error());
$row_Recordset_Store = mysql_fetch_assoc($Recordset_Store);
$totalRows_Recordset_Store = mysql_num_rows($Recordset_Store);
$total_store = -1*(0+$row_Recordset_Store['total_store2']);
echo ($total_store < 0 ? "($".abs($total_store).")" : "$".$total_store);
?></td>
<td align="center"><?php
$ap_fees=$row_Recordset1['Total'];
$ap_cc=$row_Recordset_CC['total_cc2'];
$ap_store=$row_Recordset_Store['total_store2'];
$paid=$ap_cc + $ap_store;
$balancez=($ap_fees - $paid);
echo ($balancez < 0 ? "($".$balancez.")" : "$".$balancez);
?>
</td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
?>
</table>
What makes this task difficult for me is that it is not based on a mysql query but on a calculated value ($balancez) based on other queries so I can't use Having. Can someone lead me to a jquery that can evaluate $balancez of each row and hide or display the row if value from checkbox is met? I am also open to any method that will solve this issue. Many thanks.

Updating MySQL table with PHP and checkboxes in WordPress

On my website enter link description here I'm using checkbox buttons for updating data in a MySql table (pages are with login) and use this code in PHP that's working fine:
<?php
$sql="SELECT *,cast(DATE_FORMAT(datum,'%d-%m-%Y ') AS CHAR(32)) as datum FROM wedstrijden";
$result=mysqli_query($db,$sql);
$count=mysqli_num_rows($result);
?>
<table style="width: 100%;">
<tr>
<td><form name="frmactive" action="1fanionchange.php" method="post">
<tr>
<td colspan="6"><input name="activate" type="submit" id="activate" value="Open selected" />
</tr>
<tr>
<td> </td>
</tr><tr>
<td align="center"><!--<input type="checkbox" name="allbox" title="Select or Deselct ALL" style="background-color:#ccc;"/> --></td>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Datum</strong></td>
<td align="center"><strong>Uur</strong></td>
<td align="center"><strong>Thuis</strong></td>
<td align="center"><strong>Uit</strong></td>
</tr>
<?php
while($rows=mysqli_fetch_array($result)){
?>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td align="center"><? echo $rows['id']; ?></td>
<td align="center"><? echo $rows['datum']; ?></td>
<td align="center"><? echo $rows['uur']; ?></td>
<td align="center"><? echo zoeknaam($rows['thuisploeg']); ?></td>
<td align="center"><? echo zoeknaam($rows['uitploeg']); ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="6"><input name="activate" type="submit" id="activate" value="Open selected" />
</tr>
</form>
</td>
</tr>
</table>
But when I use the same code in WordPress (I'm trying to make the website in WordPress), it is not working and no records are displayed. When I try to find the number of rows using:
echo $count. "<br />";
I get the correct number of records in that table. But the records are not showed?
Any idea how to handle the problem? Thanks.
PS. I use the plugin phpexec in WordPress!
In WordPress it is suggested to use the WordPress builtin Queries. So that it can have maximum compatibility with the server too it is hosted.
Reference: https://codex.wordpress.org/Class_Reference/wpdb
global $wpdb;
And all the following functions will attach the database that is defined as global..
$wpdb->insert('wp_submitted_form', array(
'name' => 'Kumkum',
'email' => 'kumkum#gmail.com',
'phone' => '3456734567', // ... and so on
));
Like wise you can use it for Update / Select as per your wish and it is considered as the best one according to WordPress..

Create a user's link

I have Created a website that have a login and signup form,
now what I need is to create a profile link for everyone who signup in PHP, Such as in twitter and Facebook.
How do I create a profile link for each user in PHP?
if login is successfull then store user id in session variable and excute the query that will get the detail of the user from database and show the result... as simple as that and on any page if you want to get details of user use session variable and execute the query and display the result or you can do another thing as after login store user information in session and display details using sessions...i think this will help you if not fell free to ask again
Sample code
<?php
require_once('connection.php');
$id=$_SESSION['SESS_MEMBER_ID'];
$result3 = mysql_query("SELECT * FROM member where mem_id='$id'");
while($row3 = mysql_fetch_array($result3))
{
$fname=$row3['fname'];
$lname=$row3['lname'];
$address=$row3['address'];
$contact=$row3['contact'];
$picture=$row3['picture'];
$gender=$row3['gender'];
}
?>
<table width="398" border="0" align="center" cellpadding="0">
<tr>
<td height="26" colspan="2">Your Profile Information </td>
<td><div align="right">logout</div></td>
</tr>
<tr>
<td width="129" rowspan="5"><img src="<?php echo $picture ?>" width="129" height="129" alt="no image found"/></td>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $fname ?></td>
</tr>
<tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $lname ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Gender:</div></td>
<td valign="top"><?php echo $gender ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Address:</div></td>
<td valign="top"><?php echo $address ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Contact No.: </div></td>
<td valign="top"><?php echo $contact ?></td>
</tr>
</table>
<p align="center"></p>
Assuming you have a user id in your database for each user you could do something like this.
User Profile
Then get the id on user_profile.php using $_GET['id']. Once you have this query the database on id and do what you want with the data.

How can I use WHILE loop to get as many sets of data into a table as are available?

When we run a query in ssms, we can get one or more rows of data per associateID.
However, on a table data,
<table>
<tr>
<td>...</td>
</tr>
</table>
We believe that the solution to our problem is to loop the data and dump contents in a table.
Any ideas how I can accomplish this?
Here is the code I am trying to loop:
<?php
//I need the loop here.
<table>
<tr>
<td class="dataItem" id="SignCode"></td>
<td class="dataItem" id="SignType"></td>
<td class="dataItem" id="SignSize"></td>
<td class="dataItem" id="SignColor"></td>
<td class="dataItem" id="Facing"></td>
<td class="dataItem" id="HorizClear"></td>
<td class="dataItem" id="VertClear"></td>
<td class="dataItem" id="Angle"></td>
<td class="dataItem" id="ReflCoat"></td>
<td class="dataItem" id="Condition"></td>
<td class="dataItem" id="Status"></td>
</tr>
</table>
?>
The data is a combination of query and the Javascript array. The query is
$tsql="select * from mytable where associateId='$aid'";
Then all the form ids are in Javascript like this example:
dojo.byId("SignType").innerHTML = obj["SignType"];
If you show us your php code we can give you a better answer but your code should be somthing like this:
<table>
<?php
// Your loop
while($row = /*fetchData()*/) {
?>
<tr>
<td class="dataItem" id="SignCode"><?php echo $row['SignCode']; ?></td>
<td class="dataItem" id="SignType"><?php echo $row['SignType']; ?></td>
<td class="dataItem" id="SignSize"><?php echo $row['SignSize']; ?></td>
<td class="dataItem" id="SignColor"><?php echo $row['SignColor']; ?></td>
<td class="dataItem" id="Facing"><?php echo $row['Facing']; ?></td>
<td class="dataItem" id="HorizClear"><?php echo $row['HorizClear']; ?></td>
<td class="dataItem" id="VertClear"><?php echo $row['VertClear']; ?></td>
<td class="dataItem" id="Angle"><?php echo $row['Angle']; ?></td>
<td class="dataItem" id="ReflCoat"><?php echo $row['ReflCoat']; ?></td>
<td class="dataItem" id="Condition"><?php echo $row['Condition']; ?></td>
<td class="dataItem" id="Status"><?php echo $row['Status']; ?></td>
</tr>
<?php } ?>
</table>
The code should look something like this:
(It is still unclear how you acquire the data from the DB.)
<?php
$resultSet = ... // <-- somehow aquire a result-set you can loop through
?>
<table>
<?php while ($row = <somehow_get_next_row_from_$resultSet_as_associative_array>) {?>
<tr>
<td class="dataItem" id="SignCode"><?php echo($row["signCode"]); ?></td>
<td class="dataItem" id="SignSize"><?php echo($row["signSize"]); ?></td>
<td class="dataItem" id="SignColor"><?php echo($row["signColor"]); ?></td>
...
<td class="dataItem" id="Status"><?php echo($row["status"]); ?></td>
</tr>
<?php } ?>
</table>
In case the value for id (e.g. "SignCode" etc) is the same as the name of the DB column-name AND the query returns the columns in the order you want the HTML-table columns to be, then the part between <tr> and </tr> could be further simplified as:
...
<tr>
<?php forEach ($row as $key => $value) {?>
<td class="dataItem" id="<?php echo($key); ?>"><?php echo($value); ?></td>
<?php } ?>
</tr>
...

Categories