PHP not fetching data from MySQL database - php

I'm making a comment box, in which
Comment box will be under the post
Every comment box has the same id as the post has
That's why i preffered to do a while loop
But, the problem is
As i'm using a fetch_assoc() method, some comment tables will be empty so
i've made a if else code for them
Here is my if else code :
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
$result_query_select_comment_table = $dbc->query($select_comment_table);
if(!$result_query_select_comment_table) {
$result_select_comment_table = array("full_name" => "", "comment"=> "No comments yet.");
}
else {
$result_select_comment_table = $result_query_select_comment_table->fetch_assoc();
}
If other code is need :
echo '<div id="feed_comment_box_' . $row['id'] . '"' . 'class="feed_comment_box_cl"><div id="add_comment_id" class="add_comment_cl">
<form class="comment_form" method="post" action="' .$_SERVER['PHP_SELF'] . '">
<input name="comment_full_name" type="text" class="input_comment_full_name"> </input>
<textarea name="input_comment_text" type="text" class="input_comment_text" ></textarea><input name="comment_submit" type="submit"></input> <br>
</form>
</div><br>
<div id="comment_box_id" class="comment_box_cl">
<table tabindex="0" class="comment_box">
<tr> <td class="comment_full_name">' . $result_select_comment_table["full_name"] . '</td></tr><br>' .
'<tr> <td class="comment_full_name">' . $result_select_comment_table["comment"] . '</td></tr><br>'
. '</table></div></div>';
echo '</div>';
Problem : As you see the if else code, the php should echo no comments yet when there is no comment. But, even after inserting the comment(i tried manually insert in database) it is showing no comments yet whereas it should show the comments
Note: I am using a while loop that's why the comment div is coming under every post.

It looks like you're missing a space in your query:
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
should be
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . " ORDER BY id";
The invalid query is causing $result_query_select_comment_table to always be false.

Related

Live search results showing on new page

