How can I add an specific row into database from another database? - php

I want to pass a specific row from database to another database by clicking the add button. my dbconnection is included at the top of it.
Here is the my table
Here is my Code
<table style="margin-left: 28%;">
<tr>
<th><h2>Product Code</h2></th>
<th><h2>Products and Services</h2></th>
<th><h2>Amount</h2></th>
<th><h2>Action</h2></th>
</tr>
<tr>
<?php
$select_query = "SELECT * FROM products_and_services";
$select_result = mysqli_query($connection,$select_query);
?>
<?php while ($rows = mysqli_fetch_row($select_result)):;
$id = $rows[0];
$prod_code = $rows[1];
$service = $rows[2];
$amount = $rows[3];
?>
<td style="text-align: center;"><?php echo $rows[1]; ?></td>
<td style="text-align: center;"><?php echo $rows[2]; ?></td>
<td style="text-align: center;"><?php echo $rows[3]; ?></td>
<td style="text-align: center;"><a href="" name="add">Add</td>
<?php
if(isset($_POST['add'])){
$sql = "INSERT INTO temp_table (Product_name,Product_price,Product_ID)
VALUES ('$service','$amount','$id')";
mysqli_query($db,$sql);
}
?>
</tr>
<?php endwhile ?>

Related

Display database fields with some conditions

