htaccess URL rewrite with PHP & MySQL [duplicate] - php

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 4 years ago.
My Question is: How can I show it http://localhost/jakir1/banner2/test.php/title-3 instead of http://localhost/jakir1/banner2/test.php?slug=title-3
When i click
title 1
title 2
title 3
Its Showed Output
Here is My Code:
<table border="1">
<tr>
<td>
Product Name
</td>
<td>
Description
</td>
</tr>
<?php $sql = "SELECT * FROM marchant_item ORDER BY id DESC";
$display = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($display)) { ?>
<tr>
<td><?php echo $row['item_name']; ?></td>
<td>
<?php echo $row['description']; ?>
</td>
</tr>
<?php }
?>
</table>
<?php
if (isset($_GET['slug'])) {
$data = $_GET['slug'];
?>
<table border="1">
<tr>
<td>
Product Name
</td>
<td>
category
</td>
</tr>
<?php $sql = "SELECT * FROM marchant_item WHERE slug ='$data'";
$display = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($display)) { ?>
<tr>
<td><?php echo $row['item_name']; ?></td>
<td>
<?php echo $row['category']; ?>
</td>
</tr>
<?php }
?>
</table>
<?php
}
?>
Output:

write this line in your .htaccess file--
RewriteRule ^test/([A-Za-z0-9-]+)?$ test.php?slug=$1 [NC]
update your code with
<td><?php echo $row['item_name']; ?></td>
and you get
http://localhost/jakir1/banner2/test/title-3

Related

PHP HTML table retrieve multiple data in one <td> tag

I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.

Inserting foreign keys into a table from the website using php (phpMyAdmin)

So, I have 3 tables in my database (phpMyAdmin) bikes, book and users. I have made bike_id and user_id a foreign key in the book table. Now I want these 2 to get inserted into the book table from the web page.
I've got 3 php files which are book1.php, book2.php and book3.php. In book1, you can select a bike which you want to book which results in showing that particular bike in new page. In book2, you select the date when you want to book and when you go to book3, the user_id and bike_id should be automatically inserted into the book table? But I only get ERROR when I press book in book2 page.
Can someone help me plz.. Below is the php codes for 3 php files.
book1.php:
<?php
require 'connect.php';
$select_posts = "select * from bikes ";
$run_posts = mysql_query($select_posts);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th>Select Bike</th>
</tr>
<?php while($row=mysql_fetch_array($run_posts)){ ?>
<tr>
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><a href="book2.php?id=<?php echo $row[bike_id];?>">Select</td>
</tr>
<?php } ?>
book2.php:
<?php
session_start();
?>
<?php
require 'connect.php';
if(isset($_GET['id'])){
$took_id = $_GET['id'];
$select_query = "select * from bikes where bike_id='$took_id'";
$run_query = mysql_query($select_query);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th></th>
<th>Select Date</th>
<th>Book</th>
</tr>
<?php while($row=mysql_fetch_array($run_query)){ ?>
<tr>
<form action="book3.php" method="POST">
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><?php echo $took_id; ?></td>
<td>Select Date: <input type="text" id="datepicker" name="datepicker"></td>
<td><input type="submit" name="Submit" value="Book"></td>
</tr>
<?php }} ?>
</table>
book3.php:
<?php
session_start();
?>
<?php
require 'connect.php';
// Get values from form
$datepicker = $_POST['datepicker'];
$sql="INSERT INTO $tbl_name(datepicker)VALUES('$datepicker')";
$sql="INSERT INTO $tbl_name(user_id)VALUES(select user_id from users)";
$sql="INSERT INTO $tbl_name(bike_id)VALUES(select bike_id from bikes)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>