I hope someone can help me, I have an live Ajax Jquery search bar. Now my search bar shows no more then 5 results. What I want is an button below the live results with 'More results', and if you click this button you go to a new page and see all te results of your search.
What I have tried is to put an submit button below the row results, but then he put all the names of the results into the search insteady of the text from the input.
PHP:
<form action="./s/" method="GET">
<div class="searchbox">
<!-- SEARCHBOX INPUT -->
<input type="text" name="s" class="searchbox-input" placeholder="Search trough 1,000 games in our database.." name="name" />
<div class="searchbox-line"></div>
<div class="searchbox-icon">
<input class="img-searchbox-icon" name="" value="" type="submit">
</div>
<div class="results"></div>
</div>
</form>
MySQL:
if(isset($_REQUEST["term"])){
// Prepare a select statement
$sql = "SELECT * FROM games WHERE name LIKE ? ORDER BY name LIMIT 5";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_term);
// Set parameters
$param_term = '%' . $_REQUEST["term"] . '%';
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
// Check number of rows in the result set
if(mysqli_num_rows($result) > 0){
// Fetch result rows as an associative array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo '<a href="' . $row["url"] . '">';
echo '<p class="results-name">' . $row["name"] . '</a></p>';
echo '<p class="results-platform">';
echo '<img class="platform" src="img/platform-icons/' . $row["platform1"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform2"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform3"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform4"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform5"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform6"] . '.png" />';
echo '</p>';
}
} else{
echo '<p class="results-gamename">No matches found</p>';
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Alright, I'll take a shot at this. In your HTML file (the one you originally named 'PHP') you've declared the form like this:
<form action="./s/" method="GET">
And you've given your search input the following name:
<input type="text" name="s" class="..." placeholder="..." name="name" />
Hence, when you send through the form, your input field will be sent through with the name 'name', since you've double declared its name. It'll fallback to the last 'name' attribute you've given to the element.

Retrieve values from a database and importing into table rows

Good day guys, I will be glad if you can help me out.
I have a project. and the remaining part of it is where i can pull out data from the database.
Its a tracking application. I want a situation whereby if a user type his/her reference number, it will display the results in a tabular from i.e
Reference Number, Ship date, ship weight, excpected delivery, ship country, ship state, city, zip, e.t.c. I have the following html code:
<form id="track" name="track" method="post" action="tester.php">
<h2>Track your shipment Here</h2>
<p><label> Tracking Reference:
<input type="text" id="reference" name="reference" value="" maxlength="40" required="required" /></label></p>
<div class="button_holder">
<p> <input type="submit" id="track" value="Track Now" maxlength="40" required="required" /></label>
</label></p>
</div>
and my php code is:
<?php
$ref = mysql_real_escape_string($_POST['reference']);
// conmnecting to the database
if(isset($ref)){
$connection = mysql_connect('localhost', 'root', 'wisdom');
mysql_select_db('gday');
$query = "SELECT * FROM shipment WHERE id = '".$ref."' ";
$row = mysql_query($query);
echo "<table border=\"1\"; width='550px'>"; // start a table tag in the HTML
//Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['ship_type'] . "</td><td>" . $row['ship_ref'] . "</td><td>" . $row['ship_date'] . "</td><td>" . $row['ship_weight'] . "</td><td>" . $row['expected_delivery'] . "</td></tr>" . $row['ship_country'] . "</td></tr>" . $row['ship_state'] . "</td></tr>" . $row['ship_city'] . "</td></tr>" . $row['ship_zip'] . "</td></tr>" . $row['remark'] . "</td></tr>";
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
}
else{echo " Enter a valid Reference Number";
exit;}
?>
Now, my major problem is this: the following code output just the table without any information inserted, Please what could be done to make sure the rows are filled with data retrieved from the database?
Thanks
Fetch fetch fetch.......
Change it like this:
$res= mysql_query($query);
$row=mysql_fetch_assoc($res);
mysqli_ functions are deprecated, don't use them anymore. Use the MySQLi or PDO libraries instead.

How to display results from search below the form

i would like to ask if someone could help me with a search query and displaying of the results.
Here is the code...
<?php
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="dasi";
$db_password="**************";
$db_name="dasi";
$db_tb_name="test";
$db_tb_atr_price="price_vat";
$db_tb_atr_cur="currency";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE code like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<table border=1>";
echo "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" . $data_fetch['currency'] . "</td></tr>";
echo "</table>";
}
echo "</ol>";
mysql_close();
?>
In content i added the form ...
<form action="search.php" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
</form>
So ... all is working normaly.. i am getting the results, everything is fine. Problem is:
i want to have results displayed below the form itself, not in a new page.
If anyone could help me that would be great. Thank you in advance
P.S.
Well i have no idea how it works actualy but was thinking, isnt there a way where the result can be added into empty div below the form or something like this? I tryed the options above but it dosnt helped.
Save your table in to a variable:
$table = "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
$table .= "<table border=1>";
$table .= "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" . $data_fetch['currency'] . "</td></tr>";
$table .= "</table>";
}
$table .= "</ol>";
And print it in your template:
<form action="search.php" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
</form>
<?php echo $table ?>
Just embed your form code in your search.php and then check for isset($submit) and you are good to go.
<?php
?>
<form action="" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
</form>
<?php
if(isset($submit))
{
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="dasi";
$db_password="**************";
$db_name="dasi";
$db_tb_name="test";
$db_tb_atr_price="price_vat";
$db_tb_atr_cur="currency";
//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_POST['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE code like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<table border=1>";
echo "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" . $data_fetch['currency'] . "</td></tr>";
echo "</table>";
}
echo "</ol>";
mysql_close();
}
?>
You've got two choices,
Use AJAX to actually get the results to 'collect' the PHP Generated Content and use JavaScript to append it to somewhere on the page.
Place the Search Algorithm and PHP Code at the top of the same page of the form, then use an isset() $_GET or $_POST to check if it's been submitted successfully, save the results & content to be later printed elsewhere.

Invalid argument when passing checkbox array from GET to POST

