Pull and post data from MySQL using dropdown menu - php

I'm a complete newbie in php & MySQL
Basically what I want to do is be able to retrieve data from a table
in MySQL database and put it in a drop down menu .
After I fill in the other fields I want to data from the drop down menu to be written to another table in the same database .
This is my insert.php file
<?php
#### INSERTS A SINGLE CUSTOMER IN Company-->Customer Database with UTF8 Change for special German Characters - Cyrilic Doesnt work - Other change then utf8 ???
#### Getting Data from Index.php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "company";
//making an array with the data recieved, to use as named placeholders for INSERT by PDO.
#### Getting DAta from Index.php
$data = array('CustomerName' => $_POST['CustomerName'] , 'Address1' => $_POST['Address1'], 'Address2' => $_POST['Address2'], 'City' => $_POST['City'], 'PostCode' => $_POST['PostCode'], 'CountryID' => $_POST['CountryID'], 'ContactName' => $_POST['ContactName'], 'ContactEmail' => $_POST['ContactEmail'], 'ContactPhone' => $_POST['ContactPhone'], 'ContactFax' => $_POST['ContactFax'], 'Website' => $_POST['Website']);
try {
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); ### Database Connect with Special characters Change
// 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 customer (CustomerName, Address1, Address2, City, PostCode, CountryID, ContactName, ContactEmail, ContactPhone, ContactFax, Website )
VALUES (:CustomerName, :Address1, :Address2, :City, :PostCode, :CountryID, :ContactName, :ContactEmail, :ContactPhone, :ContactFax, :Website )";
//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;
?>
And this is the source from the html page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Product</title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="CustomerName">CustomerName:</label>
<input type="text" name="CustomerName" id="CustomerName">
</p>
<p>
<label for="Address1">Address 1:</label>
<input type="text" name="Address1" id="Address1">
</p>
<p>
<label for="Address2">Address 2:</label>
<input type="text" name="Address2" id="Address2">
</p>
<p>
<label for="City">City:</label>
<input type="text" name="City" id="City">
</p>
<p>
<label for="PostCode">Post Code:</label>
<input type="text" name="PostCode" id="PostCode">
</p>
<p>
<label for="CountryID">Country ID:</label>
<input type="text" name="CountryID" id="CountryID">
</p>
<p>
<label for="ContactName">Contact Name:</label>
<input type="text" name="ContactName" id="ContactName">
</p>
<p>
<label for="ContactEmail">Contact Email:</label>
<input type="text" name="ContactEmail" id="ContactEmail">
</p>
<p>
<label for="ContactPhone">Contact Phone:</label>
<input type="text" name="ContactPhone" id="ContactPhone">
</p>
<p>
<label for="ContactFax">Contact Fax:</label>
<input type="text" name="ContactFax" id="ContactFax">
</p>
<p>
<label for="Website">Website:</label>
<input type="text" name="Website" id="Website">
</p>
<input type="submit" value="Add Records">
</form>
</body>
</html>
So I wanna add this php code that pulls out the country list and prefixes for the phone numbers and then when I hit the submit button the actual output of the drop down menu to we written in the customers table
<p>
<label for="CountryID">Country:</label>
<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";
$con_qnt = mysqli_connect($servername, $username, $password, $dbname);
if(!mysqli_connect("localhost","root",""))
{
die('oops connection problem ! --> '.mysqli_connect_error());
}
if(!mysqli_select_db($con_qnt, "company"))
{
die('oops database selection problem ! --> '.mysqli_connect_error());
}
$sql = "SELECT * FROM country";
$result = mysqli_query($con_qnt, "SELECT * FROM country" );
echo "<select name='label'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['CountryName' ] . "'>" . $row['CountryName' ] . " (" .$row['PhonePrefix' ] . ")" . "</option>";
}
echo "</select>";
?>
<name="CountryID" id="CountryID">
</p>
I don't even know if this is doable - I searched for long time but couldn't find anything that is kind of what I need . Mostly I found hard coded html dropdown menus . In fact hard coding this will work for the countries but it wont work if I want it to be able to show lets say products in a drop down menu . Thank you all in advance .