delete using checkbox code not working properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Code not working properly always shows the message fail.
function delete(){
$con = mysqli_connect("localhost","root","","rishita_db");
$sql="select * from 14_patientdetails";
$result=mysqli_query($con,$sql);
?>
<form method="post" action="">
<center>
<h1><u>Patient Details</u></h1>
<table border="1" style="font-family:Georgia;color:#800000;font-style:bold;">
<tr style="font-family:Georgia;color:green;font-style:bold;">
<th>#</th>
<th>Patient ID</th>
<th>Patient Name</th>
<th>DOB</th>
<th>Gender</th>
<th>Address</th>
<th>Phone No.</th>
<th>Medicare</th>
<th>Doctor Associated</th>
</tr>
<form method="post" action="">
<?php
while($row=mysqli_fetch_array($result))
{
$r=$row['patientId'];
?>
<tr>
<td><input type='checkbox' name='checkbox[]' id="checkbox" value=<?php echo $r; ?>></td>
<td><?php echo $row['patientId']; ?></td>
<td><?php echo $row['patientName']; ?></td>
<td><?php echo $row['DOB']; ?></td>
<td><?php echo $row['Gender']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Phone']; ?></td>
<td><?php echo $row['Medicare']; ?></td>
<td><?php echo $row['Doctor']; ?></td>
</tr>
<?php
}
?>
</table>
<table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="del" type="submit" id="del" value="Delete"></td>
</tr>
</table>
</form>
<?php
if(isset($_POST["del"]))
{
if(isset($_POST["checkbox"]))
echo 'Enter';
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
$sql1 = "DELETE FROM 14_patientdetails WHERE id='$del'";
$q = mysqli_query($con,$sql1);
}
if($q){
echo "Success";
}else{
echo 'Fail';
}
}
}
This is wrong:
$chk = isset($_POST['checkbox']);
$chkcount = count($chk);
for($i=0;$i<$chkcount;$i++){
$del=$chk[$i];
Now there are one too many mistakes, multiple <form> tags and a single closing </form>
I couldn't not edit your code on my phone so I will suggest the way to get along. I'm pretty sure you'll love it when you do it yourself.
Make a <td><input type = "checkbox" name = "checkbox" value = "<?php echo $r; ?>"/>Proceed</td> inside a while-loop something similar to your code while($row = mysqli_fetch_array($result))
In the end of the <form> make a <button> and redirect user to another page probably delete.php. Now, check its set;
if(isset($_POST['checkbox']
{
//foreach loop for your query
foreach($_POST['checkbox'] as $val)
{
// check what you're getting..
echo $val;
}}
You could also use a simple for-loop
If you want:
for($i = 0; $i <count($_POST['checkbox']; $i++)
{
// do your stuff..
}
Bottom line: try to differentiate your forms, queries and make a single loop to read from database and assign the values at the same time to your checkboxes.
This is what I have understood and tried to write from my phone,
please study mysqli/PDO to prevent SQL Injection/XSS.

hyperlink in php search result?

i am new to php
How do i make my search result,ie title is show such that is hyper link to a page?
i have already iput a url column in the table in phpmyadmin
the php code is shown below
<?php
$connect = mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("books",$connect) or die(mysql_error());
$sql = "SELECT * FROM bookinfo";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['searchbox']);
$sql .= " WHERE title = '{$search_term}'";
$sql .= " OR author = '{$search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="searchform" method="POST" >
Search:<input type="text" name="searchbox" />
<input type="submit" name="search" value="search book" />
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr>
<td>Title</td>
<td>Author</td>
<td>Price</td>
</tr>
<?php while ($row = mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['author']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php }
?>
</table>
Hyperlinks are made with the a (or "anchor") tag in HTML. Simply include one in your output:
<td><?php echo $row['title']; ?></td>

making two tables depending if they have 0 or 1

essentially I want to have two tables one with everyone with Confirmed = 1 and the other with Confirmed = 0.
I have this sql query at the moment
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE $row[Confirmed] = 1");
but I keep getting
Notice: Undefined variable: row in C:\xampp\htdocs\test.php on line 19
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
rest of the code for that page
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<?php
if ($row['Confirmed'] == 0) { ?>
<a href=confirm.php?BookID=<?php echo $row['BookID']; ?> </a> Confirm </td> <?php } else { ?>
<a href=deny.php?BookID=<?php echo $row['BookID']; ?> </a> Deny </td> <?php
} ?>
</tr> <?php
} ?>
</table>
Are you really trying to put the value in $row['Confirmed'] in the query, or are you just trying to query a field named Confirmed? if its the latter try this:
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed= 1");
but since also you seem to differentiate the table in php with if ($row['Confirmed'] == 0) you probably need to get all data in one query with this:
$result = mysqli_query($con,"SELECT * FROM tbl_booking");
this won't give you 2 tables as you initially asked for though.
you can create 2 html tables with this:
<?php
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed = 1");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<a href=deny.php?BookID=<?php echo $row['BookID']; ?> </a> Deny </td>
</tr> <?php
} ?>
</table>
<?php
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed = 0 ");
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<a href=confirm.php?BookID=<?php echo $row['BookID']; ?> </a> Confirm </td>
</tr> <?php
} ?>
</table>

Categories