I want to display the invoice bill for each customer when admin clicks on view bill button for that customer. Also I want to display all the newspaper details in a single invoice when customer name is same.I have included the relevant code and database snapshots.
c_details.php
<?php
session_start();
include_once("connect.php");
$query="SELECT * FROM sub_details";
$result=mysqli_query($conn,$query);
?>
<table align="center">
<h3 style="color: black; font-weight: bold; text-align: center;">
Subscription details</h3><br>
<tr>
<th style="text-align: center;">Id</th>
<th style="text-align: center;">Name</th>
<th style="text-align: center; width: 900px">Newspaper</th>
<th style="text-align: center;">Duration</th>
<th style="text-align: center;">Price</th>
<th style="text-align: center; width: 800px">Subscription date</th>
<th style="text-align: center;">Remaining Days</th>
<th style="text-align: center;">Bill</th>
</tr>
<?php while($rows=mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['newspaper']; ?></td>
<td><?php echo $rows['duration']; ?></td>
<td><?php echo $rows['price']; ?></td>
<td><?php echo $rows['sdate']; ?></td>
<?php echo var_dump($result->fetch_all(MYSQLI_ASSOC));
$date1=date_create();
$date2=date_create($rows['edate']);
$interval=date_diff($date2,$date1)->format("%a days");
?>
<td><?php echo $interval; ?></td>
<td>
<form action="invoice.php" method="GET">
<?php $invoiceId = $rows['name']; ?>
<button type="submit" onclick= "window.open('invoice.php?name= '<?php echo
$rows['name']; ?>)" class="btn btn-primary btn-lg" value="submit" >
View bill</button></td>
</form>
</tr>
<?php } ?>
</table>
invoice.php
<?php
include_once("connect.php");
$invoiceId = isset($_GET['name']) ? $_GET['name'] : null;
if($invoiceId) {
$query = "SELECT * FROM sub_details WHERE name = ? limit 1";
$result = mysqli_query($conn,$query);
?>
<?php while($rows=mysqli_fetch_assoc($result)) {
?>
<form style="text-align: left;margin-left: 30px" class="register-form" id="register-form">
<div class="form-group"> Bill no: <input type="text" name="id" value="<?php echo $rows['id']; ?>"></div><br>
<div class="form-group">Name: <input type="text" name="name" value="<?php echo $rows['name']; ?>"></div><br>
<div class="form-group">Address: <input type="text" name="address" value="<?php echo $rows['address']; ?>"></div><br><br>
</form>
</th>
</tr>
<td>
<table cellpadding="5px" cellspacing="6px" style="width: 75%; border-radius:20px;">
<tr>
<th>Newspaper</th>
<th >Duration</th>
<th >Price</th>
</tr>
<tr>
<td ><?php echo $rows['newspaper']; ?></td>
<td><?php echo $rows['duration']; ?></td>
<td><?php echo $rows['price']; ?></td>
</tr>
<tr>
<td>DELIVERY CHARGES</td>
<td colspan="3" style="padding-right:60px;text-align: right;">50</td>
</tr>
<tr>
<th>Total</th>
<?php
$t_price=$rows['price']+ 50; ?>
<th colspan="3" style="text-align: right;padding-right: 55px"><?php echo $t_price; ?></th>
</tr>
<tr>
<th colspan="4" >
<br><br><br><br><br><br><br>
</th>
</tr>
</table>
</td>
<tr>
<th colspan="4" style="border-top-color: #ffff4d">
<p style="text-align: left;">Note: Clients are requested to pay the bill before 5th of every month.</p>
</th>
</tr>
<?php } ?>
</table>
Database screenshots
First, you should pass the invoice ID for each record in c_details.php so that you can identify them later:
<button type="submit" onclick= "window.open('invoice.php?id='<?php echo $rows['id']; ?>)" class="btn btn-primary btn-lg" value="submit" >View bill</button>
It will produce URLs like invoice.php?id=<id>, where <id> is the ID for each record in the database. So, for example, a record with the ID 102 will be invoice.php?id=102.
On invoice.php, you can retrieve the ID with $_GET['id']. You should adjust your query to fetch the data for the given invoice:
$invoiceId = isset($_GET['id']) ? $_GET['id'] : null;
if($invoiceId) {
$query = $conn->prepare("SELECT * FROM sub_details, signup_c WHERE id = ? limit 1";
$query->bind_param('s', $invoiceId);
$query->execute();
$result = $query->get_result();
}
If you're using PHP 7.0 or later, you can simplify your code using the null coalescing operator:
$invoiceId = $_GET['id'] ?? null;
I strongly recommend you to learn about how to prevent SQL injections in PHP.

PHP View data from a single record by clicking on the view button

I have two pages students_list.php and students_profile.php
On students_list.php i have a table showing all records of all students and the VIEW button which when clicked it should open the students_profile.php page and get all the data from the single record selected based on the std_id
UPDATE: So now i get the id http://localhost/sms/student_profiles.php?std_id=3 when i click on the VIEW button but i am still not retrieving the data from this record selected
This is the code for students_list.php
<?php
require_once 'include/database/connect.php';
try {
$pdo = new PDO("mysql:host=$host_db;dbname=$name_db", $user_db, $pass_db);
$sql = 'SELECT * FROM students';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $name_db :" . $e->getMessage());
}
?>
<table id="data-table-default" class="table">
<thead>
<tr>
<th width="1%">#</th>
<th width="1%" data-orderable="false"></th>
<th>Name & Surname</th>
<th>Gender</th>
<th>Faculty</th>
<th>Semester</th>
<th>Mobile</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr class="odd gradeX">
<td width="1%" class="f-s-600 text-inverse"><?php echo ($row['std_id']) ?></td>
<td width="1%" class="with-img"><?php echo ($row['std_status']) ?></td>
<td><?php echo ($row['std_name']) ?></td>
<td><?php echo ($row['std_gender']) ?></td>
<td><?php echo ($row['std_faculty']) ?></td>
<td><?php echo ($row['std_semester']) ?></td>
<td><?php echo ($row['std_mobile']) ?></td>
<td align="right">
<i class="fas fa-eye"></i> View
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and below this is the student_profile.php
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
}
?>
<h4><?php echo ($row['std_name']) ?></h4>
<div class="d-flex">
<div class="pr-4"><strong>NATIONAL#</strong> <?php echo ($row['std_number']) ?></div>
<div class="pr-4"><strong>DOB</strong> <?php echo ($row['std_dob']) ?></div>
<div class="pr-4"><strong>GENDER</strong> <?php echo ($row['std_gender']) ?></div>
<div class="pr-4"><strong>MOBILE</strong> <?php echo ($row['std_mobile']) ?></div>
</div>
<div class="pt-1 font-weight-bold">Master's Degree (MBA)</div>
<div>STD# <strong><?php echo ($row['std_id']) ?></strong></div>
<div>email#example.com</div>
You don't fecth the data from the query. So $row will never contain the array you expect:
$row = mysqli_fetch_assoc($res); //this will populate $row
in your code:
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($res);
}
?>

PHP/MySQL: Sort database results by clicking on hyperlink in the page

