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
Related
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;
}
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 cannot figure out why the dynamic dropdown won't populate from my database.:
<!doctype html>
<html>
<body>
<h2>Insert Album</h2>
<form action="insertalbum.php" method="POST">
Title: <input type="text" name="atitle" maxlength='50' required><br>
Band: <select name='bands'>
<?php
$conn = mysqli_connect("Server","database","password","username"); //i put in these placeholder for my actual credentials
// Check connection
if(mysqli_connect_errno()){
echo nl2br("Failed to connect to MySQL: " . mysqli_connect_error() . "\n ");
}
$query = "SELECT DISTINCT name FROM band";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
unset($name);
$name = $row['name'];
echo '<option value="name"> $name </option>';
}
?>
</select>
<br>
Published Year: <input type="number" name="pyear" min='1900' max='2020' required><br>
Publisher: <input type="text" name="pname" maxlength='50' required><br>
Format:<select> <option value="record"> Record </option>
<option value="cd"> CD </option>
<option value="casette"> Casette </option>
</select> <br>
Price: <input type="number" name="price" min='0' max='9999.99'><br>
<input type="submit" value="Insert">
</form>
</body>
</html>
this a example:
<select id="employee">
<option value="" selected="selected">Select Employee Name</option>
<?php
/* connection */
$conn = mysqli_connect("Server","database","password","username");
/* query */
$sql = "SELECT id, employee_name, employee_salary, employee_age FROM employee LIMIT 10";
/* get data from db */
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
/* build your dropdown*/
while( $rows = mysqli_fetch_assoc($resultset) ) {
?>
<option value="<?php echo $rows["id"]; ?>"><?php echo $rows["employee_name"]; ?></option>
<?php } ?>
</select>
I get this: Catchable fatal error: Object of class mysqli could not be converted to string error when i want to insert data into my games table.
On my games.php i have a form that sends data to games_add.php and error occurs on line 18 in games_add.php.
(i know a lot of code) game.php code:
<body>
<form action="games_add.php" method="post">
Game name: <input type="text" name="game_name" placeholder="Enter first name ..." required="required" /><br />
Relese date: <input type="date" name="relese_date" placeholder="Enter relese date ..." required="required" /><br />
Introduction: <input type="text" name="introduction" placeholder="Enter introduction ..." required="required" /><br />
Description: <input type="text" name="description" placeholder="Enter description ..." required="required" /><br />
Original price: <input type="number" name="rating" placeholder="Enter original price ..." required="required" /><br />
Developer: <select name="developer_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM developers";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
Publisher: <select name="publisher_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM publishers";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
Categories: <select name="categories_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM categories";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
Platform: <select name="platform1_id">
<?php
include_once 'connection.php';
$query = "SELECT * FROM platforms";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
?>
</select>
<input type="submit" value="Insert" />
<input type="reset" value="Cancle" />
games_add.php code:
<?php
include_once 'connection.php';
$game_name = $_POST['game_name'];
$relese_date = $_POST['relese_date'];
$introduction = $_POST['introduction'];
$description = $_POST['description'];
$rating = $_POST['rating'];
$developer_id = $_POST['developer_id'];
$publisher_id = $_POST['publisher_id'];
$categories_id = $_POST['categories_id'];
$platform1_id = $_POST['platform1_id'];
$avrage_score = 33;
$query = sprintf("INSERT INTO games (game_name, relese_date, introduction, description, rating, developer_id, publisher_id, categories_id, platform1_id, avrage_score )
VALUES ('%s','$relese_date','%s','%s','$rating','%s','%s','%s','%s','$avrage_score') or die(mysqli_error($link)); ",
mysqli_real_escape_string($link, $game_name),
mysqli_real_escape_string($link, $introduction),
mysqli_real_escape_string($link, $description),
mysqli_real_escape_string($link, $developer_id),
mysqli_real_escape_string($link, $publisher_id),
mysqli_real_escape_string($link, $categories_id),
mysqli_real_escape_string($link, $platform1_id));
mysqli_query($link, $query);
header('Location: games.php');
?>
Picture of my database if it helps.
Are you sure you want "or die(mysqli_error($link))" to be part of your SQL and not part of your PHP?
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);
}
?>