Passing php variables - php

I am creating a user interface for managing a database. Each entry has two radio buttons, one for validation (and subsequent emailing to concerned authorities) and one for deleting the entry. The code is given below, and the bold parts are the ones which concern this functionality.
A couple of questions:
1) Is it possible to do so without the form attribute? If so, how would I do it?
2) With the form attribute, I use 'handle.php' to delete each entry. The syntax in the 'handle.php' includes this line
$sql="DELETE FROM rti WHERE ID=x"; //x here is the ID number to delete.
Now, how do I pass the value of the ID as well from my interface, so that the above line of code deletes the entry corresponding to the button that was pressed?
<html>
<head>
<style>
* {
margin: 0px;
padding: 0px;
}
div.topbar {
position: relative;
background-color: black;
height: 45px;
text-align: center;
font-family: Calibri;
font-size: 30px;
padding-top: 10px;
}
div.topbar img {
position: absolute;
left: 10px;
top: 0px;
}
div.topbar span {
color: white;
}
div.container {
background-color: #a5a5a5;
width: 100%;
height: 100%;
color: white;
}
div.data {
padding: 20px;
}
table {
background-color: #b1b1b1;
border-collapse: collapse;
}
th, td {
text-align: center;
border: solid 1px black;
margin: 0px;
}
th.one {
width: 50px;
}
th.two{
width: 150px;
}
th.three {
width: 75px;
}
th.four {
width: 200px;
}
div.validation {
background-color: #b5b5b5;
width: 200px;
height: 100%;
position: relative;
left: 1100px;
bottom: 300%;
text-align: center;
border-left: solid 1px black;
}
</style>
</head>
<body>
<div class = "topbar"><img src="logo.png"><span>RTI DATABASE</span></div>
<div class = "container">
<div class ="data">
<?php
$servername="localhost";
$username="root";
$password='';
$conn=mysql_connect($servername, $username, $password) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = 'bsp';
mysql_select_db($dbname, $conn) or die ("Error selecting specified database on mysql server: ".mysql_error());
$sql="SELECT * FROM rti";
$result=mysql_query($sql) or die ("Query to get data from firsttable failed: ".mysql_error());
echo "<table>";
echo "<tr>";
echo '<th class="one">ID</th>
<th class="two">Name</th>
<th class="three">Board</th>
<th class="four">Query</th>
<th class="five">Validate</th>
<th class="six">Delete</th>
<th class="seven">Submit</th>';
echo "</tr>";
while ($row=mysql_fetch_array($result)) {
$id=$row["ID"];
$name=$row["name"];
$board=$row["board"];
$query=$row["query"];
echo "<tr>";
echo "<td>$id</td>
<td>$name</td>
<td>$board</td>
<td>$query</td>
**<form action='handle.php' method='POST'>
<td><input type='radio' name='option' value='validate'></td>
<td><input type='radio' name='option' value='delete'></td>
<td><input type='submit' value='Submit' name='submit'>
</form></td>";
echo "</tr>";**
}
echo "</table><br>";
?>
</div>
</div>
</body>
</html>

**<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>
<td><input type='radio' name='option' value='delete'>
<input type='hidden' name="id" value='".$id."'>
</td>
<td><input type='submit' value='Submit' name='submit'></form></td>";
In handle.php:
$id = $_POST['id'];

inside your form add an hidden type input with the id value for each item , so each time user click the button the id sent. just like this :
<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td>
<input type = 'hidden' name="id" value='".$row['id']."'><td>
<input type='radio' name='option' value='delete'></td>
<td><input type='submit' value='Submit' name='submit'></form></td>";

use something like <input type='radio' name='option[your_id]' value='validate'> to identify the item.
Like: <input type='radio' name='option[$id]' value='validate'>

Related

Deleting a Table Entry with PHP and SQL

