how do i delete a single record in database? i tried with Ajax and it didn't work.here is my code,each row has a unique number ID,so i need that to delet it,but i don't know how to get it.
echo '<tr>';
echo '<td>';
echo '<a href="korisnik.php?id='.$session_poster_id.'">';
echo '<div class="profile_pic_div" style="margin-top:10px;">';
echo "<img src='$avatar' style='width:40px;height:40px;'>";
echo '</div>';
echo '</a>';
echo '<div class="timestamp" style="margin-left:50px;margin-top:-43px;font-size:15px;">';
echo $user['vrijeme'];
echo '</div>';
echo '<a href="delete.php?post_id='.$post_id.'">';
echo '<div class="icon-x" style="margin-left:550px;margin-top:-15px;">';
echo '</div>';
echo '</a>';
echo '<div class="post_div" style="margin-top:30px;">';
if (strlen($user['post']) > 500) {
$user['post'] = substr($user['post'], 0, 500);
$user['post'] = substr($user['post'], 0, strrpos($user['post'], ' ')).'... Read More';}
echo $user['post'];
echo '</div>';
echo '<br>';
echo 'Komentiraj';
echo '</td>';
echo '</tr>';
DELETE FROM Name_of_your_table
WHERE Columname = your_value;
see w3schools delete query
PS. What is in your delete.php ?
Related
I have something like this
while($row = $result->fetch(PDO::FETCH_ASSOC)){
echo '<div class="container">';
$i=0;
while($i<3){
echo '<div class="row">';
echo '<div class="col-sm-4">';
echo '<div class="panel panel-primary">';
echo '<div class="panel-body"><img src="';
echo $row['imageurl'];
echo '"class="img-responsive" style="width:250px" style="height:250px" alt="Image"></div>';
echo '<div class="panel-footer">';
echo $row['name'];
echo '</div></div></div>';
$i++;
}
}
I want to click the image so that it calls a new PHP file with the image id.
To view the output http://csedu.cf/test.php
Try to put your image tag in a tag.
while($i<3)
{
echo '<div class="row">';
echo '<div class="col-sm-4">';
echo '<div class="panel panel-primary">';
echo '<div class="panel-body"><a href="test.php?id=image_id"><img src="';
echo $row['imageurl'];
echo '"class="img-responsive" style="width:250px" style="height:250px" alt="Image"></div>';
echo '</a><div class="panel-footer">';
echo $row['name'];
echo '</div></div></div>';
$i++;
}
I'm building a php/sql crud.
sql query select: user = user id is not working...
What sql statement do i use to show only the users account info?
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($users as $user) {
echo '<tr>';
echo '<td>';
echo $user['id'];
echo '</td>';
echo '<td>';
echo $user['username'];
echo '</td>';
echo '<td>';
echo '<a href="detail.php?id=';
echo $user['id'];
echo '">';
echo 'detail';
echo '</a> ';
echo '<a href="bewerk.php?id=';
echo $user['id'];
echo '">';
echo 'bewerk';
echo '</a> ';
echo '<a href="delete.php?id=';
echo $user['id'];
echo '">';
echo 'delete';
echo '</a>';
echo '</td>';
echo '</tr>';
}
?>
This code was working perfectly and is retrieving data from my Mysql database. I had a little issue with the path pointing to this script. I am having issues with the hyperlink with the href code line. I have a field in my database that is labled fulltext . I am trying to create a script that allows me display the content of fulltext (echo "{$row['fulltext']}.";) when I click on the Read More button. The hyperlink should be populated with the echo "{$row['title']}.";
What mistake am I making by inserting a href="fulltext.php?=$row['fulltext']
fulltext.php contains htmlspecialchars($_GET["fulltext"]);
<table>
<?php
$dbhost = 'localhost';
$dbuser = 'myusernm';
$dbpass = 'mypwd';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT title, introtext, created, created_by, catid FROM mydb_items';
mysql_select_db('muslimtimes360');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo '<tr>'; echo '<td>'; echo '<span class="post-date">'; echo "{$row['created']}."; echo '</span>'; echo '</td>'; echo '</tr>';
echo '<tr>';
echo '<td>'; echo '<h2 class="blog-post-title">'; echo "{$row['title']}."; echo '</h2>'; echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'; echo '<p>'; echo "{$row['introtext']}."; echo '</p>'; echo '</td>'; echo '</tr>';
echo '<p>'; echo '<tr>';
echo '<td>'; echo '<a href="fulltext.php?=$row['fulltext']>'; echo '<input type="button" value="Read More" />'; echo '</a>'; echo '</td>';
echo '</tr>';
echo '</div>';
echo '<div class="blog-meta">';
echo '<img src="img/avatar.png" alt="Avatar" />';
echo '<tr>'; echo '<td>'; echo '<h4 class="blog-meta-author">'; echo "{$row['created_by']}."; '</h4>';
echo '<span>'; echo 'Category:'; echo "{$row['catid']}."; echo '</span>'; echo '</td>'; echo '</tr>';
echo '</table>';
}
echo "";
mysql_close($conn);
?>
First of all, you aren't putting the variable into the string properly. Variables are only expanded inside double quotes, not single quotes. And the quotes inside the array index are terminating the string after echo.
Second, you need to give a parameter name before = in the URL.
Third, you were missing the closing double quote for the href attribute.
echo '<td>'; echo ''; echo '<input type="button" value="Read More" />'; echo ''; echo '</td>';
Change the line:
echo '<a href="fulltext.php?=$row['fulltext']>'
to
echo '<a href="fulltext.php?'.$row['fulltext'].'">'
I had retrieve some result from database and want to display the result separately.
If the pb_title is same as temp_title then put it togather in a red color div
else is pb_title is not same as temp_title then put it in grey color div
Here my tried code :
$select_brand = "SELECT * FROM tblProduct_Brand WHERE pb_display='display'";
$result = mysqli_query ($mydatabase, $select_brand);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$pb_feature_image = substr(($row['pb_feature_image']),3);
$pb_logo = substr(($row['pb_logo']),3);
$temp_result;
if($temp_result == $row["pb_title"])
{
//if product brand title is same, put it in same area
echo '<div style="padding:20px; background-color:red;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"];
}
else
{
//if not same, put in another area
echo '<div style="padding:20px; background-color:grey;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
}
}
Any mistake about the code please correct me.
Thanks.
The following code assumes that you did something like this with the data you fetched from the sql query:
$data = array();
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
Which returns an array like this:
Array (
[0] => Array(
'pb_title' => '',
'pb_content' => '',
//...etc
),
//.....etc
)
Now assuming that I understand your question correctly, you'd want to do something like this:
$i = array();
$key = 'A';
foreach ($data as $item) {
if ($item['pb_title'] == $key) {
$i['red'][] = $item;
} else {
$i['grey'][] = $item;
}
}
Which gives you an array of red (Matching) and grey (Not Matching).
Which you can loop through and generate your div's appropriately.
Something like this (pseudo code)
echo '<div style="padding:20px; background-color:red;">';
foreach($i['red'] as $row) {
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
}
echo '</div>';
Example
$temp_result;
if($temp_result == $row["pb_title"])
Before checking the condition you should have assigned some value to $temp_result , but you haven't and you are comparing it anyway.
EDIT :
What I understand is that initially you have not set any value of $temp_result and it is always coming in your Else Block where you do'not assign $temp_result = $row["pb_title"]; , So that it shoudl store the previous value , you only did it in the If Block , you need to place the same code in your else block too..
Simple Edit this section in your code
else
{
//if not same, put in another area
echo '<div style="padding:20px; background-color:grey;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"]; // added this to save previous result
}
Make sure that programing should be effective for that you need to have following ideas:
1) Here you no need to use if($result) because when the sql results are true then only it will allow inside the condition
2) While programing try to avoid more if else condition for making efficient.
<?php
$select_brand = "SELECT * FROM tblProduct_Brand WHERE pb_display='display'";
$result = mysqli_query ($mydatabase, $select_brand);
while($row = mysqli_fetch_array($result))
{
$pb_feature_image = substr(($row['pb_feature_image']),3);
$pb_logo = substr(($row['pb_logo']),3);
$temp_result;
if($temp_result == $row["pb_title"]) { $style = 'style="padding:20px; background-color:red;"'; }
else { $style = ' style="padding:20px; background-color:grey;"'; }
echo '<div '.$style.' >';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"];
}
?>
I want to load information from data base and show them as accordion.in my case accordian is working only for first set of data retrieves from the database but accordion functionality is not applied for the rest of the data.i'm new to this feature and i added simple accordion function.
$(function() {
$( "#accordion" ).accordion({
collapsible: true,
heightStyle: "content"
});
});
this is the part of php codeigniter code
echo '<div class="fieldinfio"><h4>BRANCHES</h4></div>';
echo '<span class="view_btn">Add Branch</span>';
echo '<div id="branches">';
foreach ($branch_details as $branch) {
echo'<h3>'.$branch->branch_name.'</h3>';
echo '<div id="accordion">';
echo '<h3>Branch Information</h3>';
echo '<div>';
echo '<p>';
echo '<table class="datatable">';
echo '<tr><td width="200">Branch Name: </td>';
echo '<td>'.$branch->branch_name.'</td></tr>';
echo '<tr><td width="200">Account Type: </td>';
echo '<td>'.$branch->account_type.'</td></tr>';
echo '<tr><td width="200">Postcode: </td>';
echo '<td>'.$branch->postcode.'</td></tr></table>';
echo '</p>';
echo '</div>';
echo '<h3>Default Price</h3>';
echo '<div>';
echo '<p>';
if($default_price[$branch->id]) {
} else {
echo '<p>Not Set</p>';
}
echo '</p>';
echo '</div>';
echo '<h3>Training Locations</h3>';
echo '<div>';
echo '<p>';
echo '<span class="view_btn">Add Training Location</span>';
foreach ($location_details[$branch->id] as $location) {
echo '<span>'.$location->name.'</span>';
echo '<table class="datatable">';
echo '<tr><td width="200">Name: </td>';
echo '<td>'.$location->name.'</td></tr>';
echo '<tr><td width="200">Postcode: </td>';
echo '<td>'.$location->postcode.'</td></tr></table>';
}
echo '</p>';
echo '</div>';
echo '</div>';
}
echo '</div>';
It looks like the branch information is inside a loop, which will create multiple elements with the id accordion.
ID of an element must be unique in a document, so in your case use class attribute instead like
echo '<div class="accordion">';
echo '<h3>Branch Information</h3>';
then
$(function () {
$(".accordion").accordion({
collapsible: true,
heightStyle: "content"
});
});