I have this script to get the text of a dropdown
<script>
function streetvalue(data) {
var i = street.selectedIndex;
if (i != -1) {
document.getElementById("street2").value = street.options[i].text;
}
}
</script>
and here is my HTML
<tr>
<td width="120" height="30" class="label"><label for="accno"><strong>Street/Alley</strong></label></td>
<td height="30" class="content">
<select name="street" id="street" style="width:200px;" onchange="streetvalue(this)" required>
<option value="" label="Select your street">Select your street</option>
<?php
$sql = "SELECT * FROM tblstreet ORDER BY lname asc";
$result = dbQuery($sql);
while($row = dbFetchAssoc($result)) {
echo '<option value="'.$row['lid'].'">'.$row['lname'].'</option>';
}
?>
</select>
<input type="text" id="street2">
It's working perfectly when selecting items from dropdown, the textbox copies it. But when i hit submit and save it to my DB, the value that is always showing in my DB is "street2" and not the text that i selected in my dropdown. Am i missing something? thank you for your help!
When submit button is pressed here is my function
function doRegister()
{
$fname = $_POST['firstname'];
$mname = $_POST['middlename'];
$lname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$cphone = $_POST['cphone'];
$dob = $_POST['dob'];
$job_type = $_POST['intext4'];
$street2 = $_POST['street2'];
$sql = "INSERT INTO tbl_address (user_id, blknum, apartment, street, street2, subdivision, city, zipcode, res_type, address)
VALUES ($insert_id, '$blknum', '$apart', '$street', 'street2', '$subdi', '$city', '$zip', '$typeres', '$address')";
dbQuery($sql);
The street field is my dropdown menu and the street2 field is the copied text from the dropdown menu. Thank you!
Related
I am new to php and I am trying to make a school portal, and right now I am trying to create the page where admins can make the classes.
All I need to know how to do is allow the admin to select a teacher (names or username come directly from database) from the drop down menu, and enter the corresponding ID into my 'class' table, in teacher_id.
$query = "SELECT Username FROM tbluser WHERE Role='Teacher'";
$result2= mysqli_query($conn, $query);
<body>
<form action="createclass.php" method="POST">
<div>
<label>Class name</label>
<input type="text" name="classname" class="form-control" placeholder="Enter new username">
<br/>
<label>Teacher</label>
<select name ="teacher">
<option selected = "true" disabled="disabled"> Select one from below...</option>
<?php
while ($rows=mysqli_fetch_assoc($result2))
{
$teachername= $rows['Username'];
echo"<option value ='$teachername'>$teachername</option>";
}
?>
</select>
if(isset($_POST["btnAddclass"])){
$classname = $_POST["classname"];
$teacher = $_POST["teacher"];
$subject = $_POST["subject"];
$section = $_POST["section"];
$yeargroup = $_POST["yeargroup"];
$result1 = mysqli_query($conn, "SELECT ID FROM teacher INNER JOIN tbluser ON tbluser.ID=teacher.tbluser_ID WHERE Username = '$teacher'"); // using mysqli_query instead
$sql = "INSERT INTO class (classname, teacher_id, yeargroup_id, subject_id, section_id) VALUES ('$classname', '$result1', '$subject','$section','$yeargroup')";
if(!mysqli_query($conn,$sql))
{
echo "Error";
}
else
{
echo "New class has been added.";
}
}
database
Also select ID from tbluser, this ID is a teacher ID:
$query = "SELECT ID, Username FROM tbluser WHERE Role='Teacher'";
$result2= mysqli_query($conn, $query);
<form action="createclass.php" method="POST">
<div>
<label>Class name</label>
<input type="text" name="classname" class="form-control" placeholder="Enter new username">
<label>Teacher</label>
<select name="teacher">
<option selected="true" disabled="disabled"> Select one from below...</option>
<?php
while ($rows=mysqli_fetch_assoc($result2)) {?>
<option value="<?php echo $rows['ID']?>"><?php echo $rows['Username']?></option>
<?php
}?>
</select>
On submit:
$classname = $_POST["classname"];
$teacherID = $_POST["teacher"];
$subject = $_POST["subject"];
$section = $_POST["section"];
$yeargroup = $_POST["yeargroup"];
$sql = "INSERT INTO class (classname, teacher_id, yeargroup_id, subject_id, section_id) VALUES ('$classname', '$teacherID', '$subject','$section','$yeargroup')";
And as you use unsafe data from user input, consider moving to prepared statements to avoid SQL injections.
I am using a HTML table form to collect data from users. The rows of the table can be increased by clicking ADD MORE button or can be decreased by clicking DELETE button. So the number of rows in the table are not fixed for all users. Sometimes it would be only 4 rows, sometimes it can be 12 rows. I am wondering how can i insert all data(rows) in my mysqli database by clicking SUBMIT button just once. you can check my table in below link :
Demo in JsFiddle
my php code:
<?php
$email = $_SESSION["email"];
$cell = "Select `cellno` FROM `userreg` WHERE email = '$email'";
$result = mysqli_query($conn, $cell);
$cellno = "";
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_assoc($result))
{
$cellno = $row["cellno"];
}
}
if(isset($_POST['submit']))
{
$fullname = $_SESSION["fullname"];
$medname = $_POST['medname'];
$medqty = $_POST['medqty'];
$medtype = $_POST['medtype'];
$num = $_POST['num'];
$date = date("Y.m.d");
$medorder = "INSERT INTO `medorder` (`fullname`, `phoneno`, `medname`, `quantity`, `medtype`, `date`,`orderno` ) VALUES ('$fullname', '$cellno', '$medname','$medqty', '$medtype', CURRENT_TIMESTAMP, '$num' )";
if($conn->query($medorder)=== TRUE)
{
echo"<br>";
echo " Your Question has been submitted successfully";
}
else
{
echo " Error!! Try again or later";
}
}
?>
First Change the names of your inputs:
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD><input type="hidden" value="1" name="num[]" id="orderdata">1</TD>
<TD><INPUT type="text" name="medname[]" class="form-control" id="orderdata" placeholder="Your Medicine name"/></TD>
<TD><INPUT type="text" name="medqty[]" class="form-control" id="orderdata" placeholder="Quantity" /></TD>
<TD>
<SELECT name="medtype[]" class="form-control" id="orderdata">
<OPTION value="in">Bangladesh</OPTION>
<OPTION value="de">Germany</OPTION>
<OPTION value="fr">France</OPTION>
<OPTION value="us">United States</OPTION>
<OPTION value="ch">Switzerland</OPTION>
</SELECT>
</TD>
</TR>
Then built your query by looping throught number of rows in the table.
$medorder='';
foreach ($medname as $key => $value) {
$medorder .= "INSERT INTO `medorder` (`fullname`, `phoneno`, `medname`, `quantity`, `medtype`, `date`,`orderno` ) VALUES ('$fullname', '$cellno[$key]', '$medname[$key]','$medqty[$key]', '$medtype[$key]', CURRENT_TIMESTAMP, '$num' ),";
}
I created a form which can add more input field for product_name and quantity of the product with the help of jquery, this is the demo where you can add more input field in the form.
the problem is when i submit the form only the last product will submit into my database the rest of the product will not submitted.
this is my query
<?php
if(isset($_POST['submit'])){
//process the form
$date = $_POST["date"];
$customer_name = $_POST["customer_name"];
$product_description = $_POST["product_description"];
$quantity = $_POST["quantity"];
$status = $_POST["status"];
$query = "
INSERT INTO orders (
date, customer_name, product_description, quantity, status
) VALUES (
'$date', '$customer_name', '$product_description',$quantity,$status
)";
$order_set = mysqli_query($connection, $query);
if($order_set){
redirect_to("index.php");
}
} else {
// failed
}
?>
My Form
<form action="order.php" method="post">
<div class="newOrder">
<p><span>Date</span><input type="date" value="2014-12-01" name="date" /></p>
<p><span>Name</span>
<select name="customer_name">
<?php
while($customer = mysqli_fetch_assoc($customers_set)){ ?>
<option><?php echo $customer['customer_name']; ?></option>
<?php } ?>
<?php mysqli_free_result($customers_set); ?>
</select>
</p>
<div id="input_fields">
<p><span>Product Description</span>
<select name="product_description">
<?php
while($product = mysqli_fetch_assoc($product_set)){ ?>
<option><?php echo $product['product_description']; ?></option>
<?php } ?>
<?php mysqli_free_result($product_set); ?>
</select>
<input value="0" type="text" name="quantity" />
</p>
</div>
Add More Product
<p class="radio">
<input type="radio" name="status" value="0" checked />For delivery
<input type="radio" name="status" value="1" />For payment confirmation
<input type="radio" name="status" value="2" />Reserved items
</p>
<input type="submit" name="submit" value="Create Order" />
</div>
</form>
any body have any idea how to submit all product and quantity input in input field will be save in database.
Wrap your values inside your database connection. Consider this from one of my old course. Notice is a different code however working perfectly.
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];
$dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool', 'aliendatabase')
or die('Error connecting to MySQL server.');
$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
mysqli_close($dbc);
The input fields have the same name? So I guess thats why only the last one get inserted.
You have to loop the INSERT query foreach product you add, this includes quantity.
You should allso sanitize the input value before you inserting it to a query.
When you inserting multiple queries, you shouldn't do that from the php loop. Is not efficient because you are executing multiple queries instead one or two. You can loop trough the results sent from the form, clean it and prepare it for database insertion, and after that build a query based on that results. Look at here for the inserting multiple rows at once into a database :
(Insert multiple rows with one query MySQL)
you need to use foreach
foreach ($_POST['quantity'] as $quantity) {
//insert code
}
How do you add multiple fields to search database using html, php and mysql?
Here is the HTML code.
I would like to add more field options to search database for first name, age, gender.
Example: Search for [first name] AND/OR [Age] AND/OR [gender].
<h2>Search</h2>
<form name="search" action="searchresults.php" method="POST">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="firstName">First Name</option>
<Option VALUE="lastName">Last Name</option>
<Option VALUE="email">email</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
Here is the PHP.
Database connection
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$find=$_POST['find'];
$field=$_POST['field'];
$data="SELECT firstName, lastName, email, userphoto, age FROM contactInfo WHERE upper($field) LIKE '%$find%'";
$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"];
$age= $row["age"];
$age = array();
$userphoto = array();
$firstName = array();
$lastName = array();
while ($row = mysql_fetch_array($result))
{
$userphoto[] = "<img src='images/".$row['userphoto']."' height='200' width='175'>";
$firstName[] = $row['firstName'];
$lastName[] = $row['lastName'];
$age[] = $row['age'];
$email[] = $row['email'];
}
while(!empty($userphoto))
{
//output images
foreach(array($userphoto, $firstName, $lastName, $age, $email) as $items)
{
echo "<tr>";
foreach($items as $key=>$item)
{
echo "<td><font size =\"3\" >$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);
}
echo"</table>";
I would divide the query in two parts:
First create a static part:
$query = "SELECT firstName, lastName, email, userphoto, age FROM contactInfo WHERE"
And then calculated the dinamic part:
For each field select by the user according to the and/or option:
$query = $query . $nameField . ("and" || "or") . $valueField
Change Your query.
$data="SELECT firstName, lastName, email, userphoto, age FROM contactInfo WHERE `".$field."` LIKE '%".$find."%'";
I have a function in php that allows the user to input information and add it into a database of sql. It seems that my very first value is always missing when more of the data is added. The very last value seems to be shifted down so that there isn't even any values there. Here is the code:
$firstname = $_POST['Fname'];
$lastname = $_POST['Lname'];
$age = $_POST['age'];
$gender = $_POST['Gender'];
$city = $_POST['city'];
$email = $_POST['email'];
$condition = $_POST ['condition'];
if ($condition == 'add')
{
$SQL = "INSERT INTO People (Last_Name, First_Name, Age, Gender, Email, Cityid)
VALUES ('$lastname', '$firstname', '$age', '$gender', '$email', '$city')";
$result=mysql_query($SQL) or die(mysql_error());
}
Here is the HTML:
<form action = 'Project 5 Page 2.php' Method = 'POST'>
<center>
First Name <input type = 'text' maxlength = '20' name = 'Fname'>
<br>
Last Name <input type = 'text' maxlength = '20' name = 'Lname'>
<br>
Email <input type = 'text' maxlength = '100' name = 'email'>
<br>
Male <input type = 'radio' name = 'Gender' value = 'Male'>
Female <input type = 'radio' name = 'Gender' value = 'Female'>
<br>
Age <Select name = 'age' value = 'agename'>
<option value = 'agevalue'>Select</option>
<?php
for ($i=0;$i<121;$i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
</select>
<br>
<?php
$SQL="SELECT * from cities ORDER BY province ASC";
$result=mysql_query($SQL) or die(mysql_error());
$num_results=mysql_num_rows($result);
mysql_close($db);
?>
City <select name = 'city' value = 'citychoose'>
<option value = 'citychoice'>Select</option>
<?php
for ($c=0;$c<$num_results;$c++)
{
$row=mysql_fetch_array($result);
echo "<option value='".$row['id']."'>".$row['name'].", ".$row['province']." </option>";
}
?>
</select>
<br>
Add <input type = 'radio' name = 'condition' value = 'add'>
Edit <input type = 'radio' name = 'condition' value = 'Edit'>
Delete <input type = 'radio' name = 'condition' value = 'Delete'>
<br>
<input type="submit" value = Submit>