I'm working on a project for a class and I'm having trouble passing on a variable from a Chosen selector. I am passing on the $inst variable just fine, but the $focus variable is giving me this:
Undefined index: chzn2 in C:\xampp\htdocs\phptest\results.php on line 20.
fyi: I have allowed for null in the database
Here is the code for the selectors.
$sql = "SELECT institutionName, instID FROM institutions";
$dropq = mysqli_query($conn, $sql);
echo "<select data-placeholder='Select an Institution' name='chzn1' class='chzn-select' standard='true' style='width:250px;'>";
while ($row = mysqli_fetch_array($dropq)) {
echo "<option></option><option value='" . $row['instID'] . "'>" . $row['institutionName'] . "</option>";
}
echo "</select>";
$sql1 = "SELECT orgFocus FROM communityOrgs";
$dropq = mysqli_query($conn, $sql1);
echo "<select data-placeholder='Select Focus Area' name='chzn2' class='chzn-select' multiple='true' style='width:250px;'>";
while ($row1 = mysqli_fetch_array($dropq)) {
echo "<option></option>
<option value='" . $row1['orgFocus'] . "'>" . $row1['orgFocus'] . "</option>";
}
echo "</select>";
Here is the code for the results
if (isset($_POST['submit-search'])) {
if(empty($_POST['chzn1']) && empty($_POST['chzn2'])){
echo "Please enter at least one value!";
}
else if(!empty($_POST['chzn1']) || !empty($_POST['chzn2'])) {
// Grabbing variables from User Inputs
$inst = $_POST['chzn1'];
$focus = $_POST['chzn2'];
$query = "SELECT * FROM partnerships WHERE instID = '$inst' OR orgFocus = '$focus'";
$result = mysqli_query($conn, $query);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<div class='results-container'>
<h3>".$row['instName']."</h3>
<p>".$row['orgName']."</p>
<p>".$row['orgFocus']."</p>
</div>";
;}
}
As the error suggests:
Undefined index: chzn2
and according to your logic one out of the two values can be passed that leaves the ability for one to be unset so an error is thrown - as it should.
You need to do a check to see if either one of the $_POST keys exist separately and set their values respectively, either to what was passed or null.
Example
<?php
$chzn1 = null;
$chzn2 = null;
if (!empty($_POST['chzn1']))
$chzn1 = $_POST['chzn1'];
if (!empty($_POST['chzn2']))
$chzn1 = $_POST['chzn2'];
?>
Now they will both have default value so the error won't be thrown.
Note: Seeing as you are using mysqli_* make use of prepared statements.
Update #1
Maybe due to a formatting error but you also have a floating semicolon ;:
;}
Select and option is not separated. As you are echoing separate make it combine.
$row = "<select data-placeholder='Select Focus Area' name='chzn2' class='chzn-select' multiple='true' style='width:250px;'>";
while ($row1 = mysqli_fetch_array($dropq)) {
$row .= "<option></option>
<option value='" . $row1['orgFocus'] . "'>" . $row1['orgFocus'] . "</option>";
}
$row .= "</select>";
echo $row;
Same for other select tag
Related
I am using the below code to populate drop down list in php html,
<?php
$mid="mario";
$sql = "SELECT * FROM tbl_prdy WHERE col_master_id = '$mid'";
$result = mysqli_query($conn,$sql);
echo "<select name='list'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] . "
</option>";
}
echo "</select>";
?>
But, I am getting internal server error. I have debugged the code and found that the issue is with the following 2 lines in the above code. There is not much information in server logs. Can you tell me what might be the issue with the following 2 lines of code?
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['col_of_fa'] . "'>" . $row['col_of_fa'] .
"/option>";
}
mixing mysqli with mysql
change
$row = mysql_fetch_array($result)
to
$row = mysqli_fetch_array($result)
You would use this
while($row=mysqli_fetch_assoc($result )){
}
or
while($row=mysqli_fetch_array($result )){
}
//Try this :
while ($row = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row['col_of_fa'] ?>" ><?php echo $row['col_of_fa'] ?>
</option>
<?php }
I'm trying to submit a form that contains a schedule for each user ID. So far it looks like this:
$sql = "SELECT * FROM dbtable";
$result = $conn->query($sql);
$name_info = "SELECT udidId, name FROM udid";
$name_result = $conn->query($name_info);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$udidId = $row["udidId"];
echo "<label for='hours' class='schedule'><strong>I want <span>".$row["name"]."</span>";
echo "<input type='text' name='udidId' class='hidden' value='".$row["udidId"]."' />";
echo " to be <br />allowed out between <input type='text' name='outAllowedStartHour' placeholder='8' value='" . $row["outAllowedStartHour"] . "'> - <input type='text' name='outAllowedEndHour' placeholder='8' value='" . $row["outAllowedEndHour"] . "'><br />allowed in between <div class='padd_left'></div><input type='text' name='inAllowedStartHour' placeholder='8' value='" . $row["inAllowedStartHour"] . "'> - <input type='text' name='inAllowedEndHour' placeholder='8' value='" . $row["inAllowedEndHour"] . "'></strong></label>";
}
}
if(isset($_POST["update_schedule"])) {
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value' <br />";
while($row = $result->fetch_assoc()) {
foreach($value as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
$update_pets = "UPDATE v_spottData SET $x_value = $x_value WHERE udidId = $x";
$conn->execute($update_pets);
}
}
}
However is only updating inputs from the last ID in the database, and is not updating the input values at all. Any suggestions?
Execute doesn't execute a query, it executes a prepared statement. You need to use prepare to prepare the query.
Prepared statements should use placeholders. The quoting/escaping will be handled by the driver.
Note columns can't be bound/placeheld.
Your current query is trying to update a column with the same value, that can't be right. Change $updating_column below to whatever column you are trying to update.
$columns = array('outAllowedStartHour', 'outAllowedEndHour', 'inAllowedStartHour', 'inAllowedEndHour'); // whitelist columns
if(in_array($updating_column, $columns)) {
$update_pets = "UPDATE v_spottData SET `$updating_column` = ? WHERE udidId = ?";
$stmt = $con->prepare($update_pets);
$stmt->bind_param("ii", $x_value, $x);
$stmt->execute();
} else {
echo 'Using a Not Allowed Column';
}
You can read more about prepared statements here, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php.
I feel really silly, but for anyone else dealing with the issue, my solution was simple.
Try putting the PHP to handle the form submission at the top of your document, instead of at the bottom. Everything worked fine once I moved it up!
Thank you for all of your help everyone, especially #chris85!
im having some problem here. basically, i want to compare columns. so i fetched object and the comparing results appeared just as expected. however, it does not return the compare value anymore after i added the fetch_array to view the current table hoping that the compare value would appear beside the compare value. is there any way i could run the compare code and make it appear the table? i tried a query but it would only work in MySQL and not PHP.
$query = "SELECT * FROM system_audit"; $result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'];
echo $row['Type'];
echo $row['Setting'];
echo $row['Value'];
}
while ($row = mysql_fetch_object($result)) {
if($row->Setting != $row->Value) {
echo "X";
} else {
echo "O";
}
}
Your code contains a lot of echo's that have no use. I would suggest learning PHP a bit more.
Your compare is wrong, this should work :
$query = "SELECT * FROM system_audit";
$result = mysql_query($query) or die(mysql_error());
echo " ID Type Setting Value ";
while($row = mysql_fetch_array($result)) {
echo $row['ID'] . "<br>";
echo $row['Type'] . "<br>";
echo $row['Setting'] . "<br>";
echo $row['Value'] . "<br>";
if($row['Setting'] != $row['Value']) {
echo "X" . "<br>";
}
else {
echo "O" . "<br>";
}
echo "<br>";
I am populating a drop down menu from mysql database. It works well, But I want it not to repeat values. (i.e if some value is N times in database it comes only once in the drop down list)
Here is my code:
<?php
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['year'] . "'>" . $row['year'] . "</option>";
}
echo "</select>";
?>
Use DISTINCT in your query.
"SELECT DISTINCT year FROM data";
just change Your query. is better
$sql = "SELECT distinct year FROM data";
Another technique:
select year from table group by year
in PHP side you have to do this
$all_data = array();
echo "<select name='year'>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
array_push($all_data,$row["column"]);
}
$all_data = array_unique($all_data);
foreach($all_data as $columns => $values){
print("<option value='$value'>$value</option>");
}
echo "</select>";
Here is a simple trick. Take a boolean array. Which value has not come in list print it in list and which value has come in list already once, set it as true through indexing the boolean array.
Set a condition, if boolean_array[ value ] is not true, then show value in list. Otherwise, don't.
mysql_connect('host', 'user', 'pass');
mysql_select_db ("database");
$sql = "SELECT year FROM data";
$result = mysql_query($sql);
echo "<select name='year'>";
while ($row = mysql_fetch_array($result)) {
if($bul[$row['year']] != true){
echo "<option value='" . $row['year'] . "'>" . $row['year'] . " </option>";
$bul[$row['year']] = true;
}
}
echo "</select>";
?>
What i'm trying to do is display a drop down with all field names from mysql database, once the user picks one and submits the form i want to display a second dropdown filled with all the rows from the submitted field name, this is my code so far:
$result = mysql_query("select * from `parts`") or die(mysql_error());
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<select name='field_names'>";
$i = 0;
while ($i < mysql_num_fields($result)) {
$fieldname = mysql_field_name($result, $i);
echo '<option value="'.$fieldname.'">'.$fieldname.'</option>';
$i++;
}
echo "</select>";
echo "<input type='submit' value='submit'></input>";
echo "</form>";
if($_POST) {
$fields = $_POST['field_names'];
$result1 = mysql_query("select '".$fields."' from `parts`") or die(mysql_error());
echo '<select name="fields">';
while ($row = mysql_fetch_array($result1)) {
echo "<option value=".$row[$fields].">".$row[$fields]."</option>";
}
echo '</select>';
}
Can anyone spot where i'm going wrong, thanks
there is a mistake on the line number 29
$result1 = mysql_query("select '" . $fields . "' from `parts`") or die(mysql_error());
you are using ' instead of `. Do as follows
$result1 = mysql_query("select `" . $fields . "` from `parts`") or die(mysql_error());
Hope your problem is solved.
As it stands now, the second set of selects will be issued OUTSIDE of your </form> tag, so will never get submitted with the rest of the form. At best, you should move the form closing tag to below the POST handler.
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly