search database with php POST and mysql_fetch_ array - php

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.

Related

CSS/HTML Hyperlinks Break From Form CSS

I have a website that displays a list of user accounts on the left side going down in a list that are clickable (hyperlinks) that take you to the user's profile page. Also, I have a search field (form) in the middle of the page where users can just search for someone. However, because of that search field, the usernames displayed on the left side that are aligned with this field are broken, you cannot click on them anymore. The hyperlinks that are not aligned with the search field and are under the alignment of it work. After troubleshooting, I found out that this field in my CSS code was the cause of it: position: absolute;
Taking that field out of the CSS code breaks the position of my search field in the middle of the page and it gets placed at the bottom of the page.
I don't know how to fix this problem without breaking the positioning of my content on the page.
My code:
<?php
include('init.inc.php');
$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); //filter, can remove
$query = mysql_query("SELECT * FROM users WHERE user_first LIKE '%$searchq%' OR user_last LIKE '%$searchq%' OR user_uid LIKE '%$searchq%'");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There was no search found!';
}else{
while($row = mysql_fetch_array($query)){
$uname = $row['user_uid'];
$fname = $row['user_first'];
$lname = $row['user_last'];
$email = $row['user_email'];
$id = $row['user_id'];
$output .= '<div> '.$uname.' '.$fname.' '.$lname.' '.$email.'</div>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns=""http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<section id="showcase1">
<section id="newsletter">
<div class="container">
<h1>ProjectNet Members:</h1>
</div>
</section>
<div class="container">
<div class="userList">
<?php
foreach (fetch_users() as $user){
?>
<p>
<button><a class="userList" href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a></button>
</p>
<?php
}
?>
</div>
</div>
<div class="searchList">
<h2>Search for members</h2>
<form id="search" action="user_list.php" method="post">
<input type="text" name="search" placeholder="Search for members..." />
<input type="submit" value="Search" />
</form>
<?php print("$output");?>
</div>
</section>
</body>
</html>
CSS:
THIS IS THE CSS FOR THE SEARCH FIELD IN THE MIDDLE OF THE PAGE
.searchList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 19px Arial, Helvetica,sans-serif;
text-align: center;
line-height: 35px;
margin: auto;
margin-top: -100px;
position: absolute;
top: 45%;
width: 100%;
}
#search input[type="submit"] {
cursor: pointer;
border: none;
background: #fafafa;
color: #333;
font-weight: bold;
margin: 0 0 5px;
padding: 3px;
font-size: 15px;
font: Arial, Helvetica,sans-serif;
}
#search input[type="text"] {
width: 18%;
border: 1px solid #CCC;
background: #FFF;
margin: 0 0 5px;
padding: 4px;
}
#search input[type="submit"]:hover {
background: #e8491d;
color: #fafafa;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
CSS:
THIS IS THE CSS FOR THE LIST OF USERS ON THE LEFT SIDE OF THE PAGE
.userList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 20px Arial, Helvetica,sans-serif;
margin: auto;
}
.userList button {
background: #333;
width: 20%;
}
There isn't a whole lot of info supplied and its hard to visualize without an image but my guess would be a z-index issue.
Below you can see that the div goes from left to right(width 100%). I assume that this div is sitting on top of your userList div based on the supplied css. This means you probably cant click the hyperlinks below the searchList div.
.searchList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 19px Arial, Helvetica,sans-serif;
text-align: center;
line-height: 35px;
margin: auto;
margin-top: -100px;
position: absolute;
top: 45%;
width: 100%;
}
Try setting a z-index to the userlist div that is higher than the searchList so that you are able to click on the hyperlinks.
.userList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 20px Arial, Helvetica,sans-serif;
margin: auto;
z-index: 1000;
}
Absolute positioning is centering the searchList div in center without effecting other divs in the body. When you remove it, then the searchList div moves to the bottom because that is its location in the body.

CSS/HTML How To Display Form Data On A Specific Spot On The Web Page

