php $_POST select option value not working undefined index error - php

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to
mysql');
$dbname = 'vendordb';
mysql_select_db($dbname);
?>
<html>
<div class="row">
<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF'];?>"
method="post">
<?php
$result=mysql_query("select crtname from crtinfo");
echo "<select id='criteria1' name='criteria1'" . ">";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row[0]. ">". $row[0]. "</option>";
}
echo "</select>";
$crt1=$_POST['criteria1'];
echo $crt1;
?>
</form>
</div>
Unable to get dropdown value in $_POST
Error:
Notice: Undefined index: criteria1 in C:\xampp\htdocs\website\dropdowntest.php on line 25
cannot get value from $_POST

Although I'm not sure if this will fix it, this is a sample on how to prevent "Undefined index" errors from appearing by using isset()
if(isset($_POST["critera"])
{
$crt1=$_POST['criteria1'];
echo $crt1;
}

You cannot get the post value. Try this:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to
mysql');
$dbname = 'vendordb';
mysql_select_db($dbname);
?>
<html>
<div class="row">
<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF'];?>"
method="post">
<?php
$result=mysql_query("select crtname from crtinfo");
echo "<select id=criteria1 name=criteria1" . ">";
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row[0]. ">". $row[0]. "</option>";
}
echo "</select>";
?>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST['criteria1'])){
echo $_POST['criteria1'];
}
?>
?>
</div>

Related

Updating MySQL using PHP

I'm using easyPHP. I'm trying to update the records in my database but I keep getting <?php echo $btitle; ?> and <?php echo $bauthor; ?> written in the text boxes in my HTML form and data isn't updated but it does print "Updated data successfully".
Here's my form code:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Update Book</title>
</head>
<body>
<h1>Update Your Library</h1>
<form method = "post" action = "editBook.php">
<?php
$conn = mysql_connect ("localhost", "root", "");
$db = mysql_select_db ("library", $conn);
$query = "select * from Books where No = ". $_GET['bid'];
$result = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($result))
{
$bid = $row ['bid'];
$btitle = $row ['btitle'];
$bauthor = $row ['bauthor'];
}
mysql_close($conn);
?>
<table>
<input type="hidden" name="bid" size="5" value="<?php echo $bid;?>">
<tr>
<td>Title:</td>
<td><input type="text" name="btitle" size="100"value="<?php echo $btitle;?>"></td>
</tr>
<tr>
<td>Author:</td>
<td><input type="text" name="bauthor" size="100" value="<?php echo $bauthor;?>"></td>
</tr>
</table>
<p>
<input type="submit" value="Update">
</p>
</form>
</body>
</html>
<?php
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = '';
$bid=$_POST['bid'];
$btitle=$_POST['btitle'];
$bauthor=$_POST['bauthor'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('library');
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "update books
set Title='$btitle',
Author='$bauthor'
where book_id='$bid'";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
print "updated data successfully\n";
mysql_close($conn);
?>
It seems like the files does not get parsed by PHP. The problem may be a filename extention not defined in configuration.
On other hand I see another problem which may raise when the first issue is solved. Viariables which hold information are out of scope. This will lead to empty answer.
When you read from database:
while ($row = mysql_fetch_assoc($result))
{
$bid = $row ['bid'];
$btitle = $row ['btitle'];
$bauthor = $row ['bauthor'];
}
those 3 variables are created in the while(){} scope and they do not exist outside of it. Just initialize them with empty string before the loop in the main scope:
$bid = $btitle = $bauthor = '';
while ($row = mysql_fetch_assoc($result))
{
$bid = $row ['bid'];
$btitle = $row ['btitle'];
$bauthor = $row ['bauthor'];
}
Try This ::
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Update Book</title>
</head>
<body>
<h1>Update Your Library</h1>
<form method = "post" action = "save.php">
<?php
$conn = mysql_connect("localhost", "root", "");
$db = mysql_select_db("library", $conn);
$query = "select * from Books where No = " . $_GET['bid'];
$result = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($result))
{
$bid = $row['bid'];
$btitle = $row['btitle'];
$bauthor = $row['bauthor'];
}
?>
<table>
<input type="hidden" name="bid" size="5" value="<?php echo (isset($bid))?$bid:'';?>">
<tr><td>Title:</td><td><input type="text" name="btitle" size="100" value="<?php echo (isset($btitle))?$btitle:'';?>"></td></tr>
<tr><td>Author:</td><td><input type="text" name="bauthor" size="100" value="<?php echo (isset($bauthor))?$bauthor:'';?>"></td></tr>
</table>
<p><input type="submit" value="Update"></p>
</form>
</body>
</html>
<?php
mysql_close($conn);
?>

