I have the following basic HTML form:
<form action="" method="post">
Name: <input type="text" name="name" /><br><br>
College: <select name = "colleges">
<option> ---Select College---</option>
<option value="option1">DIT</option>
<option value="option2">University College Dublin</option>
</select><br><br>
Email: <input type="text" name="email" /><br><br>
Password: <input type="text" name="password" /><br><br>
Location: <input type="text" name="location" /><br><br>
<button type="submit" name="submit" >Submit</button>
</form>
And the following section of PHP which I am quite new to:
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$college_name = $_POST["college"];
$email = $_POST["email"];
$password = $_POST["password"];
$location = $_POST["location"];
}
$sql = "INSERT INTO user(name, college, email, password, location) VALUES ($name, $college_name, $email, $password, $location)";
?>
My database has college as an int, so DIT would be 1 in the database. Can anyone tell me how to do this so that it sends 1 as college_name instead of the actual name that the user sees?
Change this
College: <select name = "colleges">
<option> ---Select College---</option>
<option value="option1">DIT</option>
<option value="option2">University College Dublin</option>
</select><br><br>
to this
College: <select name = "colleges">
<option> ---Select College---</option>
<option value="1">DIT</option>
<option value="2">University College Dublin</option>
</select><br><br>
and this
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$college_name = $_POST["college"];
$email = $_POST["email"];
$password = $_POST["password"];
$location = $_POST["location"];
}
to this
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$college_name = (int) $_POST["colleges"];
$email = $_POST["email"];
$password = $_POST["password"];
$location = $_POST["location"];
}
In your options put numerical values:
College: <select name="colleges">
<option value="1">DIT</option>
<option value="2">University College Dublin</option>
</select>
then correct your php to match the name of the select in the $_POST array:
$college_name = (int) $_POST["colleges"];
Just use
<option value='1' > DIT </option>
Instead of adding option1 as a value
Why arent you using the option value?
$_POST["college"] is returning option1
I think the correct way is defining the value as 1:
The following:
<option value="1">DIT</option>
Will return: 1
You can also use a If else statement
if ($_POST["college"] == 'option1')
{
$college = 1;
}
if ($_POST["college"] == 'option2')
{
$college = 2;
}
Related
I am having trouble showing the output that i called in printf in php tutorial. I just followed the tutorial but still can't figure out what is wrong in displaying the variables inside the printf in localhost. Is anyone can help me. Thank you.
<?php
$name = '';
$password = '';
$gender = '';
$color = '';
$languages = [];
$comments = '';
$tc = '';
if (isset($_POST['submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
};
if (isset($_POST['password'])) {
$password = $_POST['password'];
};
if (isset($_POST['gender'])) {
$gender = $_POST['gender'];
};
if (isset($_POST['color'])) {
$color = $_POST['color'];
};
if (isset($_POST['languages'])) {
$languages = $_POST['languages'];
};
if (isset($_POST['comments'])) {
$comments = $_POST['comments'];
};
if (isset($_POST['tc'])) {
$tc = $_POST['tc'];
};
//here's the problem i cant resolve printing out the output//
printf('User name: %s
<br>Password: %s
<br>Gender: %s
<br>Color: %s
<br>Language(s): %s
<br>Comments: %s
<br>T&C: %s',
htmlspecialchars($name, ENT_QUOTES),
htmlspecialchars($password, ENT_QUOTES),
htmlspecialchars($gender, ENT_QUOTES),
htmlspecialchars($color, ENT_QUOTES),
htmlspecialchars(implode('', $languages), ENT_QUOTES),
htmlspecialchars($comments, ENT_QUOTES),
htmlspecialchars($tc, ENT_QUOTES));
}
?>
<form action=""
method="post">
User name: <input type="text" name="name"><br>
Password: <input type="password" value="password"><br>
Gender:
<input type="radio" name="gender" value="f"> female
<input type="radio" name="gender" value="m"> male
<input type="radio" name="gender" value="o"> other<br/>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00">red</option>
<option value="#0f0">green</option>
<option value="#00f">blue</option>
</select><br>
Languages spoken:
<select name="languages[]"multiple size="3">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select><br>
Comments: <textarea name="comments"></textarea><br>
<input type="checkbox" name="tc" value="ok"> I accept the T&C<br>
<input type="submit" value="Register">
</form>`
The problem here is if (isset($_POST['submit'])) there is no any form field with the name submit and it will never become true to execute. Remove the if Condition with submit or Else give the sumbit button name as Submit
<input type="submit" value="Register" name="submit">
Errors in your previous code are:
No semicolons ; are needed after ending if statements.
No name attribute is specified for password input in the form.
Posting form data as if (isset($_POST['submit'])) {...} without specifying the name="submit" attribute in the form.
Invalid use of implode function which will generate output be like enfrit or vice versa.
Additional Tips
Add the required attribute to each input to prevent from sending empty form data.
You have already specified method="post" in the form tag so, no need for validating each input field as if (isset($_POST['fieldname'])) {...} . if (isset($_POST['submit'])) {...} will send all the form data as POST method.
And lastly, here is your updated code.
<?php
if (isset($_POST['submit'])) {
$name = '';
$password = '';
$gender = '';
$color = '';
$languages = [];
$comments = '';
$tc = '';
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset($_POST['gender'])) {
$gender = $_POST['gender'];
}
if (isset($_POST['color'])) {
$color = $_POST['color'];
}
if (isset($_POST['languages'])) {
$languages = $_POST['languages'];
}
if (isset($_POST['comments'])) {
$comments = $_POST['comments'];
}
if (isset($_POST['tc'])) {
$tc = $_POST['tc'];
}
//print output
printf('User name: %s
<br>Password: %s
<br>Gender: %s
<br>Color: %s
<br>Language(s): %s
<br>Comments: %s
<br>T & C: %s
<br><br>',
htmlspecialchars($name, ENT_QUOTES),
htmlspecialchars($password, ENT_QUOTES),
htmlspecialchars($gender, ENT_QUOTES),
htmlspecialchars($color, ENT_QUOTES),
htmlspecialchars(implode(', ', $languages), ENT_QUOTES),
htmlspecialchars($comments, ENT_QUOTES),
htmlspecialchars($tc, ENT_QUOTES));
}
?>
<form action="" method="post">
User name: <input type="text" name="name">
<br>
Password: <input type="password" name="password">
<br>
Gender:
<input type="radio" name="gender" value="f"> female
<input type="radio" name="gender" value="m"> male
<input type="radio" name="gender" value="o"> other
<br>
Favorite color:
<select name="color">
<option value="">Please select</option>
<option value="#f00">red</option>
<option value="#0f0">green</option>
<option value="#00f">blue</option>
</select>
<br>
Languages spoken:
<select name="languages[]" multiple size="3">
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
</select>
<br>
Comments: <textarea name="comments"></textarea>
<br>
<input type="checkbox" name="tc" value="ok"> I accept the T & C
<br>
<input type="submit" name="submit" value="Register">
</form>
I am currently trying to insert rows into a MySQL database, and most of the code is there but I'm having a few issues I can't diagnose. I know the database connection is good, and every time the submit button is pressed it runs the correct php script. The issue I'm having is that it always adds 2 records to the database table and fails to carry though any of the form data (it inserts two completely blank rows.)
Here's the code for the form (with a little extra code for the wordpress page)
<div class="main-container">
<div class="content-area">
<div class="middle-align">
<div class="site-main" id="sitefull">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<div>
<form method="POST" name="cutting tool" action="add-tool-script.php">
<table style="width:auto;">
<tr>
<th width="50%"><h2><ul>Tool Information</ul></h2><br></th>
<th width="50%"><ul><h2>Storage Information</h2></ul><br></th>
</tr>
<tr>
<td width="50%">
<h3>Tooling Name</h3><br>
<input type="text" name="name" placeholder="e.g. ShearHog"><br><br>
<h3>Tooling Type</h3><br>
<select name="type">
<option selected disabled hidden style='display: none' value=''></option>
<option value="Ballnose Endmill">Ballnose Endmill</option>
<option value="Bullnose Endmill">Bullnose Endmill</option>
<option value="Boring Bar">Boring Bar</option>
<option value="Brush">Brush</option>
<option value="Burnishing">Burnishing</option>
<option value="Chamfer Mill">Chamfer Mill</option>
<option value="Countersink">Countersink</option>
<option value="Drill">Drill</option>
<option value="Drill/Mill">Drill/Mill</option>
<option value="Engraver">Engraver</option>
<option value="Face Mill">Face Mill</option>
<option value="Flat Endmill">Flat Endmill</option>
<option value="High Feed Mill">High Feed Mill</option>
<option value="Reamer">Reamer</option>
<option value="Slitting Saw">Slitting Saw</option>
<option value="Spot Drill">Spot Drill</option>
<option value="Tap">Tap</option>
<option value="Threadmill">Threadmill</option>
<option value="Woodruff">Woodruff</option>
<option value="Other">Other</option>
</select><br><br>
<h3>Tooling Brand</h3><br>
<input type="text" name="brand" placeholder="e.g. Lakeshore Carbide"><br><br>
<h3>Part Number</h3><br>
<input type="text" name="part_number" placeholder="e.g. 360014X"><br><br>
<h3>Price</h3><br>
<input type="text" name="price" placeholder="e.g. 24.95"><br><br>
<h3>Overall Length</h3><br>
<input type="text" name="oal" placeholder="e.g. 2.5"><br><br>
<h3>Tooling Material</h3><br>
<select name="material">
<option selected disabled hidden style='display: none' value=''></option>
<option value="Carbide">Carbide</option>
<option value="Ceramic">Ceramic</option>
<option value="Diamond">Diamond</option>
<option value="HSS">HSS</option>
<option value="Powdered Metal">Powdered Metal</option>
</select><br><br>
<h3>Cutting Diameter</h3><br>
<input type="text" name="cutting_diam" placeholder="e.g. 0.250"><br><br>
<h3>Shank Diameter</h3><br>
<input type="text" name="shank_diam" placeholder="e.g. .250"><br><br>
<h3>Number of Flutes</h3><br>
<input type="text" name="flutes" placeholder="e.g. 3"><br><br>
<h3>Length of Cut (Flute Length)</h3><br>
<input type="text" name="loc" placeholder="e.g. .750"><br><br>
<h3>Corner Radius</h3><br>
<input type="text" name="corner_rad" placeholder="e.g. .004"><br><br>
</td>
<td width="50%">
<h3>Quantity in Stock</h3><br>
<input type="text" name="qty" placeholder="e.g. 37"><br><br>
<h3>Minimum Trigger Quantity</h3><br>
<input type="text" name="trigger_qty" placeholder="e.g. 4"><br><br>
<h3>Reorder Link</h3><br>
<input type="text" name="reorder_link" placeholder="e.g. example.com"><br><br>
<h3>Toolbox Number</h3><br>
<input type="text" name="toolbox_no" placeholder="e.g. 1"><br><br>
<h3>Drawer Number</h3><br>
<input type="text" name="drawer_no" placeholder="e.g. 1"><br><br>
<h3>Bin Number</h3><br>
<input type="text" name="bin_no" placeholder="e.g. 1"><br><br>
<h3>Product</h3><br>
<input type="text" name="product" placeholder="e.g. Widget #2"><br><br>
<input type="submit" value="Add to Tool Crib" name="submitbutton" action="submit"/>
</td>
</tr>
</table>
</form>
</div>
<?php
//If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
<?php endwhile; // end of the loop. ?>
</div>
<div class="clear"></div>
</div>
</div>
</div>
<?php get_footer(); ?>
And here's the code for the php script to add form data to the database:
//MySQL Database
$servername = "url.com";
$username = "user_login";
$password = "user_password";
$datab = "database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $datab);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_REQUEST['name'];
$type = $_REQUEST['type'];
$brand = $_REQUEST['brand'];
$part_number = $_REQUEST['part_number'];
$price = $_REQUEST['price'];
$oal = $_REQUEST['oal'];
$material = $_REQUEST['material'];
$cutting_diam = $_REQUEST['cutting_diam'];
$shank_diam = $_REQUEST['shank_diam'];
$flutes = $_REQUEST['flutes'];
$loc = $_REQUEST['loc'];
$corner_rad = $_REQUEST['corner_rad'];
$qty = $_REQUEST['qty'];
$trigger_qty = $_REQUEST['trigger_qty'];
$reorder_link = $_REQUEST['reorder_link'];
$toolbox_no = $_REQUEST['toolbox_no'];
$drawer_no = $_REQUEST['drawer_no'];
$bin_no = $_REQUEST['bin_no'];
$product = $_REQUEST['product'];
$username = $user_login;
$sql = "INSERT INTO `cutting tools` (`name`, `type`, `brand`, `part_number`, `reorder_link`, `oal`, `price`, `material`, `cutting_diam`, `shank_diam`, `flutes`, `loc`, `corner_rad`, `qty`, `trigger_qty`, `user`, `drawer_no`, `bin_no`, `toolbox_no`)
VALUES ('$name', '$type', '$brand', '$part_number', '$reorder_link', '$oal', '$price', '$material', '$cutting_diam', '$shank_diam', '$flutes', '$loc', '$corner_rad', '$qty', '$trigger_qty', '$username', '$drawer_no', '$bin_no', '$toolbox_no')";
if(mysqli_query($conn, $sql)){
echo "Record added successfully.";
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
Also I know my database is vulnerable to injection, that was a change I planned on making once the form was up and running.
Use WordPress to your advantage. Instead of defining your own connection, use global $wpdb, and then use the insert command.
global $wpdb;
$success = $wpdb->insert('tbl_name', array(<br>
'field1_name' => $_REQUEST['field1'],<br>
'field2_name' => $_REQUEST['field2'],<br>
));<br>
if($success){<br>
echo "Inserted correctly";<br>
} else {<br>
echo "Something went awry!";<br>
}
Here's a prepared statement, a more secure way of creating a MYSQL record in your table.
<?php
//MySQL Database
$servername = "url.com";
$username = "user_login";
$password = "user_password";
$datab = "database_name";
// Create connection
$con = new mysqli($servername, $username, $password, $datab);
global $con;
// Post form data
$name = $_POST['name'];
$type = $_POST['type'];
$brand = $_POST['brand'];
$part_number = $_POST['part_number'];
$price = $_POST['price'];
$oal = $_POST['oal'];
$material = $_POST['material'];
$cutting_diam = $_POST['cutting_diam'];
$shank_diam = $_POST['shank_diam'];
$flutes = $_POST['flutes'];
$loc = $_POST['loc'];
$corner_rad = $_POST['corner_rad'];
$qty = $_POST['qty'];
$trigger_qty = $_POST['trigger_qty'];
$reorder_link = $_POST['reorder_link'];
$toolbox_no = $_POST['toolbox_no'];
$drawer_no = $_POST['drawer_no'];
$bin_no = $_POST['bin_no'];
$product = $_POST['product'];
// Prepared statement
$insert = mysqli_prepare($con, "insert into cutting tools (name,type,brand,part_number,reorder_link,oal,price,material,cutting_diam,shank_diam,flutes,loc,corner_rad,qty,trigger_qty,user,drawer_no,bin_no,toolbox_no) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
mysqli_stmt_bind_param($insert, "sssssssssssssssssss", $name,$type,$brand,$part_number,$reorder_link,$oal,$price,$material,$cutting_diam,$shank_diam,$flutes,$loc,$corner_rad,$qty,$trigger_qty,$product,$drawer_no,$bin_no,$toolbox_no);
mysqli_stmt_execute($insert);
if ($insert) { echo "success"; mysqli_close($con); } else { echo "error"; mysqli_close($con); }
?>
Pay close attention to the order of your columns and the data you are submitting to those columns. I have edited this post because your order was incorrect in several places.
The column names and the data variables being uploaded to them have to be in the exact same order if you want data created correctly.
By the way the variable $product does not seem to match the column name of user, you may want to check this.
I am working on my project I want to store my multiple selected option value in the database but when I select more than one option at one time that time my only last selected values stored in DB please help my where am I am wrong?
Here is my Code:
<?php
require_once('contact_fun.php');
require_once('contact_banner_fun.php');
$obj = new contact_banner();
$crud = new contact_us();
$banner = $obj->get_data_banner();
if(!$banner){
echo "No Banner";
exit;}
$path = 'http://localhost/THE_VELVET_WALK/contact/';
$result = $crud->getData("SELECT * FROM tb_contactus");
foreach ($result as $res) {
$id = $res['id'];
$name = $res['name'];
$business = $res['business'];
$email = $res['email'];
$phone = $res['phone'];
$message = $res['message'];
$style = $res['style'];
}
if(isset($_POST['submit']))
{
$name = $crud->escape_string($_POST['name']);
$business = $crud->escape_string($_POST['business']);
$email = $crud->escape_string($_POST['email']);
$phone = $crud->escape_string($_POST['phone']);
$message = $crud->escape_string($_POST['message']);
$style = $crud->escape_string($_POST['style']);
$result = $crud->execute(" INSERT INTO tb_contactus(name, business, email,
phone, message, style, update_dt)VALUES ('$name','$business', '$email',
'$phone', '$message', '$style', now())");
}?>
<form action="" method="POST">
<input id="00N7F000001F2j6" name="name" maxlength="40" type="text"
class="materialize-input">
<input id="company" name="business" maxlength="40" type="text"
class="materialize-input">
<input id="email" name="email" maxlength="40" type="email"
class="materialize-input">
<input id="phone" name="phone" maxlength="40" type="text" maxlength="40"
class="materialize-input">
<select multiple id="00N7F000001F2kO" name="style" multiple="multiple" >
<option name="consulting" name="styling" value=""selected disabled>image
consulting</option>
<option value="1">styling</option>
<option value="2">hair & make-up</option>
<option value="3">designing clothes</option>
<option value="4">wedding makeover</option>
<option value="5">personal shopper</option>
<option value="6">corporate services</option>
</select>
</form>
you have to specify in the name of the select that it will be an array like :
<select multiple id="00N7F000001F2kO" name="style[]" multiple="multiple" >
<option value=""selected disabled>image consulting</option>
<option value="1">styling</option>
<option value="2">hair & make-up</option>
<option value="3">designing clothes</option>
<option value="4">wedding makeover</option>
<option value="5">personal shopper</option>
<option value="6">corporate services</option>
</select>
and then you can implode it if you want a string like this
implode(',',$_POST['style']);
EDIT :
change this
$style = $crud->escape_string($_POST['style']);
to
$style = $crud->escape_string(implode(',',$_POST['style']));
you need to use an array for multiple select options :
<select multiple id="00N7F000001F2kO" name="style[]" multiple="multiple" >
<option name="consulting" name="styling" value=""selected disabled>image
consulting</option>
<option value="1">styling</option>
<option value="2">hair & make-up</option>
<option value="3">designing clothes</option>
<option value="4">wedding makeover</option>
<option value="5">personal shopper</option>
<option value="6">corporate services</option>
</select>
this is how tou get the data into your database :
.php
if(isset($_POST['styling']) && !empty($_POST['styling'])){
$Col1_Array = $_POST['styling'];
print_r($Col1_Array);
foreach($Col1_Array as $selectValue){
//show selected
echo $selectValue."<br>";
}
}
How do I create a link to each photo or text of these search results to a profile.php to query and output database?
In a nutshell - search database and click any result that would take you to their profile page. Example: Match.com, facebook.com...etc (search and click to view profile).
Also, how can I output first name and last name on the same line?
Please Help.
Here is the HTML Search Form.
<h2>Search</h2>
<form name="search" action="searchresults.php" method="POST">
Search for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="firstName">First Name</option>
<Option VALUE="email">Email</option>
</Select>
Search for: <input type="text" name="find1" /> in
<Select NAME="field1">
<Option VALUE="lastName">Last Name</option>
</Select>
<br><br>
Search for: <input type="text" name="find2" /> in
<Select NAME="field2">
<Option VALUE="gender">Gender</option>
</Select>
<br><br>
Search for: <input type="text" name="find3" /> in
<Select NAME="field3">
<Option VALUE="age">Age</option>
</Select>
<br><br>
Search for: <input type="text" name="find4" /> in
<Select NAME="field4">
<Option VALUE="city">City</option>
</Select>
Search for: <input type="text" name="find5" /> in
<Select NAME="field5">
<Option VALUE="state">State</option>
</Select>
<br><br>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
Here is the searchresults.php.
DATABASE CONNECTION
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$find=$_POST['find'];
$field=$_POST['field'];
$find1=$_POST['find1'];
$field1=$_POST['field1'];
$find2=$_POST['find2'];
$field2=$_POST['field2'];
$find3=$_POST['find3'];
$field3=$_POST['field3'];
$find4=$_POST['find4'];
$field4=$_POST['field4'];
$find5=$_POST['find5'];
$field5=$_POST['field5'];
$data="SELECT firstName, lastName, email, userphoto, gender, age, city, state FROM actorsInfo
WHERE upper($field) LIKE '%$find%'
AND upper($field1) LIKE '%$find1%'
AND upper($field2) LIKE '%$find2%'
AND upper($field3) LIKE '%$find3%'
AND upper($field4) LIKE '%$find4%'
AND upper($field5) LIKE '%$find5%'
";
$result = mysql_query($data);
$count=mysql_numrows($result);
echo '<br><br>';
if($count > 0){
echo"<table border=0>";
//get images and names in two arrays
$firstName= $row["firstName"];
$lastName= $row["lastName"];
$email= $row["email"];
$userphoto= $row["userphoto"];
$gender= $row["gender"];
$age= $row["age"];
$city= $row["city"];
$state= $row["state"];
$age = array();
$gender = array();
$userphoto = array();
$firstName = array();
$lastName = array();
$city = array();
$state = array();
while ($row = mysql_fetch_array($result))
{
$userphoto[] = "<img src='images/".$row['userphoto']."' height='200' width='160'>";
$firstName[] = $row['firstName'];
$lastName[] = $row['lastName'];
$age[] = $row['age'];
$gender[] = $row['gender'];
$email[] = $row['email'];
$city[] = $row['city'];
$state[] = $row['state'];
}
while(!empty($userphoto))
{
//output images
foreach(array($userphoto, $firstName, $lastName, $age, $email, $city, $state) as $items)
{
echo "<tr>";
foreach($items as $key=>$item)
{
echo "<td><font size =\"2\" >$item</td>";
//output only four of them
if($key==4)
{
break;
}
}
echo "</tr>";
}
//remove the first five images from $images because they're already printed
$userphoto = array_slice($userphoto, 5);
$firstName = array_slice($firstName, 5);
$lastName= array_slice($lastName, 5);
$email = array_slice($email, 5);
$age = array_slice($age, 5);
$city = array_slice($city, 5);
$state = array_slice($state, 5);
}
Here is the solution that passes the id to profile.php.
$userphoto[] = "<a href='profile.php?id=".$row['id']."'><img src='images/".$row['userphoto']."' height='200' width='160'></a>";
Here is the profile.php.
<?php
$id = (int)$_GET['id'];
if (isset($_GET['id']))
{
//fetch and display the information with database Query
$con=mysqli_connect("127.0.0.1", "admin", "password","actors");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM actorsInfo where id = " . $_GET['id']);
while($row = mysqli_fetch_array($result))
{
echo $row['firstName'] . " " . $row['email'];
echo "<br>";
}
mysqli_close($con);
}
?>
I have a database with the name Harry Butler in it under the columns first_name and last_name with this code :
<?PHP
//Create the connection…
//("where the database is", 'Database login' , 'database password' , "Database name")
$con=mysqli_connect("", 'root', 'root', "Social");
//Check our connection…
if (mysqli_connect_errno($con))
{
echo " Sorry Mate";
}
$fname = $_POST[fname];
$lname = $_POST[lname];
$result = mysqli_query($con, "SELECT * FROM `User_info` WHERE first_name = '$fname' AND last_name = '$lname'");
while($row = mysqli_fetch_array($result))
{
echo $row['first_name'];
}
?>
Which gets the form data from the previous page :
<form action="searching.php" method="post">
<input id="fname" align="center" placeholder="First Name" name="fname"></input>
<input id="lname" align="center" placeholder="Last Name" name="lname"></input>
<select id="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
<option value="God">God</option>
</select>
<br />
</form>
Yet when the results are submitted i get nothing any ideas?
change
$fname = $_POST[fname];
$lname = $_POST[lname];
to this:
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$_POST is associative array,so i hope it helped