Hi I just made a quick sample to answer your question on how to populate dropdown using data from database.
In this example, I used
JQuery's AJAX Function
so first, for the backend, (this is just a quick sample to select data. Do not use this as a pattern for your future codes
<?php
$dbh = new PDO("mysql:host=localhost;dbname=dbhelp", 'root', 'pup');
$stmt = $dbh->prepare("SELECT * from t_country");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
?>
I repeat. This is just for quick reference. Do not use it as a pattern for your code.
So using that code, we have now selected data from our table country and usedjson_encode to make it in json format. read more about this here. http://php.net/manual/en/function.json-encode.php.
Next one is the frontend(HTML part)
<body>
// empty dropdown button to be filled by data from database
<select id="country">
</select>
//include the jquery library
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax ({
url : 'dropdown_database.php', // the url from which the data will be pulled
success: function(data) {
var country = $.parseJSON(data); // parse the json format data
$.each(country, function(i, d) {
$('#country').prepend('<option>'+ d.country +'</option>'); // this one adds the <option></option> tag in the dropdown
});
}
});
});
</script>

If I got you right, you want to replace this:
<p>
<label for="CountryID">Country ID:</label>
<input type="text" name="CountryID" id="CountryID">
</p>
With a dropdown so you can quickly pick the country from the dropdown instead of memorizing the ID for each individual country. And to create the select you are using this code:
<p>
<label for="CountryID">Country:</label>
<?php
// some code here that I removed to avoid noise
$sql = "SELECT * FROM country";
$result = mysqli_query($con_qnt, "SELECT * FROM country" );
echo "<select name='label'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['CountryName' ] . "'>" . $row['CountryName' ] . " (" .$row['PhonePrefix' ] . ")" . "</option>";
}
echo "</select>";
?>
<name="CountryID" id="CountryID">
</p>
That should work fine... after you fix some issues:
If you want to replace the input with the select, the select should have the same name as the input so the value is processed automatically. Right now the select has "label" as the name and there is a strange (and incorrect) name tag. To fix this:
Get rid of the unnecessary name tag.
Replace the name attribute in the select with value "CountryID":
echo "<select name='CountryID'>";
The option value should be the country ID so, unless the ID is the country name (not the best choice), you are not using the right value. Change that line too:
echo "<option value='" . $row['CountryID' ] . "'>" . $row['CountryName' ] . " (" .$row['PhonePrefix' ] . ")" . "</option>";
(Considering CountryID as the ID, replace it for the right column name).
And I think with those two changes, the rest of the code should work just fine with insert.php that wouldn't require any updates at all. Finally it would look something like this:
<p>
<label for="CountryID">Country:</label>
<?php
// some code here that I removed to avoid noise
$sql = "SELECT * FROM country";
$result = mysqli_query($con_qnt, "SELECT * FROM country" );
echo "<select name='CountryID'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['CountryID' ] . "'>" . $row['CountryName' ] . " (" .$row['PhonePrefix' ] . ")" . "</option>";
}
echo "</select>";
?>
</p>

Related

Trying to Link a populated drop down list PHP

As the title suggests I am trying to link a populated drop down list to a form on another page.
My dropdown list is currently connected to my database which displays the addressID's of 6 people. So when the user selects for example AddressID 3 it will take them to the next page (customerdetails.php) which will then allow them to update the form which will update the database accordingly.
My current code is as follows
<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";
//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
or die("Could not select my_guitar_shop2");
?>
<p>AddressID:</p> <br>
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);
echo "<select name='addressID' onchange = 'getAddressID(this)'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID'] ."</option>";
}
echo "</select>";
?>
Now on the customerdetails.php page i have the code:
<?php
$adrresIDSelected = $_GET['addressID'];
?>
For the life of me I cannot seem to connect the 2 pages together.
Am i anywhere near the correct path? I would prefer not to use javascript as I have no prior knowledge of it.
Many thanks in advance
UPDATE
customerdetails.php page
<?php
//adding the database connection
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the databse
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("Unable to Connect to MySQL");
echo "Connected to MySQL";
//selecting the database we want to work with
$selected = mysql_select_db("my_guitar_shop2", $dbhandle)
or die("Could not select my_guitar_shop2");
?>
<?php
$addrresIDSelected = $_GET['addressID'];
?>
Contact Form
<form class="form">
<p class="first">
<label for="name">FirstLine</label>
<input type="text" name="firstline" id="first" />
</p>
<p class="second">
<label for="email">SecondLine</label>
<input type="text" name="secondline" id="second" />
</p>
<p class="city">
<label for="web">City</label>
<input type="text" name="city" id="web" />
</p>
<p class="state">
<label for="web">State</label>
<input type="text" name="state" id="web" />
</p>
<p class="zip">
<label for="web">Zip Code</label>
<input type="number" name="zip" id="web" />
</p>
<p class="update">
<input type="button" value="Update" />
</p>
<p class="remove">
<input type="button" value="Remove" />
</p>
</form>
First solution (no Javascript)
For a solution without Javascript, you will need to use the select within a form element and use a submit button too to send the information completed/selected in the form to the desired page:
...
<form action="customerdetails.php" method="get">
<select name="addressID">
<?php
$sql = "SELECT addressID FROM addresses";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['addressID'] ."'>" . $row['addressID'] ."</option>";
}
?>
</select>
<input type="submit" value="Take me to the other page">
</form>
...
UPDATE - Second solution (with Javascript)
For using the getAddressID Javascript function to send the ID instead of using a form, you will need to update the function a bit:
<script>
function getAddressID (option) {
var addressID = option.value;
// you do not need the <your_domain> prefix here, as probably both your php scripts are on the same server/domain and same folder
window.location.replace ("customerdetails.php?addressID =" + addressID);
//-----------------------------------------------------^
// Extra space must be removed!
}
</script>