why I can not store selected value by $_POST in php

I create a dropdown list to select values:
<form action="." method="post" id="aligned">
<input type="hidden" name="action" value="update_customer">
<input type="hidden" name="customer_id"
value="<?php echo htmlspecialchars($customer['customerID']); ?>">
<label>Country:</label>
<select name="selected">
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass ='';
$db = 'tech_support';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn)
die('Could not connect: '. mysql_error());
mysql_select_db($db);
$selected= mysql_query("select * from countries where countryCode = '" .$customer['countryCode']. "'");
$sql = mysql_query("select * from countries order by countryName");
if($selectedrow = mysql_fetch_array($selected)){
echo "<option selected value=\"VALUE\">" . $selectedrow['countryName']."</option>";
}
//echo "<select>";
while ($row = mysql_fetch_array($sql)) {
echo "<option value =\"VALUE\">". $row['countryName']."</option>";
}
//echo "</select>";
?>
</select><br>
In another php file as below, I was trying to store the selected value for Country, but nothing is stored, why?
<?php $country_name = $_POST["selected"];
echo $country_name;//it always print out 'VALUE'-I guess it means null.
?>
You should use
echo "<option value ='".$row['countryName']."'>". $row['countryName']."</option>";
And I want to add something, MySQL is deprecated in PHP5 and removed from PHP7. what is your PHP version. If you use PHP 5 or later use mysqli.

How to get a dynamic form with php that gets data from a database

