Ajax pagination in php mysql - php

I copied this code from a website for using ajax jquery pagination in php with mysql. I have a mysql table with 5 columns. I want to show all columns of the mysql table - not only two.
I edited the while loop but I cannot insert more columns. Please help me - I am new here and it may be difficult to understand my question. sorry in advance.
$query_pag_data = "SELECT * from ebook LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['title']); //HTML entries filter
$msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
I have linked all code
http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html

You only show $row['id'] and $row['title'], just add some extra with the row names ;)
Example:
$msg .= "<li><b>" . $row['id'] . "</b> <i>" . $row['another_row_name'] . "</i>" . $htmlmsg . "</li>";

while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['title']); //HTML entries filter
$msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</b> " . $row['somefield'] ."</li>";
}

The different values are accessed by the $row array so it depends on what your column names are for the 5 columns.
you might what something like
$msg .= "<li><b>" . $row['colname1'] . "</b> " . $row['colname2'] . " " . $row['colname3'] . <insert more columns here> . "</li>" ;

Related

wpdb->insert producing duplicates

I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.

Displaying search results using mysqli multiquery

How can i display search results using mysqli multiquery. I want to display values from my listing-details table and from my user table. Here is my code:
$searchquery="SELECT * FROM `listing-details` WHERE `listing-address` LIKE '%" . $address . "%' AND `listing-address-street` LIKE '%" . $street . "%' AND `listing-address-barangay-id` LIKE '%" . $barangay . "%'";
$searchquery.= "SELECT `user.user-username`, `user.user-firstname`, `user.user-lastname`, `listing-details.user-username` FROM `user`, `listing-details` WHERE `listing-details.user-username`=`user.user-username`";
if (mysqli_multi_query($conn, $searchquery)) {
do {
if ($result=mysqli_store_result($conn,$searchquery)){
while($row=mysqli_fetch_row($result)){
$listingid =$row['listing-id'];
$username =$row['user-username'];
$listingbedquantity =$row['listing-bedquantity'];
$listingbedtype =$row['listing-bedtype-id'];
$listingguestsquantity =$row['listing-guestsquantity'];
$listingplacetype =$row['listing-placetype-id'];
$listingpropertytype =$row['listing-propertytype-id'];
$listingbathroomquantity =$row['listing-bathroomquantity'];
$listingaddress =$row['listing-address'];
$listingstreet =$row['listing-address-street'];
$listingbarangay =$row['listing-address-barangay-id'];
$listingamenities =$row['listing-amenities-basic-id'];
$listingsafetyamenities =$row['listing-amenities-safety-id'];
$listingsaphotos =$row['listing-amenities-safety-photos-id'];
$listingspace =$row['listing-space-id'];
$listinglandmark =$row['listing-landmark'];
$listingpreferences =$row['listing-preferences-id'];
$listingphotoset =$row['listing-photosset-id'];
$listingexperience =$row['listing-experience-id'];
$listingfrequency =$row['listing-frequency-id'];
$listingstartdate =$row['listing-startdate'];
$listingrate =$row['listing-rate-id'];
$listingprice =$row['listing-price'];
$listingrules =$row['listing-rules-id'];
$listingtitle =$row['listing-title'];
$listingdescription =$row['listing-description'];
$firstname =$row['user-firstname'];
$lastname =$row['user-lastname'];
echo "<ul>\n";
echo "<li>"."" . "<h2>" . $listingtitle . "</h2></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<ul>\n";
echo "<li>"."" . "<h2>" . $listingtitle . "</h2></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<li style='float:right;'>By: " . $firstname . " " . $lastname . "</i></li>";
echo "</ul>";
echo "<hr width='80%' noshade='1'>";
}
mysqli_free_result($result);
}
}
while (mysqli_next_result($conn));
}
However, when I get to run it,the page loads, but results won't show. The purpose of it is to be able to display listing details from a listing-details table listed by the complete name from the user table. Two two tables have user-username column as common key.
Like many learners, you are using the wrong tool, simply because you don't know the proper one.
You need not a multiquery (which you never actually need anyway) but a JOIN.
Simply rewrite your two monster queries to a join like this
$searchquery="SELECT l.*, u.`user-username`, u.`user-firstname`, u.`user-lastname`
FROM `listing-details` l, user u
WHERE l.`user-username`=u.`user-username`
AND `listing-address` LIKE ...";
Besides, your quoting is wrong.

PHP pull from two MySQL tables, where multiple rows in table two

I have two tables that looks like this:
Put it in images since i dont know how to draw a table here.
My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.
The edited code after #Terminus suggestions
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $row["beskrivelse"]."<br>";
} else {
echo "0 results";
}
Can anyone help me do this? I have been searching on google for four days now, without any success.
Do as #Kenney suggested and remove the part where you echo the report from the loop.
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
$beskrivelse = $row["beskrivelse"];
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $beskrivelse . "<br>";
} else {
echo "0 results";
}

How to create a page when clicking a post link using php

Ok i have created a forum posting page where they input the data then its stored in a table then echoed out on a different page with links but i want the links to generate a page with the post then i can allow the user to comment but i have an idea for the comments i just need help with the generation of the page. The code where the user can click...
$connect = mysql_connect("localhost", "root", "123");
$database = mysql_select_db('phplogin');
$data = "SELECT * FROM forum ORDER BY time DESC";
$result = mysql_query($data);
if($connect) {
while($row = mysql_fetch_array($result)){
echo "<div class='post'>" . "<div class='leftside'>" . "<h3 class='by'>" . $row['user'] . "</h3>" . "<h6 class='when'>" . $row['time'] . "</h6>" . "</div>" . "<div class='middle'>" . "<h3 class='questiontitle'>" . htmlspecialchars($row['title']) . "</h3>" . "<p class='description'>" . htmlspecialchars($row['description']) . "</p>" . "</div>" . "<div class='rightside'>" . "<a href='#' class='answer'>Answer it</a>" . "</div>" . "</div>";
}

Input regarding this PHP script to query a mysql database?

I was using this script and everything was great except for the fact that when the actual search was executed and "No Results" was the answer, I wanted the script to display such.
When doing research to see where the FAIL was I discovered that I should be using MySQLi. I have been at this script for 2 days and I seem to be getting further instead of closer. A little help here fellas?
What I am using:
if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';
//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);
$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";
$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));
while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li>
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}
echo $datarow;
echo "<br />";
include 'dbclose.php';
}
mysql_close($con);
You can retrieve the count of the rows with:
mysqli_num_rows($page_query);
Simply verify that it is >0 to chose what to display, the error message or the results
Just add a condition to display something if your query returns 0
if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';
//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);
$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";
$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));
if (mysqli_num_rows($page_query) > 0){
while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li>
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}
echo $datarow;
echo "<br />";
} else {
echo 'Your search returned 0 results';
}
include 'dbclose.php';
}
mysql_close($con);

Categories