PDO form add to database: Array number is added rather than the text it represents

I'm new to PHP and PDO. I've managed to get the PDO to add the form data to a mysql database when the submit button is clicked.
The problem I am having is drop down box which selects and displays the data from another database table. When this is added to the databases rather than displaying the selected option 'Top, Hat or Coat' it instead displays '0, 1, 2'.
HTML Code (with some PHP):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<?PHP include_once("addProduct.php");?>
<form method="post" action="">
Product Name: <input type="text" id="productName" name="productName" /><br />
Catagory:
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("web_scripting") or die(mysql_error());
$query = "SELECT id,category FROM catagory_table";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select type="text" id="category" name="category">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['id']."'>'".$row['category']."'</option>";
}
?> </select><br />
Stock: <input type="number" id="stock" name="stock" /><br />
Cost: <input type="number" id="cost" name="cost" /><br />
<input type="submit" value="add"> <br />
<?PHP
$query = "SELECT * FROM product_table";
$result = $odb->query($query);
if($result->rowCount() > 0) {
foreach($result as $item) {
echo($item['name'] . " , " . $item['category'] . " , " . $item['stock'] . " , " . $item['cost'] . "<br />");
}
}
?>
</form>
</div>
</body>
PHP Code:
$host = "localhost";
$user = "root";
$db = "web_scripting";
$pass = "";
$odb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
if(isset($_POST['productName'])) {
$productName = $_POST['productName'];
$category = $_POST['category'];
$stock = $_POST['stock'];
$cost = $_POST['cost'];
$q = "INSERT INTO product_table(name, category, stock, cost) VALUES(:name, :category, :stock, :cost);";
$query = $odb->prepare($q);
$results = $query->execute(array(
":name" => $productName,
":category" => $category,
":stock" => $stock,
":cost" => $cost
));
}
?>
I would say this is correct, your database is saving the id of the category which is what you want. The name of the category can be found by querying the category table for that id. This is relational database design. Consider if you did store the name of the category on the product table. Later down the line you then decided to change the name of the category, you would need to update all products records rather than just one category record.

Using AJAX How can I generate selections for a dropdown menu based on records available in a database?

Using AJAX How can I generate selections for a dropdown menu based on records available in a database?.
How can then use these selections to prefill a form with record/row data from a database when selected?
Heres a mock up I created of what I'm trying to do:
http://oi58.tinypic.com/2urb2ae.jpg
PHP FILE: contact_form.php
-----------------------------------------------------------
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($connection, DB_NAME);
if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}
echo 'Connected successfully';
if (isset($_POST['itemname'])){
$itm = $_POST['itemname'];
}
else {
$itm = '';
}
if($_POST['mile']){
$mi = $_POST['mile'];
}else{
echo "Miles not received";
exit;
}
if($_POST['email']){
$email = $_POST['email'];
}else{
echo "email not received";
exit;
}
$sql = "INSERT INTO seguin_orders (itemname, mile, email)
VALUES ('$itm', '$mi', '$email')";
if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
CONACT FORM: formz.php
------------------------------------------------------------------------------
<html>
<header>
</header>
<body>
<form action="/demoform/contact_form.php" class="well" id="contactForm" method="post" name="sendMsg" novalidate="">
<big>LOAD PAST ORDERS:</big>
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
<?php
$email = $_POST['email'];
$query="select * from tablename WHERE email={$_POST['email']}";
$res=mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($res))
{
?>
<option value="<?php echo $row['fieldname']; ?>"><?php echo $row['fieldname']; ?></option>
<?php
}
?>
</select>
</br>
<input type="text" required id="mile" name="mile" placeholder="Miles"/>
</br>
<input id="email" name="email" placeholder="Email" required="" type="text" value="demo#gmail.com" readonly="readonly"/>
</br>
<input id="name" name="itemname" placeholder="ITEM NAME 1" required="" type="text" />
</br>
<input type="reset" value="Reset" />
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>
Using an exemple, let's assume you want to fill the "name" select based on the option selected at the "gender" select:
<select name="gender" id="gender">
<option value="m">Male</select>
<option value="f">Female</select>
</select>
When nothing is selected yet, the "name" select is empty:
<select name="name" id="name">
<option value="NULL">Please select a gender first</option>
</select>
So, what you gotta do is: when the gender select got some selection, you populate the name select with values based on the gender select option.
$(document).ready(function() {
$('select#gender').change(function(){
$('select#name').load('LOAD_NAMES_BASED_ON_GENDER.php?gender='+$(this).val());
});
});
And your PHP file responsible for loading the names based on gender should look like:
$gender = $_GET['gender'];
$list = // the way you retrieve your list of names from your DB
And then you loop this $list into an list of options, such like:
foreach($list as $key=>$value)
echo '<option value="$key">$value</option>';
This simple.
PS: the load() function is kind of an alias for the $.ajax request, given that the only purpose here is to retrieve data.

