I'm working at this thing that takes information from a MySQL database that comes from a contact form and displays it all to the user. Well, I got that far without issues using:
$result = mysql_query("SELECT * FROM contact");
while($row = mysql_fetch_array($result))
{
echo $row['name'] . "<br/>" . " " . $row['email'] . "";
echo "<br/>";
echo $row['message'];
echo "<br />";
}
Now I would like to add a button next to each item that would single them out, basically taking me to a page where only the selected row shows up, not all of them, as happens on this page.
The thing is that i don't know how to do that. So, does anyone have any ideas?
Thanks!
Add a button that refreshes the page with a "GET" parameter, such as yourpage.php?uid=123; Then do
if ($_GET['uid'] !== null) {
$id = (int) $_GET['uid'];
$result = mysql_query('SELECT * FROM contact WHERE id='.$id);
...
}
Related
I've been dealing with tihs problem for more than two days but still couldn't find what's the problem. I am trying to build a pagination system for my search results. Code works perfectly fine when I run it in new php file but when it comes to display those result in a table I constantly keep getting that Error! message inside of the last else section. Plus, wherever I place for loop for page numbers it always showing before the table. Think I was so busy with dealing with this problem I am focusing to the same point. Help, please!
edit I've just deleted all conditional statements and got undefined index for all my variables that I get by POST method. That's the problem but still don't know what might be the solution for this.
<?php
if (isset($_POST['search_btn'])) {
include_once "db_connect.php";
$from = $_POST['from'];
$where = $_POST['where'];
$date = $_POST['date'];
$type = $_POST['type'];
$proc_id = $_POST['proc_id'];
if(empty($from) || empty($where) || empty($date) || empty($type)){
header("Location: index.php?search=empty");
exit();
}else{
//define how many results you want per page
$results_per_page = 10;
//number of results stored in database
"SELECT * FROM proc WHERE p_from = '".$from."' and p_where = '".$where."' and type= '".$type."' ";
$result = mysqli_query($conn, $sql);
$number_of_results = mysqli_num_rows($result);
//determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
//determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
//determine the SQL LIMIT starting number for the result on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
//retrive selected results from database and display them on page $sql='SELECT * FROM proc LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result = mysqli_query($conn, $sql);
while($rows = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr onclick="content(\''. $rows['proc_id'] .'\')">';
echo "<td>" .$rows['p_name'] . " " . $rows['p_surname']. " </td>";
echo "<td>" .$rows['p_from']. "</td>";
echo "<td>" .$rows['p_where']. "</td>";
echo "<td>" .$rows['p_date']. "</td>";
echo "<td>" .$rows['price']. "</td>";
echo "<td>" .$rows['type']. "</td>";
echo "</tr>";
}
}
//display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
}else{
echo "Error!";
}
?>
I'm just going to assume that the initial display works, but that pagination is giving you problems?
You're using POST values to pass the data from the search form to your code, however, when you then click the pagination links you are transferred to the search page, and lose those values.
You could change the next page URL to be a form and you pass each necessary value as a hidden input field. However, this has the drawback that when you hit the back button in your browser that it'll complain and ask you to resubmit the form data.
Another solution would be to store these post params in a session, cookie, whatever really. But in my opinion, that's not too great of a solution for this issue either.
I'd suggest you use GET parameters and then pass those in the next page button as well. This has the added benefit of being able to bookmark your searches.
Good luck!
As a side note, instead of building your query using concatenation you should use prepared statements. See the docs for the functions I used in the converted code below: https://www.php.net/manual/en/class.mysqli-stmt.php
if ($statement = mysqli_prepare($conn, "SELECT * FROM proc WHERE p_from = ? AND p_where = ? AND type = ?")) {
mysqli_stmt_bind_param($statement, "s", $from);
mysqli_stmt_bind_param($statement, "s", $where);
mysqli_stmt_bind_param($statement, "s", $type);
$result = mysqli_stmt_get_result($statement);
}
So hey guys!
I currently have a code that gets data from a custom table called wp_refundrequests, and prints them as a table to the page. On the page the admin can either Accept, or Deny the request by pressing a button on the side of each order. Denying the request just deletes the request from the table, but accepting should delete it from the current table and insert the information to the next table called "accepted requests".
The wp_refundrequests table contains customer's order that they want to refund.
The code that gets the info and prints it:
global $wpdb;
$requests = $wpdb->get_results("SELECT * FROM wp_refundrequests", ARRAY_A);
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'] . "'>Deny</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Accept</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
With my current code, the "Accept" button deletes it, and inserts information in to the new table, BUT the information that is inserted is wrong. It seems like it wants to either insert the latest data that had been inserted in to the wp_refundrequests table to the wp_acceptedrequests, or it keeps the data from the latest refund request and tries to insert that instead because for example as seen here(Sorry for the bits of Finnish as well):
If I were to click the "Accept" button on the above, older one, the query would still insert it like this:
So it basically inserts the info from the latest refund_request insert and inserts that instead of the one selected. However the one that had been selected still gets deleted from the table.
Here's the code that is triggered when the user clicks on "Accept"
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
/* Query to do whatever here */
$wpdb->print_error();
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->print_error();
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
I have to say I have no idea why it doesn't want to insert the selected request, please comment if there's something confusing, I'll try to clear it up then.
Thanks in advance!
You redefine $_SESSION with in foreach loop so at the end of foreach it will equal to the last one, pass each row parameter to it is accept link like this
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']."&custname=".$row['customer_name']."&prodname=".$row['product_name']."'>Accept</a></li></div>";
Then call it the same way you get $accept-ID
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_GET['custname'];
$prodname = $_GET['prodname'];
Note:Iuse my phone so make sure if it was a syntax error in the href part of the code
Please try like this and comment out the session variable.
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$accepted_requests = $wpdb->get_results("SELECT * FROM wp_refundrequests WHERE id = $accept", ARRAY_A);
if( !empty($accepted_requests) ) {
$insert = $wpdb->insert("wp_acceptedrequests", $accepted_requests);
if($insert) {
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
}
}
I'm trying to create a simple e-commerce system. First thing I did was select the rows with the same Order ID from mysql. My table looks like this:
Now, I'd like to know how I can group them into separate divs, it should look like this:
Here's my code:
$con = mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result2 = mysqli_query($con, "SELECT DISTINCT request_date, department_id FROM tbl_requests WHERE request_id=".$_GET['request_id']) ;
while($row2 = mysqli_fetch_array($result2)) {
echo "" . $row2['request_date'] . "<br/>";
echo "Order from Department " . $row2['department_id'] . "<br/>";
echo "<br/>";
echo "<hr/>";
echo "<br/>";
}
$result = mysqli_query($con,"SELECT * FROM tbl_requests WHERE request_id=".$_GET['request_id']);
while($row = mysqli_fetch_array($result)) {
echo "" . $row['request_details'] . "";
echo "<br/>";
}
I'm sorry if ever this question is incomplete, please feel free to ask me any more questions. Thank you in advance :)
You can check for every product in array using Javascript or jQuery(much easier).
This way you can check if your page does contain any existing div with that manufacture id (ie. #m_1055, #m_1040) or not.
If div does exist, append product in that div (in jQuery.append())
If not, then first append the div with that manufacture id to the document and then append the product to it.
On one page I have a dynamically created form which has this element in it (note I have left out some stuff for clarity, eg: table and form elements):
$getallusers = "SELECT * FROM `ft_users`";
$usersstmt = $modx->query($getallusers);
while ($row = $usersstmt->fetch(PDO::FETCH_ASSOC)) {
echo "<td><input type='text' name='" . $row['userid'] . "' id='" . $row['userid'] . "' size='4'></td>";
}
On the form processing page I am trying to retrieve the values:
$getallusers = "SELECT * FROM `ft_users`";
$usersstmt = $modx->query($getallusers);
while ($rowuser = $usersstmt->fetch(PDO::FETCH_ASSOC)) {
$userpoints = trim($_POST["user_ids"][$rowuser["userid"]]);
}
If I do print_r($_POST); I get the array of data I sent in the form so I know the forms working OK. Its just the part converting the data into a variable:
$userpoints = trim($_POST["user_ids"][$rowuser["userid"]]);
Does anyone know a better way of doing this?
Change this in the first code block:-
while ($rowuser = $usersstmt->fetch(PDO::FETCH_ASSOC))
to
while ($row = $usersstmt->fetch(PDO::FETCH_ASSOC))
I think it has something to do with your POST retrieval. Exactly $_POST["user_ids"]; it should match whatever you have in your name='" . $row['userid'] . "'
Browse the page source and look it up
There's a form called discussion.php, in which a user will fill out his/her question/discussion and post it to savedisc.php. Some of the savedisc.php looks like this:
$message = $_POST['message'];
$title = $_POST['title'];
$represents = $_POST['represents'];
//connect to database
//save the content of discussion/question into the database for future use
$sql="INSERT INTO Discussion (Message, Title, Type)
VALUES
('$message','$title','$represents')";
//Display user's question/discussion again
echo $message . "<br />";
echo $title . "<br />";
echo $represents . "<br />";
It is not shown above, but I am saving the id field manually, i.e. via phpmyadmin as a auto increment and primary key of course. Therefore, all of the values in the table Discussion will have their own unique id. Once the question/discussion is saved, I want to be able to display $title of each question on wb.php as a link, which as of now looks like this(some code from wb.php):
$result = mysql_query("SELECT * FROM Discussion ORDER BY id DESC");
//When user clicks the question/discussion Title, he/she will be directed to wbcomm.php
while($row = mysql_fetch_array($result))
{
echo "<a href='wbcomm.php' >{$row['Title']}</a><br />";
}
Until here, everything is working smooth. However, from here on, what I'm trying to do is, when the user clicks the question/discussion title via above code, I want him/her to be directed to wbcomm.php?id=1, where id=1 represents the unique id of the question/discussion. Some of the code from wbcomm.php is below:
if (isset($_GET['id']))
{
//connect to db
$wbid = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM Discussion WHERE id = '$wbid' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows() > 0) {
$discussion = mysql_fetch_object($res);
//display member's question here:
echo $discussion['id'] . "<br />";
echo $discussion['Title'] . "<br />";
echo $discussion['Type'] . "<br />";
echo $discussion['Message'] . "<br />";
}
else {
// discussion does not exist with ID
}
}
However, for some reason, the result is blank. I.e. the question/discussion doesn't even show up. What am I doing wrong? Is my procedure even correct?
Thank you.
In your wb.php, you create a link to wbcomm.php but you are not passing the ID of the discussion, so your $wbid will be empty. You need to pass the ID along with the link, like this:
while($row = mysql_fetch_array($result))
{
echo "<a href='wbcomm.php?id={$row['id']}' >{$row['Title']}</a><br />";
}
Your ID column is an autoincrement int type so you do not need to put it in quotes or escape it. You should definitely test it to see if it's numeric, though.
Use this SQL mysql_num_rows($res) > 0