I have a page that executes a query from a database. There are 5 columns, and I want the first and last column to be a sorting hyperlink. This is my code. I want to be able to click on column "price" or "course ID" to sort them by the category. When I click the link, nothing happens, but the ?sort=price shows on the url. I am fairly new to PHP and MySQL and I cannot find anything online. Thank you in advance.
<?php
require_once ('mysqli_connect.php');
//grab data from database
$course = "SELECT course.courseID, course.courseTitle, course.credit,
book.bookTitle, book.price, book.isbn13
FROM course
INNER JOIN coursebook ON course.courseID=coursebook.course
JOIN book ON coursebook.book=book.isbn13
ORDER BY course.courseID";
$course_response = mysqli_query($dbc, $course);
$isbn = "SELECT isbn13 FROM book";
$isbn_response = mysqli_query($dbc, $isbn);
// Close connection to the database
mysqli_close($dbc);
?>
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>Book Catalog</title>
</head>
<!-- the body section -->
<body>
<main>
<div>
<h1 align='center'>Book Catalog</h1>
<?php $array = array(); // make a new array to hold data
$index = 0;
?>
<?php if($course_response){ ?>
<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"><a href="index.php?sort=course"><b>Course #</b>
</td>
<td align="left"><b>Course Title</b></td>
<td align="left"><b>Book Image</b></td>
<td align="left"><b>Book Title</b></td>
<td align="left"><b>Price</b>
</td>
</tr>
<?php
if ($_GET['sort'] == 'price') {
$query .= " ORDER BY book.price";
}elseif ($_GET['sort'] == 'course') {
$query .= " ORDER BY Description";
}
?>
<?php while($row = mysqli_fetch_array($course_response)){ ?>
<tr>
<td align="left">
<a href="www.barnesandnoble.com">
<?php echo $row['courseID']; ?></a></td>
<td align="left"> <?php echo $row['courseTitle']; ?> (<?php echo
$row['credit']; ?>)</td>
<td align="left"> <a href='bookDetails.php'>
<?php $isbnrow = $row['isbn13']; ?><img src='images/<?php echo
$isbnrow?>.jpg' width='90px' height='100px'></a></td>
<td align="left"> <?php echo $row['bookTitle']; ?></td>
<td align="left"> <?php echo "$" . $row['price']; ?></td>
</tr>
<?php } ?>
<?php } else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
} ?>
</table>
</div>
</main>
</body>
</html>
You are not passing the get paramaters to the mysql query to order the results.
update your codes by following code,
<?php
require_once ('mysqli_connect.php');
//grab data from database
$order_by = null;
if($_GET){
switch($_GET['sort']){
case 'price' :
$order_by = 'ORDER BY book.price ASC';
break;
case 'course' ;
$order_by = 'ORDER BY course.courseID DESC';
break
}
}
$course = "SELECT course.courseID, course.courseTitle, course.credit,
book.bookTitle, book.price, book.isbn13
FROM course
INNER JOIN coursebook ON course.courseID=coursebook.course
JOIN book ON coursebook.book=book.isbn13 ";
$course .= (!is_null($order_by)) ? $order_by : "ORDER BY course.courseID ASC";
$course_response = mysqli_query($dbc, $course);
if(!$course_response){
die("Error in query: " . mysqli_error($dbc));
}
?>
<!DOCTYPE html>
<html>
<head><title>Book Catalog</title></head>
<body>
<main>
<div>
<h1 align='center'>Book Catalog</h1>
<?php if($course_response){ ?>
<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"><a href="index.php?sort=course"><b>Course #</b></td>
<td align="left"><b>Course Title</b></td>
<td align="left"><b>Book Image</b></td>
<td align="left"><b>Book Title</b></td>
<td align="left"><b>Price</b></td>
</tr>
<?php while($row = mysqli_fetch_array($course_response)){ ?>
<tr>
<td align="left"><?php echo $row['courseID']; ?></td>
<td align="left"> <?php echo $row['courseTitle']; ?> (<?php echo $row['credit']; ?>)</td>
<td align="left"> <a href='bookDetails.php'><?php $isbnrow = $row['isbn13']; ?><img src='images/<?php echo $isbnrow?>.jpg' width='90px' height='100px'></a></td>
<td align="left"> <?php echo $row['bookTitle']; ?></td>
<td align="left"> <?php echo "$" . $row['price']; ?></td>
</tr>
<?php } ?>
<?php } else { echo 'results not found!'; } ?>
</table>
</div>
</main>
</body>
</html>
<?php
// Close connection to the database
mysqli_close($dbc);
?>

Split page into 4 sections with data from database

