for loop over selection option value HTML with PHP - php

Hi I work like loop through the selection option values with PHP instead of duplicating it, from value 500 to 510. Can you kindly show me how to do it?
<body>
<div class="form-group">
<br> <label>Job Description</label>: <b><?php echo $row['job_desc']; ?><br></b>
<select class="form-control" name="job_code">
<option value="500">System Analysis</option>
<option value="501">Programmer</option>
<option value="502">Database Designer</option>
<option value="503">Electrical Engineer</option>
<option value="504">Mechanical Engineer</option>
<option value="505">Civil Engineer</option>
<option value="506">Clerical Support</option>
<option value="507">DSS Analyst</option>
<option value="508">Application Designer</option>
<option value="509">Bio Technician</option>
<option value="510">General Support</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="update" value="Update Data">Save</button>
</div>
</body>
Employee BDJob DB
<?php
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, 'amaz');
if (isset($_POST['update'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sex = $_POST['sex'];
$emp_salary = $_POST['emp_salary'];
$dept_name = $_POST['dept_name'];
$job_code = $_POST['job_code'];
$query = "UPDATE employee
SET employee.first_name='$_POST[first_name]',employee.last_name='$_POST[last_name]',employee.sex='$_POST[sex]',employee.emp_salary='$_POST[emp_salary]',employee.dept_id='$_POST[dept_name]',employee.job_code='$_POST[job_code]'
WHERE employee.emp_num='$_POST[id]'";
$query_run = mysqli_query($connection, $query);
}
?>

You make a SELECT query that looks at the jobs table:
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'amaz';
$mysql = new mysqli($server, $user, $pass, $db);
if ($mysql->connect_error !== null) {
printf("Connect failed: %s\n", $mysql->connect_error);
exit;
}
$select = $mysql->query("SELECT * FROM jobs WHERE job_code > 409 and job_code < 511");
$jobs = [];
while ($row = $select->fetch_array(MYSQLI_ASSOC)) {
$jobs[] = $row;
}
?>
<select class="form-control" name="job_code">
<?php foreach ($jobs as $job): ?>
<option value="<?=$job['job_code']?>"><?=$job['job_desc']?></option>
<?php endforeach; ?>
</select>
This will output:
<select class="form-control" name="job_code">
<option value="500">System Analysis</option>
<option value="501">Programmer</option>
<option value="502">Database Designer</option>
...etc
</select>

Related

Undefined index on my checkbooking.php

This is the error I have:
Below is my php booking page . I have trouble solving the error. I faced in check booking page below. I can't find the undefined index error for service, eventdate, Customer ID. Can you help me?
<?php
session_start();
if(!isset($_SESSION['MM_Username']))
{
header("Location:login.php");
}
$un = $_SESSION["MM_Username"];
$conn = mysqli_connect("localhost", "root", "" , "m3_162931g_db");
$sql = "SELECT * from customer where Username='$un'";
$user = mysqli_query($conn, $sql);
$oneUser = mysqli_fetch_assoc($user);
?>
<content>
<h1>Booking Page</h1>
<form action="checkbook.php" method="post" style="border:1px solid #ccc">
<div class="container">
<label><b>Services :</b></label>
<select name ="service" placeholder="Select Service">
<option value="1" >Wedding</option>
<option value="2" >Same Day Printing</option>
<option value="3" >Instagram Images</option>
<option value="4" >Family Photo</option>
<option value="5" >Green Screen Photography</option>
<option value="6" >Corporate Event</option>
<option value="7" >Business Portrait</option>
<option value="8" >Advertisment Printing</option>
<option value="9" >Award Ceremonies</option>
</select>
<br>
<label><b>Date :</b></label>
<input type="date" name="eventdate" required>
<input type="text" id="hide" name="CustomerID" value="<?php echo
$oneUser['CustomerID']; ?>" hidden="hidden" >
<div class="clearfix">
<button type="reset" class="cancelbtn">Cancel</button>
<button type="submit" class="signupbtn">Book</button>
</div>
</div>
</form>
this is my checkbook php page .Its shows undefined index error for service ,eventdate, customerID
<?php
session_start();
$service = $_POST['service'];
$eventdate = $_POST['eventdate'];
$customerID = $_POST['CustomerID'];
$bs = "B";
$conn = mysqli_connect("localhost", "root", "" , "m3_162931G_db");
$sql = "insert into booking(ProductID,CustomerID,Booking_Date,status)
values ('$service','$customerID','$eventdate', '$bs')";
$result = mysqli_query($conn,$sql);
if ($result){
header("Location:orderhistory.php");
}
?>
you need to check your variable like that
if (isset($_POST['service'])) {
$service = $_POST['service'];
$eventdate = $_POST['eventdate'];
$customerID = $_POST['CustomerID'];
$bs = "B";
$conn = mysqli_connect("localhost", "root", "" , "m3_162931G_db");
$sql = "insert into booking(ProductID,CustomerID,Booking_Date,status)
values ('$service','$customerID','$eventdate', '$bs')";
$result = mysqli_query($conn,$sql);
if ($result){
header("Location:orderhistory.php");
}
}

Mysqli SELECT JOINS multiple WHERES

I have a problem with multiple wheres in a consult to database mysqli. I don't know how to make it. I'm reading about JOINS for multiple consults, but I don't know it very well, and it still doesn't work. This is my code:
$status = mysqli_escape_string($con, $_POST['status']);
$type = mysqli_escape_string($con, $_POST['type']);
$beds = mysqli_escape_string($con, $_POST['beds']);
$baths = mysqli_escape_string($con, $_POST['baths']);
$query = "SELECT * FROM properties WHERE beds LIKE '%".$beds."%' OR baths LIKE '%".$baths."%' " ;
I need to obtain all results that have beds, baths, status, types, etc, but it does not work with the actual SELECT. I already made some tries with JOINS, but that did not work the way I expected. This function gives all the results but not filtered. Thank you for any help!
EDIT:
This is all my code, my form
<form action="" id="amenities-form" name="amenities-form" data-name="Amenities Form" class="top-search-form-left" method="POST">
<select id="status" name="status" data-name="status" class="select-search-form w-select">
<option value="sale">For Sale</option>
<option value="rent">For Rent</option>
</select>
<select id="type" name="type" data-name="type" class="select-search-form w-select">
<option value="">Type of property</option>
<option value="condo">Condo</option>
<option value="house">House</option>
<option value="commercial">Commercial</option>
<option value="lot">Lot</option>
<option value="villa">Villa</option>
<option value="business">Business</option>
<option value="fractional">Fractional</option>
<option value="boat">Boat</option>
</select>
<input type="text" class="input-search-form w-input" maxlength="256" name="beds" data-name="beds" placeholder="Beds" id="beds">
<input type="text" class="input-search-form w-input" maxlength="256" name="baths" data-name="baths" placeholder="Baths" id="baths">
<input type="submit" value="Go" class="btn-search-left w-button">
</form>
<form action="" id="search-form" name="search-form" data-name="Search Form" class="top-search-form-right" method="POST">
<input type="text" class="input-search-form w-input" maxlength="256" name="search" data-name="search" placeholder="Search..." id="search-word">
<input type="submit" value="Lupa" class="btn-search-right w-button">
</form>
This is my code of php
if (!empty($_POST)) {
$con = new mysqli($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
printf("Falló la conexión failed: %s\n", $con->connect_error);
exit();
}
if (isset($_POST['search'])) {
$search = mysqli_escape_string($con, $_POST['search']);
$query = "SELECT * FROM properties WHERE title LIKE '%".$search."%'" ;
}else{
$status = mysqli_escape_string($con, $_POST['status']);
$type = mysqli_escape_string($con, $_POST['type']);
$beds = mysqli_escape_string($con, $_POST['beds']);
$baths = mysqli_escape_string($con, $_POST['baths']);
$query = "SELECT * FROM properties WHERE beds LIKE '%".$beds."%' OR baths LIKE '%".$baths."%' " ;
}
$res = $con->query($query);
if (!$res) {
trigger_error('Invalid query: ' . $con->error);
}
if ($res->num_rows) {
while ($row = $res->fetch_object()) {
//$status_name = $con->query("SELECT * FROM status WHERE id = {$row->status_id}");
//$status_name = $status_name->fetch_object();
//echo "{$row->title} ({$status_name->title})<br>";
echo "{$row->title} ({$row->status_id})<br>";
}
}else{
echo "No results";
}
$res->free();
$con->close();
}else{
echo 'No results';
}
I will try some suggest you did

Php form (using PDO) to insert into other tables (foreign keys)

I need some help, I am trying to insert into multiple tables using PDO - Can someone see what I am doing wrong - I am not getting a parse errors (nor did I set up an asset error):
Here is my form:
addcontact.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Contact</title>
<link rel="stylesheet" href="css/table.css" type="text/css" />
</head>
<body>
<div class="CSS_Table_Example" style="width:500px;height:350px;">
<center>
<form action="insert.php" method="post">
<p>
<td>
<tr><label for="ContactName">Contact Name:</label>
<input type="text" name="ContactName" id="ContactName">
</tr></p>
<p>
<tr> <label for="ContactTypeId">Contact Type:</label>
<select name="ContactTypeId">
<option value="1">Contact</option>
<option value="2">Organization</option>
</select>
</p>
<p>
<td>
<tr> <label for="AddressTypeId">Address Type:</label>
<select name="AddressTypeId">
<option value="1">Home</option>
<option value="2">Office</option>
<option value="3">Other</option>
</select>
</p>
<p>
<tr><label for="Address1">Address 1:</label>
<input type="text" name="Address1" id="Address1">
</tr></p>
<p>
<tr><label for="Address2">Address 2:</label>
<input type="text" name="Address2" id="Address1">
</tr></p>
<p>
<tr><label for="City">City:</label>
<input type="text" name="City" id="Address1">
</tr></p>
<tr> <label for="StateId">State:</label>
<select name="StateId">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">Califorina</option>
<option value="6">Colorado</option>
<option value="7">Connecticut</option>
<option value="8">Delaware</option>
<option value="9">District of Columbia</option>
<option value="10">Florida</option>
<option value="11">Georgia</option>
<option value="12">Hawaii</option>
<option value="13">Idaho</option>
<option value="14">Illinois</option>
<option value="15">Indiana</option>
<option value="16">Iowa</option>
<option value="17">Kansas</option>
<option value="18">Kentucky</option>
<option value="19">Louisana</option>
<option value="20">Maine</option>
<option value="21">Maryland</option>
<option value="22">Massachusetts</option>
<option value="23">Michigan</option>
<option value="24">Minnesota</option>
<option value="25">Mississippi</option>
<option value="26">Missouri</option>
<option value="27">Montana</option>
<option value="28">Nebraska</option>
<option value="29">Nevada</option>
<option value="30">New Hampshire</option>
<option value="31">New Jersey</option>
<option value="32">New Mexico</option>
<option value="33">New York</option>
<option value="34">North Carolina</option>
<option value="35">North Dakota</option>
<option value="36">Ohio</option>
<option value="37">Oklahoma</option>
<option value="38">Oregon</option>
<option value="39">Pennsylvania</option>
<option value="40">Rhode Island</option>
<option value="41">South Carolina</option>
<option value="42">South Dakota</option>
<option value="43">Tennessee</option>
<option value="44">Texas</option>
<option value="45">Utah</option>
<option value="46">Vermont</option>
<option value="47">Virginia</option>
<option value="48">Washington</option>
<option value="49">West Virginia</option>
<option value="50">Wisconsin</option>
<option value="51">Wyoming</option>
</select>
</tr> </p>
<input type="submit" value="Add Record">
</tr></td>
</form>
</table>
</body>
</html>
Here is insert.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "crm";
//making an array with the data received, to use as named placeholders for INSERT by PDO.
$data = array('ContactName' => $_POST['ContactName'] , 'ContactTypeId'
=> $_POST['ContactTypeId'],
'ContactId'=> $_POST['ContactId'],'AddressTypeId'=>
$_POST['AddressTypeId'],'Address1'=>$_POST['Address1'],
'Address2'=>$_POST['
Address2'],'City'=>$_POST['City'],'StateId'=>$_POST['StateId']);
try {
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname",
$username,$password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$currentID = mysql_inserted_id();
// query with named placeholders to avoid sql injections
$query = "INSERT INTO Contacts (ContactName, ContactTypeId)
VALUES(:ContactName, :ContactTypeId )";
$query2= "INSERT INTO
Addresses(ContactId,AddressTypeId,Address1,Address2,City,StateId)
VALUES(:$currentID,:AddressTypeId,:Address1,:Address2,:City,:StateId)";
//statement handle $sth
$sth = $dbh->prepare($query);
$sth->execute($data);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
?>
You need to create two arrays $data for $query & $data1 for $query1 and need use $dbh->lastInsertId() for last id. Use the below code. I think it will work:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "crm";
//making an array with the data received, to use as named placeholders for INSERT by PDO.
$data = array('ContactName' => $_POST['ContactName'] , 'ContactTypeId'
=> $_POST['ContactTypeId']);
$data1=array('AddressTypeId'=>$_POST['AddressTypeId'],'Address1'=>$_POST['Address1'],
'Address2'=>$_POST['
Address2'],'City'=>$_POST['City'],'StateId'=>$_POST['StateId']);
try {
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname",
$username,$password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// query with named placeholders to avoid sql injections
$query = "INSERT INTO Contacts (ContactName, ContactTypeId)
VALUES(:ContactName, :ContactTypeId )";
$sth = $dbh->prepare($query);
$sth->execute($data);
$currentID = $dbh->lastInsertId();
$query2= "INSERT INTO
Addresses(ContactId,AddressTypeId,Address1,Address2,City,StateId)
VALUES($currentID,:AddressTypeId,:Address1,:Address2,:City,:StateId)";
$sth = $dbh->prepare($query2);
$sth->execute($data1);
//statement handle $sth
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
?>

Checking seat numbers correctly in MySql table

I am developing a script for a table called 'minivan'. Here is the table structure sql fiddle link. Admin provides seats for minivan.
I want to check if seats are available from this search form: search form link The method is GET.
First i want to check the date [this is format of date: 2015-02-16 for example] that a user submits. If the date matches i want to proceed with 'from' and 'to' location that a user chooses. If records are found i want to check what is the total passenger from the dropdown list for different ages that a users provides.
If the total number of passenger does not exceed the seat number for a particular day that a user chooses, i want to echo "seats are available". and proceed with other parts that i am planning to develop later.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_GET['submit'])) {
$date = $_GET['date_up'];
$query = "SELECT date_up FROM minivan WHERE date_up = '$date'";
$result = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($result);
//print_r($numrows);
if ($numrows)
{
$startPlace = $_GET['startpoint'];
$query2 = "SELECT startpoint FROM minivan WHERE startpoint = '$startPlace'";
$result2 = mysqli_query($conn, $query2);
//print_r($result2);
$numrows2 = mysqli_num_rows($result2);
$endPlace = $_GET['endpoint'];
$query3 = "SELECT endpoint FROM minivan WHERE endpoint = '$endPlace'";
$result3 = mysqli_query($conn, $query3);
//print_r($result2);
$numrows3 = mysqli_num_rows($result3);
if ($numrows2 && $numrows3) {
$adult = $_GET['adult'];
$juvenile = $_GET['juvenile'];
$kids = $_GET['kids'];
$child = $_GET['child'];
$totalPassenger = $adult + $juvenile + $kids + $child ;
$seatQuery = "SELECT seat FROM minivan WHERE seat > $totalPassenger";
$result4 = mysqli_query($conn, $seatQuery);
$numrows4 = mysqli_num_rows($result4);//shows error here how to fix it?
if ($numrows4) {
while ($row = mysqli_fetch_row($result4)) {
$seats = $row[0];
echo "$seats are availble for the $date you selected";
}
} else {
echo "no seats are found";
}
}
} else {
"start point and end point does not match";
}
}
$conn->close();
?>
It shows error here $numrows4 = mysqli_num_rows($result4);//shows error here how to fix it?
here is the form i am trying to search with
<form action="" method="GET">
From
<select name="startpoint" id="from">
<option >Hat Yai Airport</option>
<option >Pak Bara</option>
<option >Kohlipe</option>
</select>
To
<select name="endpoint" id="to">
<option >Pak Bara</option>
<option >Hat Yai Airport</option>
<option >Kohlipe</option>
</select>
<label for="Date">Date</label>
<input type="text" name="date_up" id="datepicker">
<h4 style="margin-top: 30px;">Passengers</h4>
Adults
<select name="adult" id="from">
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
<option >5</option>
</select>
< 12 years
<select name="juvenile" id="to">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
<option >4</option>
</select>
< 7 years
<select name="kids" id="from">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
< 3 years
<select name="child" id="to">
<option >0</option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
<input type="submit" value="Search" class="submit" name="submit">
Can't really figure out whats wrong with this. I am beginner in php and mysql. Please help me solve this.

Cannot insert data in the database using option(textarea)

I got this code but it is not inserting the content of the option (textarea) into the database.
connection.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "copy";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
?>
submit.php
<?php
include 'connection.php';
$foodA = $_POST['foodA'];
$foodB = $_POST['foodB'];
$foodC = $_POST['foodC'];
$foodD = $_POST['foodD'];
$foodE = $_POST['foodE'];
if(!$_POST['submit']) {
echo "please fill out the form";
header('Location: select.html');
}
else {
$sql = "INSERT INTO remove(foodA, foodB, foodC, foodD, foodE) VALUES (?,?,?,?,?);";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt,"sssss",$foodA,$foodB,$foodC,$foodD,$foodE);
mysqli_stmt_execute($stmt);
echo "User has been added!";
header('Location: select.html');
}
select.html
<html lang="en">
<title>Catering Service</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/js.1.js" type="text/javascript"></script>
</head>
<body>
<form action="submit.php" method="post">
<select multiple="multiple" class="options" id="textarea" >
<option value="foodA">foodA</option>
<option value="foodB">foodB</option>
<option value="foodC">foodC</option>
<option value="foodD">foodD</option>
<option value="foodE">foodE</option>
</select>
<button type="button" id="copy" onclick="yourFunction()">Copy</button>
<button type="button" id="remove" onclick="yourFunction()">Remove</button>
<select id="textarea2" multiple class="remove" >
<input type="submit" name="submit" />
</form>
</select>
</html>
You need to name the <select> so you can use the data.
name="food[]"
Like this
<select multiple="multiple" name="food[]" class="options" id="text area" >
<option value="foodA">foodA</option>
<option value="foodB">foodB</option>
<option value="foodC">foodC</option>
<option value="foodD">foodD</option>
<option value="foodE">foodE</option>
</select>
Then if you want the value to be 0 or 1, depending on selected or not, you can use the following to replace this:
$foodA = $_POST['foodA'];
$foodB = $_POST['foodB'];
$foodC = $_POST['foodC'];
$foodD = $_POST['foodD'];
$foodE = $_POST['foodE'];
to
$foodA = 0;
$foodB = 0;
$foodC = 0;
$foodD = 0;
$foodE = 0;
foreach ($_POST['food'] as $value) {
if($value == 'foodA')
$foodA = 1;
if($value == 'foodB')
$foodB = 1;
if($value == 'foodC')
$foodC = 1;
if($value == 'foodD')
$foodD = 1;
if($value == 'foodE')
$foodE = 1;
}
You need to name your select with name=food or something so it will be in $_POST['food']. Each option in the select does not show up in $_POST, only what is selected will be in the name of the select. Each option is not it's own thing.
<select multiple="multiple" name="food" class="options" id="textarea" >
<option value="foodA">foodA</option>
<option value="foodB">foodB</option>
<option value="foodC">foodC</option>
<option value="foodD">foodD</option>
<option value="foodE">foodE</option>
</select>
When you save the data it will have:
$_POST['food'] with the value of 'foodA' if multiples, it will be 'foodA, foodB'

Categories