Hi this is my second time posting on this site. I looked # the other article pertaining to this message and am unable to figure this out.
So i'm trying to dynamically populate the content of a drop down box with data store in the DB.
I'm getting the following error message displayed in the drop down for each record in the DB (3times)
Any help would be appreciated.
Thanks
<!DOCTYPE html>
<?php
$con=mysqli_connect("localhost","root","","project1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// this block would be at the top of your file above the html
$query = "select * from countries";
$country_results = mysqli_query($con,$query);
$number_of_returns = mysqli_num_rows($country_results);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<h1>Insert Site into DB</h1>
<h2 class="button"><a href=/index.html>Home</a></h2>
<h2 class="button"><a href=/insert.html>add site</a></h2>
<h2 class="button"><a href=/delete.html>delete site</a></h2>
<h2 class="button"><a href=/search.html>search site</a></h2>
<form class="insert" action="insert.php" method="post">
<p>Site Info</p>
Site code:<input type="text" name="site_code"><br>
Site name: <input type="text" name="site_name"><br>
Address: <input type="text" name="site_address"><br>
City:<br>
Postal code: <input type="text" name="site_postalcode"><br>
Province: <select name="province"></select><br>
Country: <select name=”country”>
<?php while (($row = mysqli_fetch_row($country_results)) != null){ ?>
<option value=”<?php echo $row[‘country’];?></option>
<?php } ?>
</select><br>
<p>Site Contact Info</p>
Site contact name: <input type="text" name="site_contact_name"><br>
Phone number 1: <input type="number" name="site_contact_number1"><br>
Phone number 2: <input type="number" name="site_contact_number2"><br>
Email address: <input type="email" name="site_contact_email"><br>
<input type="submit">
</form>
</body>
</html>
If you want to access the result as an associative array e.g. $row['country'] you have to use mysqli_fetch_assoc. By using mysqli_fetch_row you can access you results as $row[0]. Also, the are some odd quotes in your code which have to be replaced as well as some other errors in your html e.g. missing value attribute closing quotes in options.This will work:
<select name="country">
<?php while($row = mysqli_fetch_assoc($country_results)){ ?>
<option value="<?php echo $row['title'];?>"><?php echo $row['title'];?></option>
<?php } ?>
</select>
Related
I want to create a simple website to show form input fields based on select values. Here is my codes:
index.php
<?php
include('conn.php');
?>
<!-- For server type field -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script>
// For physical type -------------------------------------
$(document).ready(function () {
toggleFieldsPhys(); // call this first so we start out with the correct visibility depending on the selected form values
// this will call our toggleFields function every time the selection value of our other field changes
$("#type").change(function () {
toggleFieldsPhys();
});
});
// this toggles the visibility of other server
function toggleFieldsPhys() {
if ($("#type").val() === "physical")
$("#physical").show();
else
$("#physical").hide();
}
// -------------------------------------------------------
// For cloud type ----------------------------------------
$(document).ready(function () {
toggleFieldsCloud(); // call this first so we start out with the correct visibility depending on the selected form values
// this will call our toggleFields function every time the selection value of our other field changes
$("#type").change(function () {
toggleFieldsCloud();
});
});
function toggleFieldsCloud() {
if ($("#type").val() === "vps")
$("#vps").show();
else
$("#vps").hide();
}
// -------------------------------------------------------
</script>
<body>
<center>
<h1>Add Server</h1>
<center>
<form method="POST" action="add_process.php" enctype="multipart/form-data" >
<section class="base">
<div>
<label>Server Type</label>
<td>
<select id="type" name="type" required>
<option value="">Choose Type</option>
<option value="physical">Physical</option>
<option value="vps">VPS Hosting</option>
</select>
</td>
</div>
<div class="input-group" id="physical">
<fieldset>
<p>Server Name:
<input type="text" name="servername" required />
</p>
<p>Manufactur:
<input type="text" name="brand" required />
</p>
<p>Type:
<input type="text" name="product" required />
</p>
<p>Serial Number:
<input type="text" name="sn" required />
</p>
</fieldset>
</div>
<br>
<div class="input-group" id="vps">
<fieldset>
<p>Hosting Name
<input type="text" name="hosting" required />
</p>
</fieldset>
</div>
<br>
<div>
<label>Processor</label>
<input type="text" name="processor" autofocus="" required="" />
</div>
<br>
<br>
<div>
<button type="submit">Save</button>
</div>
</section>
</form>
</body>
</html>
add_process.php
<?php
include 'conn.php';
$type = $_POST['type'];
$number = $_POST['number'];
$servername = $_POST['servername'];
$brand = $_POST['brand'];
$product = $_POST['product'];
$sn = $_POST['sn'];
$hosting = $_POST['hosting'];
$processor = $_POST['processor'];
$query = "INSERT INTO try (type, number, servername, brand, product, sn, hosting, processor ) VALUES ('$type', '$number', '$servername', '$brand', '$product', '$sn', '$hosting', '$processor')";
$result = mysqli_query($conn, $query);
if(!$result){
die ("Query Failed: ".mysqli_errno($conn).
" - ".mysqli_error($conn));
} else {
echo "<script>alert('Data Saved');window.location='index.php';</script>";
}
The problem is when I choose Physical type, the data is saved to the database. But when I choose VPS Hosting, I cannot Save to the database because the save button can't be clicked normally.
What should I do if I click VPS Hosting and I fill the form, the data save to the database?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm a beginner in PHP and MySQL and I want to add values that come from an input in HTML, to a MySQL database.
I have to find some things on the Internet but this doesn't work and so I tried to learn a little bit more PHP but I still don't understand why the condition in the code below is not valid:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<title>SHAR-APP</title>
</head>
<body>
<div class='div1'>
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</div>
<?php
echo "test1";
if (isset($_POST['name'])) {
echo "test2";
$mtsqli = mysqli_connect('localhost','the_name_of_my_project','my_password');
mysqli_select_db('project_database', $msqli);
$requete = 'INSERT INTO the_name_of_the_database's_table VALUES(NULL,"' . $_POST['name'] . '","' . $_POST['code'] . '")';
$query = "SELECT * FROM the_name_of_the_database's_table";
echo $_POST['name'];
echo "test3";
}
?>
</body>
</html>
I'm on this for 3 days and I'm really blocked. Maybe I have others mistake in the PHP code. If I can do that with another language i prefer to stay on PHP because I don't want to learn too much languages. If I can do a bridge between HTML and MySQL with Python or JavaScript I'm OK to know that.
THIS PART IS GOOD but another problem is come ...
when i want to connect on my database this error message is display
C:\Users\titou>set PATH=$PATH$;C:\Program Files\MySQL\MySQL Server 8.0\bin
C:\Users\titou>mysql -h localhost -u root -p
Enter password: **********
ERROR 1045 (28000): AccŠs refus‚ pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
its in french but you can see that there is two # instand of one ('root'#'localhost')
First you need to add a Form Method
<form action="" method="POST">
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</form>
The is some kind of class that tells the browser to expect inputs and then on the button click- to treat them.
The Action="" is for initializing where to treat the given inputs.
In your case, since your php code is in the same class as the form, you can leave it blank, either way you should initialize the path you want to send those data.
The Method="POST" is just a Method for treating your data on the web. It is also more secure than GET method which it works too but it's more sensitive since all the data from the inputs it's going to be exposed also in the URL.
Furthermore, I hope you have already installed XAMPP and already created a database in MySQL.
Add the form attribute to your form and in it add a method and an action. Method is needed to tell the form to post, and action is needed to tell the form what to do when you submit.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<title>SHAR-APP</title>
</head>
<body>
<form method="post" action="">
<div class='div1'>
<div class='div2'>
<label for="name">Name of the user:</label>
<input class ='in'type="text" id="name" name="name" size="20">
</div>
<div class='div2'>
<label class = 'label' for="name">Code:</label>
<input class='in' id ='code' type="text" name="code" size="20">
</div>
<div class="div2" id='b'>
<input type="submit" value="send" class='button'>
</div>
</div>
</form>
<?php
if (isset($_POST['name'])) {
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$requete = 'INSERT INTO the_name_of_the_database's_table VALUES(NULL,"' . $_POST['name'] . '","' . $_POST['code'] . '")';
mysqli_query($db, $requete);
$query = "SELECT * FROM the_name_of_the_database's_table";
$Data = mysqli_query($db, $query);
var_dump[$Data];
}
?>
</body>
</html>
Now an important thing to note, never use this in a live website as it would open up your website to SQL injection. You should use prepared statements instead, but for learning purposes, this is fine.
I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
When the page loads i get the undefined index error message enumerated 7 times, I assume it's 1 message per variable.
When I click submit all the form data still get submitted to the DB.
Once I submit the form the Undefined Index error goes away! on page reload.
Weird
<!DOCTYPE html>
<?php
$con=mysqli_connect("localhost","root","","project1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// check variables set
if (isset($_POST['submit']))
{
$site_code = $_POST['site_code'];
$site_name = $_POST['site_name'];
$site_address = $_POST['site_address'];
$site_city = $_POST['site_city'];
$site_postalcode = $_POST['site_postalcode'];
$province = $_POST['province'];
$country = $_POST['country'];
}
// Query from Countries table
$query_countries = "select * from countries";
$country_results = mysqli_query($con,$query_countries);
$number_of_returns_country = mysqli_num_rows($country_results);
// Query from Provinces Table
$query_provinces = "select * from provinces";
$provinces_results = mysqli_query($con,$query_provinces);
$number_of_returns_province = mysqli_num_rows($provinces_results);
//insert form values into sites table
$sql_site="INSERT INTO sites (site_code, site_name, site_address, site_city, site_postalcode, id_province, id_country)
VALUES
('$_POST[site_code]','$_POST[site_name]','$_POST[site_address]','$_POST[site_city]','$_POST[site_postalcode]',$_POST[province],$_POST[country])";
mysqli_query($con,$sql_site);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<h1>Insert Site into DB</h1>
<h2 class="button"><a href=/index.html>Home</a></h2>
<h2 class="button"><a href=/insert.php>add site</a></h2>
<h2 class="button"><a href=/delete.html>delete site</a></h2>
<h2 class="button"><a href=/search.html>search site</a></h2>
<form class="insert" action="insert.php" method="post">
<h3>Site Info</h3>
Site code: <input type="text" name="site_code"><br>
Site name: <input type="text" name="site_name"><br>
Address: <input type="text" name="site_address"><br>
City: <input type="text" name="site_city"><br>
Postal code: <input type="text" name="site_postalcode"><br>
Province: <select name="province">
<?php while($row = mysqli_fetch_assoc($provinces_results)){ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['province'];?></option>
<?php } ?>
</select><br>
Country: <select name="country">
<?php while($row = mysqli_fetch_assoc($country_results)){ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['country'];?></option>
<?php } ?>
</select><br>
<h3>Site Contact Info</h3>
Site contact name: <input type="text" name="site_contact_name"><br>
Phone number 1: <input type="number" name="site_contact_number1"><br>
Phone number 2: <input type="number" name="site_contact_number2"><br>
Email address: <input type="email" name="site_contact_email"><br>
<input type="submit">
</form>
</body>
</html>
This happens because, when you load your page for the first time, data of the form isn't set. When you compile and send it, the error doesn't show up simply because now data is set.
You are getting the error, because there is no data in your $_POST variable. To fix it, you will have to add a name to your submit button:
<input type="submit" name="form_posted">
and enclose your PHP code into this if:
if(isset($_POST['form_posted'])) {
}
Alternatively, you can add this on top of your PHP, to exclude warnings:
error_reporting(E_ALL ^ E_WARNING);
I have a drop down box that contains a list of names and a search field.
The drop down is populated with a list of names from the database and the search field allows you to perform a wild card search.
At the moment the wild card search works as expected but choosing a name from the drop down does not work.
I believe this might be possibly because of some unwanted characters as I am seeing the below in my address bar on the browser having chosen a name from the drop down list and clicked the search button:
http://localhost:81/connect/players/?name=%0D%0A3&text=&action=search
I think that text above (%0D%0A) is causing a problem as my code looks like this:
if (isset($_GET['action']) and $_GET['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
$id = $_GET['name']; // name slightly confusing but does return the id
$text = $_GET['text'];
try
{
$sql = "SELECT id, name, age FROM player
WHERE player.id = '$id'
OR player.name LIKE '%$text%'
GROUP BY player.id";
$s = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching names.' . $e->getMessage();;
include 'error.html.php';
exit();
}
// This is responsible for populating the new player info underneath all
foreach ($s as $row)
{
$names[] = array('id' => $row['id'], 'name' => $row['name'], 'age' => $row['age']);
}
include 'searchprofiles.html.php';
exit();
}
And I believe this is preventing it from comparing the id in the database with the id that is stored in the variable $id.
I have however also just manually stripped %0D%0A out from the address bar and it still doesn't work so perhaps there might be another issue?
It should also be noted that if no value is selected from the drop down and no wild card is entered then all rows are returned.
HTML is as follows:
SEARCHPROFILES.HTML.PHP
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Jokes: Search Results</title>
</head>
<body>
<h1>Search Results</h1>
<?php if (isset($names)): ?>
<table>
<tr><th>Name</th><th>Options</th></tr>
<?php foreach ($names as $name): ?>
<tr>
<td><?php htmlout($name['name']); ?></td>
<td><?php htmlout($name['age']); ?></td>
<td>
<form action="?" method="post">
<div>
<input type="" name="id" value="<?php
htmlout($name['id']); ?>">
<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Delete">
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<p>New search</p>
<p>Return to JMS home</p>
</body>
</html>
BELOW IS THE HTML FOR THE FORM WHERE THE VALUES ARE ADDED.
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Profiles</title>
</head>
<body>
<h1>Manage Profile</h1>
<p>Add new profile</p>
<form action="" method="get">
<p>View player profiles satisfying the following criteria:</p>
<div>
<label for="name">By name:</label>
<select name="name" id="name">
<option value="">Any name</option>
<!-- populates the drop down with names -->
<?php foreach ($names as $name): ?>
<option value="
<?php htmlout($name['id']); ?>">
<?php htmlout($name['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="text">Containing text:</label>
<input type="text" name="text" id="text">
</div>
<div>
<input type="hidden" name="action" value="search">
<input type="submit" value="Search">
</div>
</form>
</body>
</html>
Any help is greatly appreciated.
Thanks
The reason is the line break you have in your form control:
Change
<option value="
<?php htmlout($name['id']); ?>">
<?php htmlout($name['name']); ?>
</option>
To
<option value="<?php htmlout($name['id']); ?>">
<?php htmlout($name['name']); ?>
</option>