Connecting tables PHP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm a number of days busy with a PHP script.
This is what I want:
- People can type their ZIP code. If their ZIP code is in table1 than do .. if their ZIP code is in table2 than do... If their ZIP code is in none of the tables than ...
- I can insert ZIP code with other data example name.
But I don't know how to build this. I have searched on the internet I have tried to get what I have. But the function to search in other tables en than do that. It don't work for me
This is what I have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
php database filtering
add a comment |
I'm a number of days busy with a PHP script.
This is what I want:
- People can type their ZIP code. If their ZIP code is in table1 than do .. if their ZIP code is in table2 than do... If their ZIP code is in none of the tables than ...
- I can insert ZIP code with other data example name.
But I don't know how to build this. I have searched on the internet I have tried to get what I have. But the function to search in other tables en than do that. It don't work for me
This is what I have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
php database filtering
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Devon
Nov 16 '18 at 13:07
add a comment |
I'm a number of days busy with a PHP script.
This is what I want:
- People can type their ZIP code. If their ZIP code is in table1 than do .. if their ZIP code is in table2 than do... If their ZIP code is in none of the tables than ...
- I can insert ZIP code with other data example name.
But I don't know how to build this. I have searched on the internet I have tried to get what I have. But the function to search in other tables en than do that. It don't work for me
This is what I have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
php database filtering
I'm a number of days busy with a PHP script.
This is what I want:
- People can type their ZIP code. If their ZIP code is in table1 than do .. if their ZIP code is in table2 than do... If their ZIP code is in none of the tables than ...
- I can insert ZIP code with other data example name.
But I don't know how to build this. I have searched on the internet I have tried to get what I have. But the function to search in other tables en than do that. It don't work for me
This is what I have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Plusgas - Postcode invoeren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="bootstrap/bootstrap.min.js"></script>
<style>
body{
font-family: Courgette;
}
.submit{
background-color: purple;
color:white;
text-size:24px;
padding: 6px;
border-radius: 5px;
border:1px solid white;
font-size: 24px;
}
.submit:hover{
background-color: white;
color: purple;
box-shadow: 0px 0px 20px white;
}
h1{
font-size: 14px;
}
td,th{
padding: 4px;
text-align: center;
}
</style>
</head>
<?php
$servername = "localhost";
$username="*";
$password="*";
$dbname="*";
$id="";
$postcode="";
$provincie="";
$website="";
$contactpersoon="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[1]=$_POST['postcode'];
$data[2]=$_POST['provincie'];
$data[3]=$_POST['website'];
$data[4]=$_POST['contactpersoon'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM postcodes WHERE id = '$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$id = $rows['id'];
$postcode = $rows['postcode'];
$provincie = $rows['provincie'];
$website = $rows['website'];
$contactpersoon = $rows['contactpersoon'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `postcodes`(`postcode`, `provincie`, `website`, `contactpersoon`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("Postcode is toegevoegd");
}else{
echo("Postcode is niet toegevoegd");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
?>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-4">
<form method ="post" action="">
<h1>ID nummer (voor filterern)</h1>
<input type="number" name="id" class="form-control" placeholder="ID No. /Automatic Number Genrates" value="<?php echo($id);?>" disabled>
<div class="form-group row">
<div class="col-xs-6">
<h1>Postcode</h1>
<input type="text" name="postcode" class="form-control" placeholder="Postcode" value="<?php echo($postcode);?>" required>
</div>
<div class="col-xs-6">
<h1>Provinicie</h1>
<input type="text" name="provincie" class="form-control" placeholder="Provinicie" value="<?php echo($provincie);?>" required>
</div>
</div>
<h1>Website</h1>
<select name="website" class="form-control" value="<?php echo($website);?>">
<option value="websiteZH">Website Zuid-Holland</option>
<option value="websiteNH">Website Noord-Holland</option>
<option value="websiteZL">Website Zeeland</option>
<option value="websiteUT">Website Utrecht</option>
</select>
</div>
<div class="col-xs-6">
<h1>Contactpersoon</h1>
<input type="text" name="contactpersoon" class="form-control" placeholder="Contactpersoon" value="<?php echo($contactpersoon);?>" required>
</div>
</div>
<div>
<input type="submit" class="btn btn-success btn-block btn-lg" name="insert" value="Add">
</div>
</form>
</div>
<div class="col-lg-8">
<h2>Student Data</h2>
<?php
$servername = "localhost";
$username = "*";
$password = "*";
$dbname = "*";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id,postcode,provincie,website,contactpersoon FROM postcodes";
$result = $conn->query($sql);
echo "<table border='1'>
<tr>
<th>Search ID</th>
<th>Postcode</th>
<th>Provinicie</th>
<th>Website</th>
<th>Contactpersoon</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['provincie'] . "</td>";
echo "<td>" . $row['website'] . "</td>";
echo "<td>" . $row['contactpersoon'] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</div>
</div>
</body>
</html>
php database filtering
php database filtering
edited Nov 16 '18 at 15:21
Zain Farooq
2,05321030
2,05321030
asked Nov 16 '18 at 13:04
MooelbMooelb
165
165
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Devon
Nov 16 '18 at 13:07
add a comment |
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Devon
Nov 16 '18 at 13:07
1
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Devon
Nov 16 '18 at 13:07
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Devon
Nov 16 '18 at 13:07
add a comment |
1 Answer
1
active
oldest
votes
I'm not realy sure what you want to achieve, what goes wrong, but for sure i see one problem - your function getData() returns array indexed 1..4. Then you get it in search part and you search for $info[0], and that is never set.
2nd of all - this is dangerous way of coding. If someone mean will use your code, he can use SQL injections. Not to manting, that even by mystake someone can use ' (apostroph) and crash SQL query. You should always escape data passed to queries, ie by using real_escape_string() or prepared statements.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338491%2fconnecting-tables-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not realy sure what you want to achieve, what goes wrong, but for sure i see one problem - your function getData() returns array indexed 1..4. Then you get it in search part and you search for $info[0], and that is never set.
2nd of all - this is dangerous way of coding. If someone mean will use your code, he can use SQL injections. Not to manting, that even by mystake someone can use ' (apostroph) and crash SQL query. You should always escape data passed to queries, ie by using real_escape_string() or prepared statements.
add a comment |
I'm not realy sure what you want to achieve, what goes wrong, but for sure i see one problem - your function getData() returns array indexed 1..4. Then you get it in search part and you search for $info[0], and that is never set.
2nd of all - this is dangerous way of coding. If someone mean will use your code, he can use SQL injections. Not to manting, that even by mystake someone can use ' (apostroph) and crash SQL query. You should always escape data passed to queries, ie by using real_escape_string() or prepared statements.
add a comment |
I'm not realy sure what you want to achieve, what goes wrong, but for sure i see one problem - your function getData() returns array indexed 1..4. Then you get it in search part and you search for $info[0], and that is never set.
2nd of all - this is dangerous way of coding. If someone mean will use your code, he can use SQL injections. Not to manting, that even by mystake someone can use ' (apostroph) and crash SQL query. You should always escape data passed to queries, ie by using real_escape_string() or prepared statements.
I'm not realy sure what you want to achieve, what goes wrong, but for sure i see one problem - your function getData() returns array indexed 1..4. Then you get it in search part and you search for $info[0], and that is never set.
2nd of all - this is dangerous way of coding. If someone mean will use your code, he can use SQL injections. Not to manting, that even by mystake someone can use ' (apostroph) and crash SQL query. You should always escape data passed to queries, ie by using real_escape_string() or prepared statements.
answered Nov 16 '18 at 13:19
MatKusMatKus
358
358
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338491%2fconnecting-tables-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Devon
Nov 16 '18 at 13:07