I have a web page which displays data on the left side of the page and it goes down in a list.
I need help displaying a form in the middle/right side of the page using CSS. With the current CSS I have for the form, it is displayed at the bottom of the page which is wrong, I want it to be in the middle/right. The form is basically a search field where users can search for members on the website.
I'm bad with CSS, maybe someone can also suggest a cool style for the form? :D
The div class called searchList is where the form is that I want to align to the middle/right
My code:
<?php
include('init.inc.php');
$output = '';
if(isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); //filter, can remove
$query = mysql_query("SELECT * FROM users WHERE user_first LIKE '%$searchq%' OR user_last LIKE '%$searchq%' OR user_uid LIKE '%$searchq%'");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'There was no search found!';
}else{
while($row = mysql_fetch_array($query)){
$uname = $row['user_uid'];
$fname = $row['user_first'];
$lname = $row['user_last'];
$email = $row['user_email'];
$id = $row['user_id'];
$output .= '<div> '.$uname.' '.$fname.' '.$lname.' '.$email.'</div>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns=""http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<section id="showcase1">
<section id="newsletter">
<div class="container">
<h1>ProjectNet Members:</h1>
</div>
</section>
<div class="container">
<div class="userList">
<?php
foreach (fetch_users() as $user){
?>
<p>
<button><a class="userList" href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a></button>
</p>
<?php
}
?>
</div>
</div>
<div class="searchList">
<form id="search" action="user_list.php" method="post">
<input type="text" name="search" placeholder="Search for members..." />
<input type="submit" value="Search" />
</form>
<?php print("$output");?>
</div>
</section>
</body>
</html>
CSS:
.searchList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 20px Arial, Helvetica,sans-serif;
text-align: center;
}
UPDATE:
I managed to get the form in the middle of the screen, I need help moving it slightly towards the right.
CSS:
.searchList {
color: #ffffff;
text-decoration: none;
font-weight: bold;
font: 19px Arial, Helvetica,sans-serif;
text-align: center;
left: 0;
line-height: 35px;
margin: auto;
margin-top: -100px;
position: absolute;
top: 50%;
width: 100%;
float: right;
}
#search input[type="submit"] {
cursor: pointer;
border: none;
background: #fafafa;
color: #333;
font-weight: bold;
margin: 0 0 5px;
padding: 3px;
font-size: 15px;
font: Arial, Helvetica,sans-serif;
}
#search input[type="text"] {
width: 18%;
border: 1px solid #CCC;
background: #FFF;
margin: 0 0 5px;
padding: 4px;
}
}
#search input[type="submit"]:hover {
background: #e8491d;
color: #fafafa;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
Use CSS position property or float property.
To display form on the right of the page use float : right;
form {
float : right;
}
Moving to the right, where the 4th value is the left padding.
padding: 0px 0px 0px 30px;
..alternatively you can use.
padding-left: 30px;
If you want to have better control of the layout structure I suggest you use "grid" which is embedded in css. Search for "css grid".

I forgot the right column during my design. It is possibile to add a right column?

