I have worked on this for a few days now.
This snippet finds the correct row(s) from the database. It works fine. However, when the Button is clicked, I only want for the comment it is next to targeted, not all the comments on that page. It's hard to explain.. Let me show you image.
What I am doing currently is selecting the database rows and outputting them for debugging, instead of removing them (I don't want to go around deleting sections of my database with broken code)
$db is mysqli connection to the database
Snippet of the while loop:
TL/TR
I want to do the following with this in the end:
Remove the comment from the database.
Give the feedback to an admin.
This may sound like a noob question, but please help me.
Many thanks!
Just can't wrap my head around this, made like 6 pages single-handed and now I am stuck in a thing this "easy" for a d**n week and i still can't get it right.
EDIT
Latest snippet (Still doing it, What the hell!?!)
$query = $db->query("SELECT * FROM comments WHERE post_id='$id'");
while($row = $query->fetch_object()){
echo "<h5>".$row->name."</h5>","<br>";
$strip_comment = strip_tags($row->comment);
$delComment = $row->comment_id;
$strip_comment_shlashes = stripslashes($strip_comment);
echo "<blockquote>".$strip_comment_shlashes,"<br><br></blockquote>";
//button stuff
if($is_admin){
$query1 = $db->prepare("SELECT comment_id FROM comments WHERE comment_id = '$delComment'");
$query1->execute();
$query1->bind_result($commId);
while($row2 = $query1->fetch()):
?>
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<input type="submit" class="closeButton" name="deleteComment" value="<?php echo $commId; ?>" />
</form>
<?php
if(isset($_POST['deleteComment'])&& $is_admin){
if($is_admin && $commId){
echo "Comment ID <b>$commId</b> Removed";
}
}
endwhile;
}
Edit: NON-OBJECT error..
$delComment_2 = $_POST['deleteComment'];
$query2 = $db->prepare("SELECT * FROM comments WHERE comment_id='$delComment_2'");
$query2->execute();
Edit 2: (31/10)
Please, Could someone fix this snippet and post it? I don't usually ask for working snippets, but this one is driving me crazy. I just am too noob to understand how this goes. Thanks.
Change the input to this:
<input type="submit" class="closeButton" name="deleteComment" value="$comment_id_here" />
And then just use the comment id to delete a specific one from the database. (check $_POST['deleteComment'])
Of course, there are numerous other ways to do this - but the point is the same: you need to pass the comment id (not the post id) from your page to the query string. Whatever way you use to accomplish that is up to you, I just gave you an example.
Related
The newbie is back with another question. Any help would be much appreciated. Suppose we have got a form in which we have written down the name of a user and in front of which there is an input box in which we can allocate a grade to the mentioned user. Within this scenario, everything is clear. We have a form with the name of user (it's 'id' as the value) and another variable, that is the grade' which are posted to the php-action-page. Hence, in the php-action-page, I get two variables, one is the id of the user and the other allocated grade, through POST. Here, everything is clear and the process easy, since I have got just two defined variables. Now, suppose that we are inserting a list of users from our 'Users' table into the form dynamically. We fill our form with for example 10 users grabbed from the database. In front of them there are input boxes for the 'grade' to be inserted into. So far, everything is fine. The problem, though, lies in the next stage. The problem is I don't know how to ask php-action-page to do the insert, that is insert the grade in the database for specific users as long as there are posted variables of users. Here I have tens of users and tens of dynamic variables. And if the question is a little bit vague, please do excuse me; yet, do your best to get me free from this condition of bafflement. Many thanks.
Here comes some bits of the code to make the problem a little more clear.
I start with the following code:
<?php
require_once ('../inc/takein.php');
$pd = new dbase();
$students = $pd->run_a_query('SELECT * from `checking`');
Here I am including the database and other necessary files. Then I run a query to fetch a list of my students from the table. So far, everything is fine. The next line of action which makes me perplexed is the following code.
Before having a look at the code may you please look at the html design in the following picture:
Final Design
I totally have no idea about it being wrong or correct. You might help with this bit as well.
<form action="grades.php" method="post">
<table class="table table-bordered font-label" id="item_table">
<?php
foreach ($students as $student) {
echo '<tr>';
echo '<td>'.$student['name'].'</td>';
echo '<td><input type="text" name="grade[]" class="form-control omit-radius-input"></td>';
echo '<input type="hidden" name="id[]" value="'.$student['id'].'">';
echo '<tr>';
}
?>
</table>
<input type="submit" name="dispatched" class="btn btn-green">
</form>
Here, I am putting the information in a table within the form element. As you can see in the above picture, I am getting four students from the database. Now I want to send these students back to the database along with their newly set grades. What I want to be posted here is the student id and their grades.
Then, the following is the last part of the code which is left incomplete because I couldn't make any senses how to do it.
if (isset($_POST['dispatched'])) {
$id[] = $_POST['id'];
$grade[] = $_POST['grade'];
// what to do now???!!!
foreach(...HOW TO DO THE 'FOREACH') {
...
}
}
May you please help me insert my student grades. Many thanks in advance.
Simply name your variables as arrays - if your form looks like this
<form method="POST">
<input type="text" name="grade[12]">
<br>
<input type="text" name="grade[15]">
<br>
<input type="text" name="grade[7]">
<br>
<input type="text" name="grade[21]">
<!-- continue here -->
</form>
then in your PHP code you will access the grades like this
if(is_array($_POST['grade'])) foreach($_POST['grade'] as $id => $value)
{
// REPLACE INTO user_grades(user_id, grade) VALUES($id, $value)
}
UPDATE
You should also put the ID of your students in the name of the INPUT field - otherwise you won't know for which student is the given grade.
<?php
foreach ($students as $student) {
echo '<tr>';
echo '<td>'.$student['name'].'</td>';
echo '<td><input type="text" name="grade['.$student['id'].']" class="form-control omit-radius-input" value="'.$student['current_grade'].'"></td>';
echo '<tr>';
}
?>
The foreach is shown above in my original answer.
People can order links at my platform and these get stored in two different tables, eg wp_project and wp_articles. The reason for this is that when people place an order for 1 link one project row gets created and one article row gets created.
However if a client orders 10 links there will be one project entry and ten article entries. Now this isn't very relevant to my problem but just to explain how the system works.
There are two common identifiers for projects and articles, both the ID and the project title are stored in both inpu_project and inpu_articles.
Now if I want to echo all the links from one project I built a search function where I can select the project ID, click search and whether a client ordered 1 link or 20 links that amount of links will show.
The problem starts when I want to search based on the project title instead of ID, which is non-numeric but just words (with spaces in between sometimes).
This to prevent the error undefined index in my error logs
$where_clouse='';
This is the search function based on the project title:
if(isset($_GET['todo']) && $_GET['todo']=='search')
{
if($_GET['proid']!=0){
$where_clouse.=' AND P.`project_title`='.$_GET['proid'];
}
}
This is the database query to pull the links from it:
$sql="SELECT P.`project_title`, P.`backlink` FROM `inpu_articles` AS P
WHERE P.`post_it_posted` = '1' ".$where_clouse." ORDER BY P.`project_title` ASC";
$backlinks=$wpdb->get_results($sql, ARRAY_A);
This is the actual search function that triggers the first code I pasted:
<p>Select a project title and hit search!</p>
<form action="" method="get">
<input type="hidden" name="todo" id="todo" value="search" />
<select name="proid" id="proid">
<option value="0">All Projects</option>
<?php $projects=getTitles(); foreach($projects as $project){ ?>
<option value="<?php echo $project['title']; ?>" <?php if(isset($_GET['proid']) && $_GET['proid']==$project['title'] ) { echo 'selected="selected"'; } ?> ><?php echo $project['title'] ?>
</option>
<?php } ?>
</select>
<input class="submit_search" type="submit" id="search_button" value="Search"/>
</form>
And this is the code that pulls the project titles from the project table:
function getTitles($where=false)
{
global $wpdb;
$sql="SELECT `title`, `link` FROM `inpu_project` WHERE `link`='1' ORDER BY `title` ASC";
$result = $wpdb->get_results($sql, ARRAY_A);
return $result;
}
All pretty straight forward and it works 100% fine if I replace the non-numeric title in the code for both the inpu_articles and the inpu_project tables with the numeric id.
Then the code does exactly what it does, but when I use the project title instead it just shows ALL of the links that every client ever ordered and no error shows up in my database. It's like it can't find a match or something, which is plain weird.
So am I missing something here? Is it not possible to match words or is there something that I should adjust when using words instead of numeric values?
Lastly, for what it's worth, this is the code that echos the query:
<?php foreach ($backlinks as $backlink) {
$backlink_overview=$backlink['backlink'];
?>
<ul>
<li><?php echo $backlink_overview; ?></li>
</ul>
<?php } ?>
The titles do show up in my form / search drop down menu so I am surprised it can't match the project title with the title that's stored in the article table.
My gut says this is where it goes wrong:
P.`project_title`='.$_GET['proid'];
But it doesn't throw any syntax errors or any errors whatsoever, it just shows all the links that are stored in the inpu_article table, ignorning my search function altogether.
The problem is in the condition for where_clause and the where_clause itself.
To check for input you need to check if the parameter is set and is not empty. Then for where clause you need to put the string into quotes. Change that part of the code like bellow
if(isset($_GET['proid']) && !empty($_GET['proid'])){
$where_clouse.=" AND P.project_title='".$_GET['proid']."'";
}
i think you can be wrong in SQL statement. And you can print the sql string statement to the screen and copy it into Mysql to check. I hope you can find your mistake.
I researched here in stackoverflow trying to find whether someone is also encountering the same problem. I know it's kind of easy and even I really don't know what's the error because there's no problem with my query.
On the previous page, here's my code to retrieve the ID Number so I'll be able to select the data with that ID number:
<?php echo $row['place_name'];?>
I tried first to print the value of the place id and it works fine.
But when it was being called to the Package page, the data I want to show weren't displayed.
I look at the URL and it shows this after the package.php
place_id=
I don't know why it is blank, please check my code if there's missing or just wrong.
In my package page, here's the PHP code:
<?php
include("common/connect.php");
$place_id = $_GET['place_id'];
$result = mysql_query("SELECT * FROM package_items WHERE place_id = '$place_id'");
$row1 = mysql_fetch_array(mysql_query("SELECT place_name FROM packages WHERE place_id = '$place_id'"));
if($result === FALSE) {
die(mysql_error()); // for better error handling
}
?>
In HTML Code:
<h1><?php echo $row1['place_name'];?></h1>
<?php while($row=mysql_fetch_array($result)) {?>
<?php echo $row['item_title'];?>
<br>
Back
<?php } ?>
Please check my codes. Thanks.
You are not printing it.
Change
<?php $row['place_id'];?> // It will output nothing as no echo or print.
To
<?php echo $row['place_id'];?>
Rest of the code looks fine.
Three suggestions:
1)
$place_id = $_GET['place_id'];
Change to
$place_id = ! empty($_GET['place_id']) ? $_GET['place_id'] : ''; // To avoid any warning.
2) Don't feed variable from $_GET or $_POST to any SQL.
3) Don't use mysql_ functions as they are deprecated and will be remove in future versions of PHP.
How do I Limit some information displayed from the database and add a link eg "More" to enable read all information in a drop down using PHP. such as what is on facebook (Read more...). I am dealing with a lot of content and I dont want it all displayed at once.
Here is part of the code
echo "<p>".$row['Firstname']." ".$row['Lastname']."</p>";
echo "<p>".$row["Course"]." | ".$row["RegID"]."</p>";
echo "<p>".$row["Email"]."</p>";
echo "<p>"."Tel:".$row["Telephone"]."</p>";
echo "<p>".$row["info"]."</p>";
The code is running well only that I want to limit the information
echo "<p>".$row["info"]."</p>";
so that not all is displayed
Thanks
Use Jquery-ui click on "view source" and you'll see it's very simple really, just set the row that you want as the header (what's clicked to show the rest) and store the rest in a div below.
Split info into two strings, one intro, and the rest. Display only the intro to begin with. Insert a link that displays the rest when clicked.
$intro = substr($row['info'], 0, 200);
$rest = substr($row['info'], 200);
echo sprintf(
<<<HTML
<p>
<span class="intro">%s</span><span class="rest" class="display: none">%s</span>
Show more
</p>
HTML
, htmlentities($intro)
, htmlentities($rest)
);
displayRest is a Javascript-function that, given a link, finds the previous span with class rest, shows it and removes the link. I leave it as an exercise to implement this in a way that fits your project. You can go with native Javascript, or use a library such as jQuery, YUI, MooTools, Prototype etc.
if(isset($_POST['more']))
{
$query="select col1,col2,col3, ... ,colN from tableName ";
}
else
{
$query="select col1,col2,col3 from tableName ";
}
//HTML
<form method="post">
<input type="submit" name="more" value="More" />
</form>
//PHP
$records=mysql_query($query);
while($row=mysql_fetch_assoc($records))
{
//Display
}
The limit must be fixed on the SQL request.
// If you want to transmit limitation with a GET PARAMETER.
// You can also $_POST ur data.
$limitation = $_GET['limit'];
//..... And in your SQL REQUEST
$sql = "SELECT * FROM your_table LIMIT 0 , $limitation";
//And in the link....
echo 'Show only 10 Results'
?>
You can optimize that and add security precaution to prevent errors when $limitation receive empty or non numeric parameters.
<?php if(isset($_GET['limit']) && !empty($_GET['limit']) && !preg_match(EXPRESSION, $_GET['limit'])){
//YOU CAN DO THE LIMITATION WHITOUT SQL ERRORS
}
else{
//ERROR DIRECTIVE
}
?>
I am trying to write a custom script that will keep a list of strings in a textarea. Each line of the textarea will be a row from a table.
The problem I have is how to work the script to allow for adding, updating, or deleting rows based on a submit.
So, for instance, I currently have 3 rows in the database:
john
sue
mark
I want to be able to delete sue and add richard and it will delete the row with sue and insert a row for richard.
My code so far is as follows:
To query the db and list it in the textarea:
$basearray = mysql_query("SELECT name FROM mytable ORDER BY name");
<textarea name="names" cols=6 rows=12>');
<?php
foreach($basearray as $base){
echo $base->name."\n";
}
?>
</textarea>
After the submit, I have:
<?php
$namelist = $_REQUEST[names];
$newarray = explode("\n", $namelist);
foreach($newarray as $name) {
if (!in_array($name, $basearray)) {
mysql_query(DELETE FROM mytable WHERE word='$name'");
} elseif (in_array($name, $basearray)) {
;
} else {
mysql_query("INSERT INTO mytable (name) VALUES ("$name")");
}
}
?>
Please tell me what I am doing wrong. I am not getting any functions to work when I edit the contents of the textarea.
Thanks!
The answer is simple. Don't do it using textarea. That's what you're doing wrong.
List your strings in HTML table, with "Edit" and "Delete" buttons.
Edit your rows by one and you'll never have any problem.
As for your approach to treat a database as a plain text file, be consistent and think of it as a plain text file. So, it must be emptied before writing.
Edit:
Or - even much,much better - get rid of the database and use a txt file instead.
So, your code become as simple, as
<textarea name="names" cols=6 rows=12>');
<?php readfile('base.txt') ?>
</textarea>
and form handler is
<?php file_put_contents("data.txt",$_REQUEST['names']); ?>