How to split my page into 4 sections with data from database?
I'm displaying data from the database and they appear one by one on the table. And I want to split them into 4 parts and each of them will show another result from the database. In addition, I would like to make line in the middle, which will keep them separated.
That's my code, to show results from DB:
EDIT: Ok, I edited like u said, and now page is splitted on 2 section, but I got on them the same results. How to change this, that on the left side will be shown one record, and on the right side another.
$result = $wpdb->get_results("SELECT company_wojewodztwo, company_powiat, company_miasto, company_name, telefon, company_nip, company_opis FROM test WHERE company_wojewodztwo = '$woj' AND company_powiat = '$pow' AND company_miasto = '$nazwa' ORDER BY RAND()");
foreach ( $result as $k => $v ) {
$c_name = stripslashes($v->company_name);
$c_opis = stripslashes($v->company_opis);
$c_mob_nr = stripslashes($v->telefon);
$c_nip = stripslashes($v->company_nip);
?>
<table>
<tbody>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_name; ?></td>
<td style="width: 50%;"><?php echo $c_name; ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_nip ?></td>
<td style="width: 50%;"><?php echo $c_nip ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_mob_nr ?></td>
<td style="width: 50%;"><?php echo $c_mob_nr ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo $c_opis ?></td>
<td style="width: 50%;"><?php echo $c_opis ?></td>
</tr>
</tbody>
</table>
<?php if(count($result) != $k+1){ ?>
<p>___________________________________________________</p>
<?php } ?>
<?php
}
?>
do something like this:
<table>
<tbody>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo 'data 1'; ?></td>
<td style="width: 50%;"><?php echo 'data 2'; ?></td>
</tr>
<tr style="height: 50%;">
<td style="width: 50%;"><?php echo 'data 1'; ?></td>
<td style="width: 50%;"><?php echo 'data 2'; ?></td>
</tr>
</tbody>
</table>
I put styles inline, You try to put them in a css file.
Update your code...
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT company_province, company_district, company_city, company_name, telefon, company_nip, company_opis FROM test WHERE company_province = '$woj' AND company_district = '$pow' AND company_city = '$nazwa' ORDER BY RAND()");
foreach ( $result as $k => $v ) {
$c_name = stripslashes($v['company_name']);
$c_opis = stripslashes($v['company_opis']);
$c_mob_nr = stripslashes($v['telefon']);
$c_nip = stripslashes($v['company_nip']);
?>
<div class="parent">
<table>
<tr><th><p><b> Name: </b></th><td>
<?php echo $c_name;?></p></td></tr>
<tr><th><p><b> NIP: </b></th><td>
<?php echo $c_nip;?></p></td></tr>
<tr><th><b> Number tel: </b></th><td>
<?php echo '+48 '.$c_mob_nr;?><br></td></tr>
<tr><th><b> Opis Firmy: </b></th><td>
<span class="more"> <?php echo $c_opis;?></span></td></tr>
</table>
<?php if(count($result) != $k+1){ ?>
<p>___________________________________________________</p>
<?php } ?>
</div>
<?php
}
?>

delete selected rows list in php mysql

I'm trying to delete the selected rows from the list I have in mysql so I won't have to delete the rows in my table one by one. When I press delete, my page refreshes without any php error but I don't get the result desired either. here is what I have:
<?php $product_set = find_all_products(); ?>
<body>
<form action="manage_products.php" method="delete">
<div class="page">
<article>
<div id="page">
<?php echo message(); ?>
<h2>Manage Products</h2>
<table>
<tr>
<th style="text-align: left; width: 125px;">Location</th>
<th style="text-align: left; width: 250px;">URL to the Product</th>
<th style="text-align: left; width: 100px;">First Name</th>
<th style="text-align: left; width: 100px;">Last Name</th>
<th style="text-align: left; width: 100px;">Size</th>
<th style="text-align: left; width: 100px;">Status</th>
<th style="text-align: left; width: 100px;">Checked</th>
<th colspan="2" style="text-align: left;">Actions</th>
</tr>
<?php while($product = mysqli_fetch_assoc($product_set)){ ?>
<tr>
<td><?php echo htmlentities($product["location"]);?></td>
<td><?php echo htmlentities($product["productURL"]); ?></td>
<td><?php echo htmlentities($product["fname"]); ?></td>
<td><?php echo htmlentities($product["lname"]); ?></td>
<td><?php echo htmlentities($product["size"]); ?></td>
<td><?php echo htmlentities($product["status"]); ?></td>
<td><input name="checkbox" type="checkbox" id="checkbox[]" value="<?php echo $product['id']; ?>"></td>
<td>Edit Order</td>
<td>Delete Order</td>
</tr>
<?php } ?>
</table>
<?php
// Check if delete button active, start this
if(isset($_GET['delete']))
{
$checkbox = $_GET['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM product WHERE link_id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
$_SESSION["message"] = "Product deletion failed.";
redirect_to("created_products.php"); }
}
//mysqli_close($dbc);
?>
<br />
</div>
</div>
<br>
<input type="submit" name="submit" value="Delete" onclick="return val();"/>
</form>
<div class="clear-all"></div>
</article>
</div>
</body>
Also place all the code which is for deleting the rows in the starting of your file. Thank you #Traveller for this.
Instead of for loop, use WHERE IN
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM product WHERE link_id='$del_id'";
$result = mysql_query($sql);
}
Replace that with
$ids = implode(",", $_POST['checkbox']);
$sql = "DELETE FROM product WHERE link_id in ($ids)";

Categories