I've got a dropdown menu in a form on my website which is filled with values of a database table which shows all the school-classes of my school. Now I have a second page in which I display all students that are in a database table, wiich are in the selected class, in a form with checkboxes.
My question is now as following:
How can I get these two pages in one together? So that all students of the selected class get displayed when i just select the class in the dropdown menu without pressing the submit button.
My Sourcecode:
1) Code of the website in which the class gets selected:
<html>
<?php
$db_host = "localhost";
$database = "internat_db";
$con=mysqli_connect($db_host, "root", "", $database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$klassen = mysqli_query($con, "Select * from klassen");
echo '<form action="schueler_ausgabe.php" method="POST">';
echo '<select name="Klassenwahl">';
while($row = mysqli_fetch_array($klassen))
{
echo "<option value=".$row['Klasse'].">".$row['Klasse']."</option>";
}
echo '<input id="submit" name="submit" type="submit" value="Auswaehlen" />';
echo '</form>';
mysqli_close($con);
?>
</html>
2) Code of the website that displays all students:
<html>
<?php
$klassenname = $_POST["Klassenwahl"];
$db_host = "localhost";
$database = "internat_db";
$con=mysqli_connect($db_host, "root", "", $database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$zw = "Select vorname, nachname FROM schueler WHERE klasse = '".$klassenname."'";
$schueler = mysqli_query($con, $zw);
echo '<form action="" method="POST">';
while($row = mysqli_fetch_array($schueler))
{
echo "<input type='checkbox' name='schueler' value=".$row['vorname']." ".$row['nachname'].">".$row['vorname']." ".$row['nachname']."";
echo '<br>';
}
echo '<input id="submit" name="submit" type="submit" value="Auswaehlen" />';
echo '</form>';
mysqli_close($con);
?>
</html>
Thank you very much for any help/idea out there!

Removing selected data from drop down list(php)

I need to create a function which retrieves my data from my database into a drop down list, then choose and remove the selected data. I have my drop down list done & working but I'm experiencing some error with my 'Remove' function.
<body>
<?php
//connecting to database
$server = 'localhost';
$username = 'root';
$password = 'infosys';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
//dropdown list to retrieve sentence
$sql = "SELECT words FROM sentence";
$result = mysql_query($sql);
echo "<select name
='dropdownlist'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['words'] ."'>" . $row['words'] ."</option>";
}
echo "</select>";
?>
<form method="post" action="remove.php">
<input type="submit" value="Remove" />
</form>
<a href="index.php" >View list</a>
</body>
Followed by another php file
<?php
$server = 'localhost';
$username = 'root';
$password = 'infosys';
$database = 'project';
mysql_connect($server,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$dropdownlist1 = $_POST['dropdownlist'];
$dropdownlist2 = $dropdownlist1.value;
if(isset($_GET['id'])) {
$mysql_query("DELETE FROM 'words' WHERE id = $dropdownlist2");
header("location: index.php");
exit();
}
?>
Move your Select box or dropdown inside the form tag,
<form method="post" action="remove.php">
<select name ='dropdownlist'>
<?php while ($row = mysql_fetch_array($result)) { ?>
<option value='<?php echo $row['words'];?>'><?php echo $row['words'];?></option>
<?php } ?>
</select>
<input type="submit" value="Remove" />
</form>
in php,
if(isset($_POST['dropdownlist'])) {
$dropdownlist1 = $_POST['dropdownlist'];
mysql_query("DELETE FROM `sentence` WHERE `words` = '$dropdownlist1' ");
header("location: index.php");
exit();
}
Note: Use mysqli_* or PDO functions instead of using mysql_* functions(deprecated)

Turning a mysql column into an array and using the array in a dropdown

Long time listener, first time caller. I'm having trouble with pulling a column called "Rep_IP" from a mysql table called "roster", turning it into an array, and then using that array to populate a dropdown in html. I've tried several suggestions listed here as well as other places and I'm not having any luck. The page shows up just fine but the dropdown has no options to select. I figured I would see if one of you could tell me what I am doing wrong here.
<html>
<body>
<form action="insert.php" method="post">
<p>Rep ID:</p>
<?php
$con = mysql_connect("localhost", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("rep_stats", $con);
$query = "SELECT Rep_ID FROM roster";
$result = mysql_query($query) or die ("no query");
$result_array = array();
echo "$query"
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
?>
<select name="Rep_ID">
<?php
foreach($result_array as $rep)
{
echo "<option value=" . $rep['Rep_ID'] . ">" . $rep['Rep_ID'] . "</option>";
}
?>
</select>
Issues Handled: <input type="number" name="IssuesHandled">
Hours Worked: <input type="number" step="any" name="HoursWorked">
<input type="submit">
</form>
</body>
</html>
As you can see, the drop down is part of a form that is used to create an entry in a new table as well. I don't know if that makes a difference but I figured I would point it out.
Try this.
<select name="Rep_ID">
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
?>
</select>
Try this:
<?php
function select_list(){
$host = "host";
$user = "user";
$password = "password";
$database = "database";
$link = mysqli_connect($host, $user, $password, $database);
IF (!$link)
{
echo ('Could not connect');
}
ELSE {
$query = "SELECT Rep_ID FROM roster";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
}
mysqli_close($link);
}
$begin_form =
<<< EODBEGINFORM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Insert data </title></head>
<body>
<form action="insert.php" method="post" name="form1">
<p>Rep ID:</p>
<select name="reps" form="form1">
EODBEGINFORM;
$end_form =
<<< EODENDFORM
</select><br>
Issues Handled: <input type="text" size="12" name="IssuesHandled"><br>
Hours Worked: <input type="text" size="12" name="HoursWorked"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EODENDFORM;
echo $begin_form;
echo select_list();
echo $end_form;
?>
You will notice that I have used MYSQLI_ istead of MYSQL_ the reason is that this better for new code, see the comments above.
I debugged your code and ran I to a lot of problems.:-( The main problem was:
echo "$query" Your forgot the semicolon at the end of the line.
Good luck with you project.

Categories