So I have created a form that takes in entries and stores them in a SQL database called Warehouse_Maitenence (yes I know it is mispelled and I did that on purpose). The goal for this set of code is to connect to the database and display the table accordingly
<?php include("nav.php") ?>
<body style='background-color:#C0C0C0'>
<style>
.top{
text-align: Left;
}
.Headers_Inputs{
padding-top: 20px;
padding-bottom: 20px;
border-style: solid;
border-width: 1px;
}
input{
width: 100%;
}
.PRIMARY_TABLE{
padding-left: 20px;
padding-right: 20px;
width: 100%;
}
body{
padding-left: 20px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 60px;
}
.PRIMARY_TRAY{
border-style: solid;
border-width: 2px;
}
</style>
<body>
<table width="100%" border="1">
<tr>
<th style="text-align: center; font-size: 16px;"> ID </th>
<th style="text-align: center; font-size: 16px;"> MACHINE </th>
<th style="text-align: center; font-size: 16px;"> LABEL </th>
<th style="text-align: center; font-size: 16px;"> CONDITION </th>
<th style="text-align: center; font-size: 16px;"> ACTION </th>
<th style="text-align: center; font-size: 16px;"> ADDRESS </th>
<th style="text-align: center; font-size: 16px;"> DATE </th>
<th style="text-align: center; font-size: 16px;"> COMMENT </th>
<th style="text-algin: center; font-size: 16px;"> DELETE </th>
</tr>
<?php
$username="";
$password="";
$database="";
$servername = "";
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT ID, MACHINE, LABEL, COND, ACTION, ADDRESS, DATE, COMMENT FROM Warehouse_Maitenence ORDER BY `Warehouse_Maitenence`.`MACHINE` ASC, `Warehouse_Maitenence`.`LABEL` ASC, `Warehouse_Maitenence`.`ID` ASC';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td style='text-align: center; font-size: 16px;' id=".$row["ID"].">".$row["ID"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["MACHINE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["LABEL"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COND"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ACTION"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ADDRESS"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["DATE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COMMENT"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'><input type='button' onclick=delete_entry(".$row["ID"].")</td>";
echo "</tr>";
}
} else { echo "0 results"; }
echo "</table>";
mysqli_close($conn);
?>
So This level of code does work as intended up to the deletion point. I want to be able to load buttons on the end of each row of the table and onclick delete that row from the table and SQL Database.
For the table I have a JS already which works in another set of code I have been working on.
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("dataTable").deleteRow(i);
}
I know that onclick I will need to reconnect to the database (no issues there since it is already in this code) and then run
DELETE FROM `Warehouse_Maitenence` WHERE `Warehouse_Maitenence`.`ID` = whatever row was selected
but I am not sure how to put it all together, since I am not doing a form submission.
Am only good with php and i will advice you to do this..
add this to the list of the table
<td>
<form action="delete.php" method="post" enctype="multipart/form-data">
<input type="text" name="id" value="<?php echo $id; ?>" style="display:none;" />
<input type="submit" name="delete" value="Delete" class="btn btn-danger" />
</form>
</td>
then create a page call delete.php
<?php
if(!isset($_SERVER['HTTP_REFERER'])){ //This is to stop direct access to this delete.php page
header('location: /404.php'); //where to be redirected to
exit;
}
session_start();
include("connect.php"); //your database connection declaration page
if(isset($_POST['delete'])){ //name from the form from table
$id = mysqli_real_escape_string($con, $_POST['id']); //id of the row
$variable = $con->query("DELETE FROM tablename WHERE id = '$id'") or die(mysqli_error($con)); //connecting to the db to the table to delete
if($variable == TRUE){
echo"<script>alert('Deletion Completed!');</script>";
echo"<meta http-equiv='refresh' content='0 url=page.php' />"; //where to be redirected to after deleting.
}else{
echo"<script>alert('Error');</script>";
print_r("Error, Lets go back and Try again");
echo"<meta http-equiv='refresh' content='0 url=page.php' />";
}
exit();
}
?>
I hope am able to solve your issues.!

CSS Changing The Look of A Table

