So right now my input form looks something like this
<form method = "post" action = "Display_Department_Info.php">
Select Department :
<select name = "dept">
<option value="" selected >-- Select One--</option>
<?php
$conn = mysqli_connect('localhost', 'root', 'root', 'DM Phase 2');
if(!$conn) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$result = mysqli_query($conn, 'select name from department');
while($row = mysqli_fetch_array($result)) {
$val = $row['name'];
echo "<option value ='".$val."'>".$val."</option>";
}
mysqli_close($conn);
?>
</select>
<input type = "submit" value = "Submit" />
</form>
but when I use the following command to read $_POST[dept] for a particular input
"Children's IKEA", it only displays "Children"
This is my reading code.
$departmentinput = mysql_real_escape_string("$_POST[dept]");
Please help! thank you :3
The ' is breaking it because you are using ' to quote the value:
$val = htmlspecialchars($row['name'], ENT_QUOTES);
//or
$val = htmlentities($row['name'], ENT_QUOTES);
Related
I have this database with the following data:
data 1: text1
data 2: text2
.
.
.
data 14: text 14
I was trying to input this data into a "select" column. So I had done this:
<script>
function dropdownlistchange(dropDown) {
var selectedValue = dropDown.options[dropDown.selectedIndex].value;
document.getElementById("category").value = selectedValue;
}
</script>
and the display is:
<?php
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
mysqli_select_db($con,"adminsys") or die ("no database");
$query = "SELECT * FROM productmodelcategory";
$results = mysqli_query($con, $query)or die("Connection could not be established");
echo "<select class='categoryoption' name='categorylist' id='categorylist' onChange='dropdownlistchange(this);'>";
while ($row = mysqli_fetch_assoc($results))
{
echo "<option value='".$row['Category']."'selected='selected'>".$row['Category']."</option>";
}
echo "</select>";
?>
and my display output is:
<input name="category" type="hidden" id="category" value="" required="required"/>
The problem that I having is I am attaching this coding in a submit form, and when I try to not selecting any value due to leave it as default value, and when i submit form, the value displaying "Column 'Category' cannot be null". Anyone can help me solve this problem? I not sure changing the "onchange" method into what method.
Your category input will hold some value when user actually select something from dropdown. If user didn't select anything in this case category input will not hold any value.
Do one thing assign value attribute to your category input like below
<input name="category" type="hidden" id="category" required="required" value="0"/>
Else, you can set 1st row value as default as follow
<?php
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
mysqli_select_db($con,"adminsys") or die ("no database");
$query = "SELECT * FROM productmodelcategory";
$results = mysqli_query($con, $query)or die("Connection could not be established");
echo "<select class='categoryoption' name='categorylist' id='categorylist' onChange='dropdownlistchange(this);'>";
$i = 0;
$default_value =0;
while ($row = mysqli_fetch_assoc($results))
{
if($i == 0){
$default_value = $row['Category'];
$i = 1;
}
echo "<option value='".$row['Category']."'selected='selected'>".$row['Category']."</option>";
}
echo "</select>";
?>
<input name="category" type="hidden" id="category" required="required" value="<?php echo $default_value;?>"/>
i need insert new location to row with the name i choose from my drop down.
How can i connect between the name and the location?
<?php
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM test';
$res = mysqli_query($connect, $query);
echo "<select name='testform'>";
while($row=mysqli_fetch_assoc($res)){
echo "<option value=>$row[name]</option>";
}
echo "</select>";
?>
<html>
<form action="indexx.php" method="POST">
<br>Locatio name:<input type= "text" method="POST"><BR>
<input type="submit" value="Insert" method="POST">
</form>
</html>
Check if the form was submitted by giving it a name and checking if it is set in your PHP code. If it is set, it means that the user submitted your data entry form. Perform an INSERT statement with the value the user entered.
Sidenote: input-tags don't have an attribute "method". You can remove those.
You might want to read about the lifecycle of a PHP script and the usage of the $_POST array in PHP. The syntax of the INSERT statement can be found in numerous language specs or tutorials.
I think you mixed things up a bit and wrote the following code for you. Please tell me if this explains it a bit more.
The code you require
<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}
// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM test';
$res = mysqli_query($connect, $query);
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br />
New name: <input type="text" name="new_location"><br />
<input type="submit" name="submit" value="Update" />
</form>
<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}
// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM test';
$res = mysqli_query($connect, $query);
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br />
New name: <input type="text" name="new_location"><br />
<input type="submit" name="submit" value="Update" />
</form>
I am trying update an id that has a foreign key to another table of names. I have a drop menu and in the drop menu I have name from table NAME_TEST. I need to select the name but the insert that I want is:
INSERT INTO (test) values (the value that i need is the ID of selected name)
Code:
<html> <h1>Update form</h1></html>
<?php
if (isset($_POST['submit'])) {
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = "UPDATE test SET location_name='".$_POST['new_location']."' WHERE id='".$_POST['location']."' LIMIT 1";
$res = mysqli_query($connect, $query);
if (!$res) {
die("Something went wrong");
}
}
// This is the code to load the select options
$connect = mysqli_connect('localhost', 'root', '', 'test');
$query = 'SELECT * FROM name_test';
$res = mysqli_query($connect, $query);
echo "Choose setup";
$options = array();
while($row = mysqli_fetch_assoc($res)) {
$options[] = $row;
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"><BR><BR>
<select name="location">
<option value="0">--- Select an option ---</option>
<?php foreach ($options as $option): ?>
<option value="<?= $option['id'] ?>"><?= $option['name'] ?></option>
<?php endforeach; ?>
</select><br /><BR><BR>
<B> New name:</B> <BR> <input type="text" name="new_location"><br /><BR><BR>
<input type="submit" name="submit" value="Update" />
</form>
You can use a join for this. The idea is something like this:
INSERT INTO t(nameid)
select nameid
from names n
where n.name = ?;
t is the table you want to insert into. names is the table that has the name id and the name.
I have two input text fields where user has to specify the begin and end of the fly.
<input type="text" name="start" placeholder="Start destination">
<input type="text" name="end" placeholder="End destination">
I would like to change that and give user to chose start and end destination from database.
<select>
<option value="$id">$name</option>
</select>
I know how to get done if i read database and input values manually, but i know its posible if page loads and execute my SELECT QUERY.
So i have to create dropdown list and fill that with a values from database.
This dropdown list has to be filled when the page load.
Some idea for this ???
I am working with php.
Thank you in advance !!
EDIT : I get done this only with php.
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "flights";
$conn = mysql_connect("$db_host","$db_username","$db_password") or die ("no conn");
#mysql_select_db("$db_name") or die ("no database");
if ($conn = true) {
// echo "";
}
//cyrilic
$sql = "SET NAMES 'utf8'";
mysql_query($sql);
//query for end
$sql="SELECT Distinct end from flights_table;";
$result=mysql_query($sql);
echo "<select name=\"city\">";
echo "<option>end destination</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['end']."'>".$row['end']." </option>";
}
echo "</select>";
?>
This php fires when page loads. Those select options i have putted in a form, and when form is submited, it fires php itself. I am getting selected options this way :
$startfly=$_POST['end'];
I am doing this for starting the flight :)
Thank you guys !
Try this :
At the top of page include your database connection file :
<?php
require "connection.php";
?>
Then :
<?php
$selectStart = "Start : <select name='start'>";
$selectEnd = "End : <select name='end'>";
$query = mysql_query("SELECT * FROM someTable ORDER BY dateField ASC");
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
$selectStart .= "<option value='".$row['startItem']."'>".$row['startItemName']."</option>";
$selectEnd .= "<option value='".$row['endItem']."'>".$row['endItemName']."</option>";
}
}
$selectStart = "</select>";
$selectEnd = "</select>";
?>
In your HTML :
<form action='destinationPage.php' method='post'>
<?php
echo $selectStart;
echo $selectEnd;
?>
<input type='submit' value='Submit' />
</form>
can anybody post an code for option list which can retrieve data dynamically from database, and once user selects from option list a record, that record must post it ($_POST) to database.. !!
ive tried this, it retrieves records from db, but not posting it :
<?php
require_once "db.php";
if (isset($_POST['a_id']) {
$a = $_POST ['a_id'];
$sql = "INSERT INTO projektet
VALUES ('$a')";
mysql_query($sql);
}
HERE IS THE PART SEEMS NOT WORKING :
<form method="post">
<select name="a_id">
<?php
$host="localhost";
$username = 'root';
$password = "";
$con = mysql_connect($host,$username,$password);
mysql_select_db('naho',$con);
// Checking connection
if (!$con){
echo ("Failed to connect to MySQL:. " .mysql_error($con));
}
else {
echo("db connect");
}
$result = mysql_query("SELECT * from `arqitekti`");
if($result == FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row=mysql_fetch_array($result)){
?>
<option value="<?php '.row[a_id];'?>"><?php echo $row["a_emri"];?></option>
<?php }
?>
</select>
<input type="submit" value="submit"/>
</form>
<option value="<?php '.row[a_id];'?>"><?php echo $row["a_emri"];?></option>
This line looks strange... Shouldn't it be:
<option value="<?php echo $row[a_id]; ?>"><?php echo $row["a_emri"];?></option>
I think that your code is POSTing, but it wasn't getting any value because of the value declaration on the options of the select. After that change it should work.
PS: If your POST code isn't in the same page as your HTML, it's <form method="POST" action="YOUR_PAGE">, not only <form method="POST">.