how to pre populate fields based on search results of data base

I am new to stack overflow and really appreciate all the help. I currently have a database with basic columns, name id company etc. I created a search query that shifts through this list based on firstname, lastname, or timestamp date. I was able to print the results on the query page of the search but want this to pre populate on the same form page as a new entry. i was able to link from the query page to the form page but am not sure how to populate these results on the form page.
my current query page prints as the following :
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<div id="container" style="width:750px;background-color:#FFFE8D;margin-left: 250px">
<div id="Back" style="float:left; background-color:#FFFE8D;">
<form action="http://localhost/contractor/existingcontractorpage3.php">
<input type="submit" value="Back">
</form>
</div>
<div id="Is this the Contractor?" style="float:right; background-color:#FFFE8D;">
<form action="http://localhost/contractor/redirectcontractorpage.php">
<input type="submit" value="Next">
</form>
</div>
<div id="info" style="width:750px;height:95px; text-align: center">
<h3> If this is the contractor, please move on to the next page using the corresponding button above. </h3>
<h3> Please enter the exact information on the next page. </h3>
</div>
<div id="results" style="width:750px;height:225px; text-align: center">
<?php
$host = "localhost"; //server
$db = ""; //database name
$user = ""; //databases user name
$pwd = ""; //password
mysql_connect($host, $user, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$searchTerm = trim($_GET['searchname']);
// Check if $searchTerm is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
else
{
$sql = "SELECT * FROM contractor WHERE CONCAT(FIRSTNAME,' ',LASTNAME,' ', ARRIVAL) like '%$searchTerm%'";
$query = mysql_query($sql);
$count=mysql_num_rows($query);
if(($count)>=1)
{
$output = "";
while($row = mysql_fetch_array($query))
{
$output .= "First Name: " . $row['FIRSTNAME'] . "<br />";
$output .= "Last Name: " . $row['LASTNAME'] . "<br />";
$output .= "Arrival: " . $row['ARRIVAL'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
}
?>
</div>
<br> </br>
<br> </br>
</body>
</html>
Now ideally i would want the results to pop up here, and if possibly have a radio button to the right of each result( if there is ever more than one) to select and continue to the form page to pre populate each field. the form page is simply like this:
<form action="insert_submit.php" method="post">
First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" />
<br>
</br>
Last Name: <input type = "text" name="LASTNAME" id="LASTNAME"/>
<br>
</br>
Purpose: <input type = "text" name="PURPOSE" id="PURPOSE"/>
<br>
</br>
Company: <input type = "text" name="COMPANY" id="COMPANY" />
<br>
</br>
Who here to see: <input type = "text" name="WHOHERETOSEE" id="WHOHERETOSEE"/>
<br>
</br>
<input type="submit" value="Submit">
<br>
</br>
</form>
thanks so much! hope to hear back soon as this is my last straw on my project.
The simplest way to do this is to pull the data from the database like you're doing, except where you want to add the form into the page. Then in your loop print out form fields:
You need to give each radio a unique name. You could use an incrementing id, or you could give it the same as the row name.
Method 1:
while($row = mysql_fetch_array($query))
{
$output .= "<label>First Name: " . $row['FIRSTNAME'] . "</label><input type=\"radio\" name = \"radio[]\" value=\"".$row['FIRSTNAME']." ".$row['LASTNAME']."\"/>";
..
..
}
You will then be able to get the values from the radio buttons by using GET or POST when the from is submitted.
Setting name = "radio[]" puts all the values in an array which you can get on the next page once the form is submitted.
On the next page after form submission, $_POST['radio'] or $_GET['radio'] will return an array of all the values which you specify. Notice how the value attribute has been filled in above. Do this for each of the rows.
Also be careful about using my_sql connections. It's depreciated as of PHP 5.5.0. Use mysqli or PDO instead, it's more secure. http://ie1.php.net/function.mysql-connect
Check it this link. it compares and provides examples on both
I don't see where you're connecting the query search and form page, but you could create the link back containing the data in GET params in the url, since I don't see password or private material in these fields. Then use php to populate the forms.
$url = "your_form_page?";
while($row = mysql_fetch_array($query))
{
$url .= "fname=" . $row['FIRSTNAME'];
$url .= "&lname=" . $row['LASTNAME'];
$url .= "&arrival=" . $row['ARRIVAL'];
}
// $url = your_form_page?fname=bob&lname=jones&arrival=blah
Then to populate the form..
<form action="insert_submit.php" method="post">
First Name: <input type = "text" name="FIRSTNAME" id="FIRSTNAME" value="<?php echo $_GET['fname']; ?>" />
Hopefully this helps or at least gets you thinking in the right direction.

Troubleshooting HTML and PHP / MySQL

Long time reader, first time poster. I am a novice PHP enthusiast, and I have a page that I have been working. Right now I have the DB connection working well and my SELECT statement is giving me the info needed. My problems are two fold (maybe more after this post; set your phasers to cringe):
At one point, I had the INSERT working, but it suddenly stopped and no amount of tweaking seems to bring it back. I have verified that the INSERT statement works in a seperate PHP file without variables.
When I did have the INSERT working, every refresh of the page would duplicate the last entry. I have tried tried several ways to clear out the $_POST array, but I think some of my experimenting lead back to problem #1.
<?php
$dbhost = "REDACTED";
$dbuser = "REDACTED";
$dbpass = "REDACTED";
$dbname = "guest_list";
// Create a database connection
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("DB's not here, man: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
// replacement for mysql_real_escape_string()
function html_escape($html_escape) {
$html_escape = htmlspecialchars($html_escape, ENT_QUOTES | ENT_HTML5, 'UTF-8');
return $html_escape;
}
// Posting new data into the DB
if (isset($_POST['submit'])) {
$first = html_escape($_POST['first']);
$last = html_escape($_POST['last']);
$contact = html_escape($_POST['contact']);
$associate = html_escape($_POST['associate']);
$insert = "INSERT INTO g_list (";
$insert .= "g_fname, g_lname, g_phone, g_association) ";
$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";
$i_result = mysqli_query($connection, $insert);
// I have verified that the above works by setting the varialble
// in the VALUES area to strings and seeing it update
}
$query = "SELECT * ";
$query .= "FROM g_list ";
$query .= "ORDER BY g_id DESC";
$q_result = mysqli_query($connection, $query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Guest List</title>
<link href="guest.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1>REDACTED</h1>
<h2>Guest Registry</h2>
</header>
<div class="container">
<div class="registry">
<form name="formup" id="main_form" method="post">
<fieldset>
<legend>Please enter your name into the registry</legend>
<p class="first">First Name:
<input type="text" name="first" value="" placeholder="One or more first names" size="64"></p>
<p class="last">Last Name:
<input type="text" name="last" value="" placeholder="Last name" size="64"></p>
<p class="contact">Phone Number or Email:
<input type="text" name="contact" value="" placeholder="" size="32"></p>
<p class="associate">Your relation?
<input type="text" name="associate" value="" placeholder="" size="128"></p>
<p class="submit">
<input type="submit" name="submit" title="add" value="submit" placeholder=""></p>
</fieldset>
</form>
</div>
</div>
<h3>Guest List:</h3>
<table>
<tr>
<th>Firstname(s)</th><th>Lastname</th>
<th>Phone or Email</th><th>Association</th>
</tr>
<?php while($guest = mysqli_fetch_assoc($q_result)) {
echo "<tr>" . "<td>" . $guest["g_fname"] . "</td>"
. "<td>" . $guest["g_lname"] . "</td>"
. "<td>" . $guest["g_phone"] . "</td>"
. "<td>" . $guest["g_association"] . "</td>" . "</tr>";
} ?>
</table>
<footer>
<div>Copyright <?php echo date("Y"); ?>, REDACTED, LLC.</div>
<?php
if (isset($connection)) {
mysqli_close($connection);
}
?>
</footer>
</body>
</html>
These two lines will fail:
$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";
Two problems here, all with the second line:
No SPACE between ) and LIMIT: )LIMIT 1 is your code;
LIMIT 1 in an INSERT is not allowed....

Categories