I have a table on my website which contains the columns: User, Title, Description, Join, Update, Delete.
The "User" column's width is way too big as well as the "Title" column. I need help with CSS to set them to something smaller without affecting the width of the other columns as they are perfect as is.
My Code:
<?php
echo "<table>
<tr>
<th>User</th>
<th>Title</th>
<th>Description</th>
<th></th>
<th>Join</th>
<th>Update</th>
<th>Delete</th>
</tr>";
while ($record = mysql_fetch_array($myData))
{
echo "<form action=findGroup.php method=post>";
echo "<div class=joinLink>";
echo "<tr>";
echo "<td>" . "<input type=text name=name value='" . $record['form_user'] . "'/> </td>";
echo "<td>" . "<input type=text name=name value='" . $record['form_name'] . "'/> </td>";
echo "<td>" . "<input type=text name=description value='" . $record['form_description'] . "'/> </td>";
echo "<td>" . "<input type=hidden name=hidden value='" . $record['form_id'] . "'/></td>";
echo "<td><a class=joinLink type=text name=join href='http://localhost:3000'>Join</a></td>";
echo "<td>" . "<input type=submit name=update value='Update" . "'/> </td>";
echo "<td>" . "<input type=submit name=delete value='Delete" . "'/> </td>";
echo "</tr>";
echo "</div>";
echo "</form>";
}
echo "</table>";
CSS:
table {
width: 100%;
font: 17px/1.5 Arial, Helvetica,sans-serif;
text-align: centre;
align: centre;
}
input {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: centre;
align: centre;
height: auto;
overflow: hidden;
}
th {
align: centre;
text-align: centre;
background-color: #4D5960;
color: white;
}
tr {
background-color: #f2f2f2
}
input[type="text"]{
width:100% !important;
line-height:30px;
box-sizing:border-box;
}
a[type="text"]{
width:100% !important;
line-height:30px;
box-sizing:border-box;
}
You can use width property to fix the width of your columns. You can apply width in html directly or using css
HTML
<td width="20%">content</td>
<th width="20%">content</th>
CSS
.custom-class{
width: 20%;
}
<th class="custom-class"></th>
<td class="custom-class"></td>
If you don't mind I would suggest some changes, see below. Check if the form action is either join, update or delete to perform the action. About the data structure you receive when the form is submitted check out the var_dump($_POST); part
PHP:
<?php
var_dump($_POST);
?>
CSS:
table {
width: 100%;
font: 17px/1.5 Arial, Helvetica,sans-serif;
}
input[type="text"] {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
box-sizing: border-box;
padding: 6px 13px;
}
button {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
box-sizing: border-box;
padding: 6px 13px;
}
thead th {
align: center;
text-align: center;
background-color: #4D5960;
color: #ffffff;
}
tbody td {
align: center;
text-align: center;
background-color: #f2f2f2;
}
HTML/PHP:
<form action="index.php" method="POST" enctype="application/x-www-form-urlencoded">
<table>
<thead>
<tr>
<th class="col-name">User</th>
<th class="col-title">Title</th>
<th class="col-description">Description</th>
<th class="col-join">Join</th>
<th class="col-update">Update</th>
<th class="col-delete">Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ([0, 1, 2, 3] as $row) : ?>
<tr>
<td class="col-name">
<input type="hidden" name="user[id][]" value="">
<input type="text" name="user[name][]" value="" size="10">
</td>
<td class="col-title">
<input type="text" name="user[title][]" value="" size="10">
</td>
<td class="col-description">
<input type="text" name="user[description][]" value="" size="10">
</td>
<td class="col-join">
<button type="submit" name="action" value="join">Join</button>
</td>
<td class="col-update">
<button type="submit" name="action" value="update">Update</button>
</td>
<td class="col-delete">
<button type="submit" name="action" value="delete">Delete</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
it seems that there is no problem, everything is the same
table {
width: 100%;
font: 17px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
align: center;
}
input {
width: 100%;
font: 13px/1.5 Arial, Helvetica,sans-serif;
text-align: center;
align: center;
height: auto;
overflow: hidden;
}
th {
align:center;
text-align: center;
background-color: #4D5960;
color: white;
}
tr {background-color: #f2f2f2}
input[type="text"]{width:100% !important; line-height:30px; box-sizing:border-box;}
a[type="text"]{width:100% !important; line-height:30px; box-sizing:border-box;}
https://jsfiddle.net/1qy5v05p/6/
you can also use Bootstrap 4, maybe it will be better https://www.w3schools.com/bootstrap4/bootstrap_forms.asp
There are bunch of jQuery table plugins. Check this out.
My favourites are DataTables, x-Editable and Bootgrid. There are also open sources, you can see them with their Github repo.

When I submit my form I get an answer of Column count doesn't match value count at row 1

I just resolved an issue with my field list(which was careless I admit),but I cannot find a way to solve this issue. I looked at error-column-count-doesn't-match-value already but could not find a solution that worked with my code. Any Suggestions?
attendence-form.php
<?php include( "dbconfig.php"); session_start(); if(!isset($_SESSION[ 'login_user'])) { header( "Location: default.php"); } ?>
<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<meta charset="UTF-8">
<title>User Area|SparkAcad</title>
<style>
body {
background-size: cover;
font-family: Montserrat;
}
.logo {
width: 213px;
height: 36px;
margin: 30px auto;
}
.login-block {
width: 320px;
padding: 20px;
background: #fff;
border-radius: 5px;
border-top: 5px solid #ff656c;
margin: 0 auto;
}
.login-block h1 {
text-align: center;
color: #000;
font-size: 18px;
text-transform: uppercase;
margin-top: 0;
margin-bottom: 20px;
}
.login-block input {
width: 100%;
height: 42px;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #ccc;
margin-bottom: 20px;
font-size: 14px;
font-family: Montserrat;
padding: 0 20px 0 50px;
outline: none;
}
.login-block input#username {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#username:focus {
background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input#password {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
background-size: 16px 80px;
}
.login-block input#password:focus {
background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
background-size: 16px 80px;
}
.login-block input:active,
.login-block input:focus {
border: 1px solid #ff656c;
}
.login-block button {
width: 100%;
height: 40px;
background: #ff656c;
box-sizing: border-box;
border-radius: 5px;
border: 1px solid #e15960;
color: #fff;
font-weight: bold;
text-transform: uppercase;
font-size: 14px;
font-family: Montserrat;
outline: none;
cursor: pointer;
}
.login-block button:hover {
background: #ff7b81;
}
table#header {
width: 100%;
background-color: #ff3366;
}
tr:hover {
background-color: #f5f5f5
}
</style>
</head>
<body>
<div class="logo">
<table id="header" align="center">
<tr>
<td>
Home
</td>
<td>
Student Managment
</td>
<td>
Transcript/SSL
</td>
<td>
</td>
</tr>
</table>
</div>
<div class="login-block">
<!---uper boundAll content should go between these --->
<?php if(isset($_POST[ 'search'])) { $valueToSearch=$ _POST[ 'valueToSearch']; // search in all table columns // using concat mysql function $query="SELECT * FROM `records` WHERE CONCAT(`FName`, `LName`) LIKE '%" .$valueToSearch. "%'"; $search_result=f
ilterTable($query); } else { $query="SELECT * FROM `records`" ; $search_result=f ilterTable($query); } // function to connect and execute the query function filterTable($query) { $connect=m ysqli_connect( "mysql.hostinger.co.uk", "u733142706_root",
"Summer$2000", "u733142706_user"); $filter_Result=m ysqli_query($connect, $query); return $filter_Result; } ?>
<form action="insertmulti.php" method="post" align="center">
<table border="1" align="center">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Mark</th>
</tr>
<!-- populate table from mysql database -->
<?php while($row=m ysqli_fetch_array($search_result)):?>
<tr>
<td>
<?php echo $row[ 'FName'];?>
</td>
<td>
<?php echo $row[ 'LName'];?>
</td>
<td>
<input type="text" placeholder="Present/Absent/Tardy" id="Mark" value="Present">
</td>
</tr>
<?php endwhile;?>
</table>
<input type="Submit" value="Take Attedence">
</form>
<!---lower bound All content should go between these --->
<br>
<br>
Logout
</div>
</body>
</html>
insertmulti.php
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("mysql.hostinger.co.uk", "u733142706_root", "Summer$2000", "u733142706_user");
// Check connection
if ($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$FName = mysqli_real_escape_string($link, $_POST['FName']);
$LName = mysqli_real_escape_string($link, $_POST['LName']);
$Mark = mysqli_real_escape_string($link, $_POST['Mark']);
// attempt insert query execution
$sql = "INSERT INTO attendence (FName, LName, Mark)
SELECT 'FName' ,1
UNION ALL
SELECT 'LName' ,2
UNION ALL
SELECT 'Mark' ,3";
if (mysqli_query($link, $sql))
{
echo "Records added successfully.";
else
{
echo "ERROR: Could not take attendence " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);
?>
You are inserting a table - each select is a row, each comma delimited item in the selects is a field. Each select has 2 fields, while the parenthetical field list has 3 fields. Try something like:
INSERT INTO attendence (FName, LName, Mark)
$FName, $LName, $Mark
To use your select test setup:
INSERT INTO attendence (FName, LName, Mark)
SELECT 'FName1', 'LName1', 1
UNION ALL
SELECT 'FName2', 'LName2', 2
UNION ALL
SELECT 'FName3', 'LName3', 3

search database with php POST and mysql_fetch_ array

I'm trying to create a search form to search through my mysql database.
I succeded to to create the form with one text field and a submit buttom. BUT..
When I leave the text field blank and hit the submit button all results appear from the specific database table on my search.php site.
When i write anything in the text field (text that is included in the names from the database) and hit the submit button no search results appear at all.
As you can see in the code I've been trying to list the search results in two diferent ways. The first one reacts as described above. The last one does not list any search results at all.
Do anybody know why? Do I have a typo somewhere or do I miss anything ind the code?
Thanks in advance.
This is my index.php
<! DOCTYPE html>
<html>
<head>
<meta charset ="UTF-8">
<title> Townin </title>
<link rel="stylesheet" type="text/css" href="Townin/style.css">
<style>
body {background-color: white;}
header{background-color:#6ab47b;
border-color:#599a68;
}
header h1, h2 {margin-left: 20px;
color: white;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h1, h1 {margin-left: 20px;
color: gray;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
p {margin-left: 20px;
color: gray;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
nav a, nav a:visited {
color: white;
background-color: #599a68;}
nav a.selected, nav a:hover{color: black;
background-color:#6ab47b;}
nav {
text-align:center;
padding: 0 0;
margin:20px 0 0;
height: 50 px;
background-color:#599a68;
}
nav ul {
list-style:none;
margin: 0px 10px;
padding: 0px;
}
nav ul li {
display: inline-block;
border-right: 1px solid #6ab47b;
text-align: center;
width: 250px;
padding:0px 0px;
}
nav ul li a {
display: block;
height: 40px;
width: 100%;
line-height:50px;
background-color: #599a68;
}
li a:hover {
background-color: #6ab47b;
}
a:link {
text-decoration: none;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color:gray;
}
a:visited {
color:gray;
}
.button {
background-color:#6ab47b;
color: #fff;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
font-weight: bold;
margin: 0px 20px;
padding:20px 20px;
border-radius: 100 %;
}
.button:visited {
color:white;
}
#bar-knap {
display: inline-block;
max-width: 100%;
border-radius:10%;
height:40%;
width:20%;
margin: 10px 10% 10px 10%;
border-style:solid;
border-color:gray;
}
#spise-knap {
display: inline-block;
max-width: 100%;
border-radius:10%;
height:40%;
width:20%;
margin:10px 10% 10px 10%;
border-style:solid;
border-color:gray;
}
footer {
background-color: #6ab47b;
color: white;
height: 70px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
margin: 20px auto 0px auto;
}
</style>
</head>
<body>
<?php include ("Header2.php"); ?>
<a href="barer.php" >Barer
<img src="img/barknap.jpg" alt="Barer" id="bar-knap">
</a>
<a href="spisesteder.php" >Spisesteder
<img src="img/spiseknap.png" alt="Spisesteder" id="spise-knap">
</a>
<!--Search form-->
<p style="text-align:center;"> Søg mellem barer </p>
<br />
<br />
<form method="post" action="search.php">
<input type="text" name="search" />
<input type="submit" name="submit" value=" Search ">
</form>
<br />
<br />
<footer>
Natalie
</footer>
</body>
</html>
And this is the search.php
<?php
//Connection to phpmyadmin
//Step1
$db = mysql_connect("host","username","password");
if (!$db) {
die("Database connection failed miserably: " . mysql_error());
}
//Step2
$db_select = mysql_select_db("databasename",$db);
if (!$db_select) {
die("Database selection also failed miserably: " . mysql_error());
}
$search = $_POST[search];
//SQL statement to select what to search
$sql="SELECT * FROM Brugerdatabase
WHERE 'Navn' like '%$search%' OR
'Mad genre' like '%$search%' OR
'Beliggenhed' like '%$search%'
ORDER BY Navn ASC";
// Run sql statement
$result = mysql_query($sql, $db) or die(mysql_error());
//Find out how many matches
$number= mysql_num_rows($result);
$pagetitle ="Search Results";
?>
<!doctype html>
<html lang="da">
<head>
<meta charset ="UTF-8">
<title>Søge resultater</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
table {
border-collapse: collapse;
width: 80%;
margin:0 5%;
}
table th, td {
border-bottom:1px solid gray;
height: 50px;
vertical-align: center;
padding: 15px;
text-align: left;
}
tr:hover{background-color:#f5f5f5}
fieldset {
width: 60%;
margin:0 20%;
box-align: center;
}
</style>
</head>
<body>
<?php include ("Header2.php"); ?>
<!--Advanceret søge funktion-->
<p style="text-align:center;"> Søge resultater</p>
<?php
//------------------------------------
//This code inside the lines list the results when nothing is typed in the search field.
//Creates a loop to loop through results
while($row = mysql_fetch_array($result)){
echo "<table><tr><td>"
. $row['1'] .
"</td><td>"
. $row['4'] .
"</td><td>"
. $row['5'] .
"</td><td>"
. $row['6'] .
"</td><td>"
. $row['7'] .
"</td><td>"
. $row['8'] .
"</td></tr></table>";
}
//-----------------------------------
//-------------------------------
//This code inside the lines does not list anything at all..
// loop through results and get variables
while ($row=mysql_fetch_array($result)){
$navn =$row["Navn"];
$genre =$row["Mad genre"];
$beliggenhed =$row["Beliggenhed"];
}
// Tabel with search results
print " <tr>
<td>
<form method=\"post\" action=\"confirmdelete.php\">
<input type=\"hidden\" name=\"sel_record\" value=\"$id\">
<input type=\"submit\" name=\"delete\" value=\" Delete \">
<form>
<form method=\"post\" action=\"updateform.php\">
<input type=\"hidden\" name=\"sel_record\" value=\"$id\">
<input type=\"submit\" name=\"update\" value=\" Edit \">
</form>
</td>
<td><strong>$navn</strong><br />
Mad genre: $genre<br />
Beliggenhed: $beliggenhed</td>
</tr>";
print "</tr></table></body></html>";
//----------------------------
mysqli_close($db);
?>
</body>
</html>
You need to encase your array keys -in this example with single quotes- so:
$search = $_POST['search'];
You should also real_escape string your key with:
$search = mysql_real_escape_string($_POST[search]);
...As a minimum (read below)
Your search SQL you should not encase your column names, so rewrite them as:
$sql="SELECT * FROM Brugerdatabase
WHERE Navn like '%$search%' OR
`Mad genre` like '%$search%' OR
Beliggenhed like '%$search%'
ORDER BY Navn ASC";
You SHOULD encase the middle column within backticks (`) because the column name contains a space. quotes should only go around values, not column or table names.
Now, read again the line LIKE '%$search%' OR ... if $search is empty then this will return a string of <anyvalue><null><anyvalue> ==> %%, so this will return any of the columns that are not NULL because they contain something.
Your code detailed below does not output anything to the browser to display so you will never have anything to show for your query:
//This code inside the lines does not list anything at all..
// loop through results and get variables
while ($row=mysql_fetch_array($result)){
$navn =$row["Navn"];
$genre =$row["Mad genre"];
$beliggenhed =$row["Beliggenhed"];
}
Finally, you really, REALLY should be looking into using MySQLi rather than the standard MySQL as that has been Deprecated and removed from current/future versions of PHP. It's use is really not a good idea and it's riddled with flaws and holes.

search query doesnt work in firefox but does in other browsers?

i have a search query script that works fine in firefox, safari etc but not in chrome.
for whatever reason once the user types in the query and hits submit im noticing the urls are very different.
in chrome i get this which works and is right
http://localhost/ptb1/home.php?query=london&submit.x=0&submit.y=0&submit=Start+Search
but in firefox i get this (notice that stat+search is missing and i dont know why
http://localhost/ptb1/home.php?query=london&submit.x=12&submit.y=10
please can anyone show me why i am getting this problem and how to put it right, thanks.
here's my code:
<form method="get" action="">
<input type="text" id="text" name="query" class="searchbox" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/search.png" height="19" width="20" class="searchbutton" name="submit" value="Start Search" />
</form>
<?php
//PHP CODE STARTS HERE
if(isset($_GET['submit'])){
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="";
$db_tb_atr_name="display_name";
//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 *,MATCH(display_name, location, sex, ethnicity, hobbies, station, age, weight_st, weight_lb, height_ft, height_in, build) AGAINST ('$query' IN BOOLEAN MODE) AS relevance FROM ptb_stats WHERE MATCH(display_name, location, sex, ethnicity, hobbies, station, age, weight_st, weight_lb, height_ft, height_in, build) AGAINST('$query' IN BOOLEAN MODE) ORDER BY relevance DESC LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<div class=\"text\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
echo "<div class=\"spacing\"><img width=35px height= 30px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> ";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</a></div></div>";
}
echo "<div class=\"morebutton-search\">+ view more results</div>";
mysql_close();
}
?>
<style type="text/css">
.boxgridsearch{
width: 35px;
height: 30px;
margin-top:-3px;
margin-left:0px;
margin-right:10px;
float:left;
background:#161613;
border:4px solid #000 .8;
-moz-box-shadow: 0px 0px 8px #CCC;
-webkit-box-shadow: 0px 0px 0px #CCC;
box-shadow: 0px 3px 10px #030303;
}
.suggestionsBox {
position: relative;
left: 12px;
margin: 10px 0px 5px 0px;
width: 172px;
background-color: #FFF;
border: 1px solid #CCC;
color: #fff;
z-index:99;
}
.searchbutton {
padding-top: 2px;
padding-left:5px;
position:absolute;
z-index:70;
}
.spacing {
padding-top: 12px;
padding-bottom:12px;
border-bottom: solid #CCC 1px;
border-top: solid #CCC 1px;
text-align:left;
margin-left:0px;
margin-top:5px;
width:160px;
}
.text {
text-align:left;
margin-left:20px;
margin-top:0px;
width:200px;
}
.search-results {
width: 200px;
height:218px;
float: left;
margin-bottom: 20px;
margin-left: 0px;
margin-top:10px;
text-align: center;
/* [disabled]margin-left: 30px; */
overflow: hidden;
padding-top: 0px;
padding-bottom: 10px;
}
.searchbox {
margin-left: -25px;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
a.search {
color: #646464;
text-decoration: none;
}
a.search:visited {
color: #646464;
}
a.search:hover {
color: #646464;
text-decoration: none;
}
a.search:active {
color: #646464;
}
</style>
You can try using $_SERVER['REQUEST_METHOD'] to determine if the page has been submitted, rather than isset($_GET['submit']) so basically instead:
if(isset($_GET['submit'])){
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
you do:
if($_SERVER['REQUEST_METHOD'] == 'GET'){
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
Browsers will interpret how that form gets submitted differently. The behavior you're seeing with Firefox is actually the drafted standard:
http://www.whatwg.org/specs/web-apps/current-work/multipage/
Where we could delve into styling buttons vs using images (submit, vs image input type, vs button) - the easiest fix is to just add a hidden input value to your form:
<form method="get" action="">
<input type="hidden" name="dosearch" value="1">
<input type="text" id="text" name="query" class="searchbox" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/search.png" height="19" width="20" class="searchbutton" name="submit" value="Start Search" />
</form>
Then just tweak the condition for processing in your PHP:
if(isset($_GET['dosearch'])){
Quick patch.
You should consider a framework if you're going to do lots of this; you get lots of this kind of logic for free, in very clean and stratified code (MVC).

Categories