Good morning and happy Saturday to everyone,
Nothing wrong with my page, but I just realized that I made a mistake to create just a main content column. I do need to column for the main content. Left column dedicated to the main content and a right column dedicate to other small stuff, such as banner, and other div.
The right column width has to accomodate an ads banner of width 336px. just to give you a sense of how width the right column must be.
How do I achieve this?
Thank you so much for your help.
Below is my current css code:
<style>
.navmenu {
background-color: #FFFFFF;
}
.navmenu ul {
margin: 0;
padding: 0;
list-style-type: none;
display: flex;
align-items: center;
}
.navmenu li:last-child {
margin-left: auto;
}
.navmenu ul li a {
text-decoration: none;
margin: 4px;
padding: 5px 20px 5px 20px;
color: #FFFFFF;
background: #4285F4;
display: inline-block;
}
.main-content {
padding: 5px 20px 5px 20px;
line-height: 18px;
}
</style>
Thanks this is my entire page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CLIHELP - Help for Command Line Interface</title>
<meta name="description" content="Help for Command Line Interface">
<meta name="author" content="clihelp.org">
<style>
.navmenu {
background-color: #FFFFFF;
}
.navmenu ul {
margin: 0;
padding: 0;
list-style-type: none;
display: flex;
align-items: center;
}
.navmenu li:last-child {
margin-left: auto;
}
.navmenu ul li a {
text-decoration: none;
margin: 4px;
padding: 5px 20px 5px 20px;
color: #FFFFFF;
background: #4285F4;
display: inline-block;
}
.main-content {
padding: 5px 20px 5px 20px;
line-height: 18px;
}
.feedback-search {
font-size: 13px;
}
.feedback-search a {
text-decoration: none;
}
.feedback-search a:hover {
text-decoration: underline;
}
.title {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
font-size: 18px;
}
.title a {
text-decoration: none;
}
.title a:hover {
text-decoration: underline;
}
.tags {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
font-size: 13px;
color: #006621;
}
.script {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
font-size: 13px;
}
</style>
</head>
<body>
<div class="navmenu">
<ul id=menu>
<li>Clihelp</li>
<li><form action='q.php' method='GET'>
<input type='text' size='25' name='search'>
<input type='submit' style='position: absolute; left: -9999px' name='submit'/>
</form></li>
</ul>
</div>
<div class="main-content">
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if (!$button)
echo "you didn't submit a keyword";
else {
if (strlen($search) <= 1)
echo "Search term too short";
else {
echo "<p>Your search - <b>$search</b> ";
mysql_connect("localhost", "username", "password");
mysql_select_db("db");
$search_exploded = explode(" ", $search);
foreach ($search_exploded as $search_each) {
$x++;
if ($x == 1)
$construct .= "(CONCAT(code,cliCommandId) LIKE '%$search_each%' OR os LIKE '%$search_each%' OR title LIKE '%$search_each%' OR tags LIKE '%$search_each%' OR script LIKE '%$search_each%') ";
else
$construct .= "AND (CONCAT(code,cliCommandId) LIKE '%$search_each%' OR os LIKE '%$search_each%' OR title LIKE '%$search_each%' OR tags LIKE '%$search_each%' OR script LIKE '%$search_each%')";
}
$construct = "SELECT * FROM cliCommand WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum == 0)
echo "- did not match any documents.</br></br>Suggestions:</br></br>- Make sure all words are spelled correctly.</br>- Try different keywords.</br>- Try more general keywords.</br>- Search by id.";
else {
echo "- About $foundnum results - <span class='feedback-search'><a href=''>Give us Feedback about this result</a></span><br><br>";
while ($runrows = mysql_fetch_assoc($run)) {
$cliCommandId = $runrows ['cliCommandId'];
$code = $runrows ['code'];
$os = $runrows ['os'];
$title = $runrows ['title'];
$tags = $runrows ['tags'];
$script = $runrows ['script'];
echo "
<div class='title'><a href=''>$title</a></div>
<div class='tags'>$tags</div>
<div class='script'>$script</div><br>
<p>
";
}
}
}
}
?>
</div>
</body>
</html>
Change your structure to add a wrapper like this:
<div class="wrapper">
<div class="main-content">
</div>
<div class="sidebar">
</div>
</div>
Then on your CSS you can either use flexbox on your wrapper or float: left; for your main content and float: right; for your sidebar.

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

how to Add form date to html table