PHP novice here. Goal is to:
a) Display a table of booking options (fitness classes) dynamically from database records, allow user to select multiple options with checkboxes against each row - this bit's working.
b) Pass the checkbox selection to a table listing the selected data on a confirmation page. I'm getting an error here: Invalid argument supplied for foreach().
c) Update the database when user hits the second page's 'Confirm' button.
Research so far uncovered this advice on using $_GET and $_POST to achieve this with an array.
My checkbox code on the initial page:
echo '<form action="makebooking.php" method="get">';
echo '<td><input type="checkbox" name="class_id[]" value=' . $row['class_id'] . '</td></tr>';
The foreach statement error comes from this code that generates the table of choices on the second page:
//Check if the GET is set from classes.php
if (isset($_GET['class_id'])) {
// Grab the score data from the GET
foreach($_GET['class_id'] as $class_id) {
$_GET['class_id'] = $class_id;
}
}
//table header
echo '<table class="table table-bordered table-hover">';
echo '<thead><tr><th>Date</th><th>Time</th><th>Venue</th><th>Who\'s going?</th> <th>Add someone</th></tr></thead>';
//create the form
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
//Get the class IDs from the GET to use in the POST
foreach ($_GET['class_id'] as $class_id) {
$sql = "SELECT class_id, DATE_FORMAT(date, '%a, %d %b') AS new_date, DATE_FORMAT(time, '%H:%i') AS new_time, venue FROM classes WHERE class_id = '$class_id'";
$data = mysqli_query($dbc, $sql);
//---------------------------------------------------------------------------------
//get table data
while ($row = mysqli_fetch_array($data)) {
$date = $row["new_date"];
$time = $row["new_time"];
$venue = $row["venue"];
$class_id = $row["class_id"];
}
//---------------------------------------------------------------------------------
//Show a table of the selected classes
echo '<input type="hidden" name="id" value= ' . $class_id . ' />';
echo '<td>' . $date . '</td>';
echo '<td>' . $time . '</td>';
echo '<td>' . $venue . '</td>';
echo '<td>' . $username . '</td>';
echo '<td><button class="btn btn-mini" type="button"><i class="icon-user"></i><i class="icon-plus"</i></button></td></tr>';
}
echo'</table>';
// Make booking button
echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Confirm">';
echo '</form>';
}
Full code of both pages at this pastebin. All error-fixing advice gratefully accepted!
Figured out that I had been declaring the variables in if/else loops that made them inaccessible to other parts of the code.
I also added some extra hidden input arrays into both tables to validate the input. This required a string-to-integer conversion of the class ID in order to update the db with the selected data.
Full fixed code is here for anyone struggling with something similar.

Update within mysql query results

hoping you can help with what I expected to be a simple function.
I'd like to allow users to remove (i.e. change published=y to published=n) a query result but can't for the life of me figure out how to do it. Here's the query as is:
$result = mysql_query("SELECT * FROM discussion WHERE publish='y' ORDER BY timestamp DESC");
while($row = mysql_fetch_array($result))
{
echo ("<tr><td>" . $row['name'] . "<span class=\"remove\">(REMOVE)</span></td>
<td width=\"300\">" . nl2br($row['question']) . "</td>
<td>" . $row['author'] . "</td>
<td>" . $row['timestamp'] . "</td>
<td>View Discussion</td></tr>");
}
The query I'd like to run when the user clicked 'REMOVE' (within the above query):
mysql_query("UPDATE discussion SET publish='n' WHERE discussionID='XXXXX");
Any ideas from the fine people at SO?
New to php/mysql so forgive the ignorance.
I think the simplest option here would be to insert your remove button into a form.
Try adding this form into your table. I am echoing the discussionID into the checkbox so that you can distinguish between the multiple checkboxes in the table:
<form method="post" action="">
<input type="checkbox" id="removeCheckbox" name="removeCheckbox" value="<?php echo $row['discussionID']; ?>" />
<input id="submit" name="submit" value="Submit" type="submit" />
</form>
And then in your PHP, you update the database with the value of the checkbox:
<?php
if (isset($_POST['removeCheckbox'])) {
$checkboxValue = $_POST['removeCheckbox'];
mysql_query("UPDATE discussion SET publish='y' WHERE discussionID='$checkboxValue'");
}
?>
Does that make sense?
EDIT: This should now work! Also updated to include Dan's suggestion.
the same way you do a link to go to the discussion ( a href=\"discussion.php?discussionID=" . $row['discussionID'] . "\">View Discussion ).
a href=\"delete_discussion.php?discussionID=" . $row[ 'discussionID' ] . "\">REMOVE
and create the delete_discussion.php file to run your update query

Categories