Hi I want to send form data to a php confirmation page. The confirmation page should contain details of registrants.New registrants' detail should be added to the previously registered ones. Here is what I have done, but instead of adding new info to the table, it replaces it. I am new to php and I need advice on how to get round it. it should look like this after submitting:
--------------------------
Registration Conformation |
---------------------------------------------
First name |Last name |Email |
---------------------------------------------
Jack |Fams |mail#time.com |
---------------------------------------------
Matthew |Bills |mail#time.com |
---------------------------------------------
Judith |Kamps |mail#time.com |
---------------------------------------------
<html>
<head>
<title>Registration form
</title>
<style type="text/css">
.button{
box-shadow: 10px 10px 5px #888888;
padding: 5px;
width: 90%;
}
.div{
border: 2px solid black;
width: 28%;
margin: auto;
margin-top: 15%;
padding: 10px;
font-family: Comic Sans;
text-align: center;
}
h1{
text-align: center;
font-family: "Comic Sans", "Comic Sans MS", cursive;
font-weight: bold;
}
table.set{
text-align: center;
margin-left: auto;
margin-right: auto;
width: 60%;
}
td{
text-align: left;
font-family: "Comic Sans", "Comic Sans MS", cursive;
font-weight: bold;
}
</style>
</head>
<body>
<div class="div">
<form method="POST" action="registry.php">
<h1>NewsLetter-Registration</h1>
<table class="set">
<tr><td>First Name:</td><td><input type='text' name='fname' size="25" /></td></tr>
<tr><td>Last Name:</td><td><input type='text' name='lname' size="25"/></td></tr>
<tr><td>E-mail:</td><td><input type='text' name='email' size="25"/></td></tr>
<tr><td></td><td><input class="button" type="submit" name="register" value="Register"></td></tr>
</table>
</form>
</div>
</body>
</html>
registry.php
<html>
<head>
<title>Registration form
</title>
<style type="text/css">
.button{
box-shadow: 10px 10px 5px #888888;
padding: 5px;
width: 90%;
}
.div{
width: 60%;
margin: auto;
margin-top: 15%;
padding: 10px;
font-family: Comic Sans;
text-align: center;
}
h1{
text-align: left;
font-family: "Comic Sans", "Comic Sans MS", cursive;
font-weight: bold;
}
table.set{
text-align: center;
margin-left: auto;
margin-right: auto;
width: 60%;
border-collapse: collapse;
border: 2px solid black;
}
td{
text-align: left;
font-family: "Comic Sans", "Comic Sans MS", cursive;
font-weight: bold;
}
</style>
</head>
<body>
<div class="div">
<table class="set" border="1">
<tr><td colspan="3"><h1>NewsLetter-Registry</h1></td></tr>
<tr><td>First Name:</td><td>Last Name:</td><td>E-mail</td></tr>
<tr><td><?php echo $_POST["fname"]; ?></td><td><?php echo $_POST["lname"]; ?></td><td><?php echo $_POST["email"]; ?></td></tr>;
</table>
</div>
</body>
</html>
Problem
At the moment you're simply sending the form data to a new page and printing it inside the table. It is not stored anywhere.
As you want to achieve this without database, you can use a csv or json file which we cann "data file"
Submit form to registry.php
Append new entry to data file
Load data file and using "while" or "loop" print the data into the table
A quick example for a simple way (not best way) - for large data, appending or database will be your best solution.
registry.php
<?php
$datafile = "data.json";
$data = json_decode(file_get_contents($datafile),1); //make sure to create data.json empty file first
if(isset($_POST['fname'])){
$data[] = array(
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'email' => $_POST['email']
);
file_put_contents($datafile, json_encode($data,1)); //save new file
}
?>
You can then print the table thus:
<table class="set" border="1">
<tr><td colspan="3"><h1>NewsLetter-Registry</h1></td></tr>
<tr><td>First Name:</td><td>Last Name:</td><td>E-mail</td></tr>
<?php
foreach($data as $i=>$entry){
<tr><td><?php echo $entry["fname"]; ?></td>
<td><?php echo $entry["lname"]; ?></td>
<td><?php echo $entry["email"]; ?></td></tr>
} ?>
</table>
This is a basic example and untested so if there's errors let me know and I'll update the code. You'll need to

Categories