Alright, So I have 2 different tables in one DB, I am trying to use a while statement to echo out a name from one table and then echo out a selector for each name..
For instance, If there is only one name in the table, There would be only one selector, Else if there was two names, But the options selected value should be set to what it says in the DB... Right now everything is echoing out correctly but all the selected for the option are not being unique, They are staying the same as the one before it. So If I set one to disabled, The selector shows them all as disabled.. Here is the code
<?php
$userID = $_GET['uid']; // from the query string in the link
$_SESSION['userid'] = $userID;
$query = $con->prepare("SELECT * FROM users WHERE ID = '". $userID ."'") or die(mysqli_error());
$query->execute();
$user=$query->fetch(PDO::FETCH_ASSOC);
// echo out user details:
echo '<b>' . 'User Info:' . " " . '</b>';
echo '<br>';
echo $user['last']. "," . " " .$user['first'];
echo '<br>';
echo 'Business Unit';
echo '<br>';
echo $user['busunit'];
echo '<br>';
echo 'Location';
echo '<br>';
echo $user['location'];
echo '<br>';
echo 'Position';
echo '<br>';
echo $user['position'];
echo '<br>';
echo '<br>';
echo '<b>Access Info</b>';
echo '<br>';
$number = '1';
$number++;
$new = 'op' . $number;
$stmt = $con->prepare("SELECT * FROM access");
$stmt->execute();
$userIDi = $user['ident'];
$q = $con->prepare("SELECT * FROM priv WHERE ident = :ID ");
$q->bindparam(":ID", $userIDi);
$q->execute();
$priv=$q->fetch(PDO::FETCH_ASSOC);
$val1 = 'Yes';
$val2 = 'No';
echo '<form method="post">';
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['name'];
echo ' ';
echo '
<select id='.$new.'>
<option value="Yes"';
if($priv[$new]==$val1){echo 'selected="selected"';} echo '>Enabled </option>';
echo '<option value="No"';
if($priv[$new]==$val2){echo 'selected="selected"'; } echo '>Disabled </option>';
echo '</select>';
echo '<br>';
}
echo '<br>';
echo '<input type="submit" name="submit"/>';
echo '</form>';
Updated code to this,
Still cannot get it to work.
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['name'];
echo ' ';
echo '
<select id='.$new.'>';
while($priv=$q->fetch(PDO::FETCH_ASSOC)){
echo '<option value="Yes"'; if($priv[$new]==$val1){echo 'selected="selected"' . '>Enabled </option>';}
echo '<option value="No"'; if($priv[$new]==$val2){echo 'selected="selected"' . '>Disabled </option>'; }
}
echo '</select>';
echo '<br>';
}
Here is the third update..
Now the first selector is working dynamiclly, It will changed to enabled or disabled but the other ones just stay static.
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['name'];
echo ' ';
echo '
<select id='.$new.'>
<option value="Yes"';
while($priv=$q->fetch(PDO::FETCH_ASSOC)){
if($priv[$new]==$val1){echo 'selected="selected"';} echo '>Enabled </option>';
echo '<option value="No"';
if($priv[$new]==$val2){echo 'selected="selected"'; }} echo '>Disabled </option>';
echo '</select>';
echo '<br>';
}
echo '<br>';
echo '<input type="submit" name="submit"/>';
echo '</form>';
Related
I have made a form that lets the user to select a year and place they want. The select tag for year is as below:
<strong>Tahun: </strong>
<?php
echo ('<select name="tahun">');
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo ('<option value= "'.$i.'">'.$i.'</option>');
}
echo ('</select>');
?>
And code for place selection tag as below:
<strong>Tempat: </strong>
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error._LINE_);
while($row = $results->fetch_assoc()){
echo ('<option value= "'.$row['nama'].'">'.$row['nama'].'</option>');
}
echo ('</select>');
?>
Now, what I want to do is add empty value with a message "Please choose one" for both selection tag. So, can someone help me how to add that value and message in this tag?
If you wanted to add select options to your code, this is how it should be:
<?php
echo ('<select name="tempat">');
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo ('<option>Please choose one </option>'); // Added this here
while ($row = $results->fetch_assoc()) {
echo ('<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>');
}
echo ('</select>');
?>
When you add default option with empty value then you should add as <option value="">Select...</option>
<strong>Tahun: </strong>
<?php
echo '<select name="tahun">';
echo '<option value="">Please Choose One</option>';
for($i = date("Y"); $i < date("Y")+11; $i++)
{
echo '<option value= "'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
and
<?php
$query = "SELECT *FROM bilik WHERE kosong = 'tak'";
$results = $mysqli->query($query) or die($mysqli->error . _LINE_);
echo '<select name="tempat">';
echo '<option value="">Please choose one</option>'; //Please choose one
while ($row = $results->fetch_assoc()) {
echo '<option value= "' . $row['nama'] . '">' . $row['nama'] . '</option>';
}
echo '</select>';
?>
My website contains a page to add data to a database. Now I'm working on the output of that data and making it able to edit it, if necessary.
On my add page, I have a dropdown with several options. If selected, that value goes to the database. Now I want to reverse it. Select the value from the database and show it in a dropdown with the same values as on the add page. But the value from the database needs to be default selected.
In my code I only try with 2 values, but should be easy to add more if this codes works.
code:
<?php
$A = "A";
$B = "B";
$query = mysqli_query($conn, "SELECT * FROM table WHERE id='$id'");
while ($record = mysqli_fetch_assoc($query))
{
echo "<table>";
echo "<tr>";
echo '<td><b>Type: </b>
<select name="type">
if ($record['type'] == "A"){
echo "<option selected value ="' . $A . '">A</option>";
echo "<option value="' . $B . '">B</option>";
} elseif ($record['type'] == "B"){
echo "<option selected value="' . $B . '">B</option>";
echo "<option value="' . $A . '">A</option>";
}
</select>
</td>';
echo "</tr>";
echo "</table>";
}
?>
I think I'm missing some "" or '' quotes somewhere.
Your if's are inside a string because you've not closed the echo, therefore they are being handled as strings and not actual code.
<?php
$A = "A";
$B = "B";
$query = mysqli_query($conn, "SELECT * FROM table WHERE id='$id'");
while ($record = mysqli_fetch_assoc($query))
{
echo "<table>";
echo "<tr>";
echo '<td><b>Type: </b>
<select name="type">';
// You were missing closing '; on the line above
if ($record['type'] == "A"){
echo "<option selected value ="' . $A . '">A</option>";
echo "<option value="' . $B . '">B</option>";
} elseif ($record['type'] == "B"){
echo "<option selected value="' . $B . '">B</option>";
echo "<option value="' . $A . '">A</option>";
}
// Dont forget to then open the echo again
echo ' </select>
</td>';
echo "</tr>";
echo "</table>";
}
?>
You are already used one echo Don't want use multiple echo for display one string.
echo '<td><b>Type: </b>
<select name="type">'.
if ($record["type"] == "A"){.'
<option selected value ="' . $A . '">A</option>
<option value="' . $B . '">B</option>
.'} elseif ($record['type'] == "B"){.'
<option selected value="' . $B . '">B</option>
<option value="' . $A . '">A</option>.'
}.'
</select>
</td>';
Other way is using Concatenate with variable is:
$A = "A";
$B = "B";
$query = mysqli_query($conn, "SELECT * FROM table WHERE id='$id'");
$tbl = '';
while ($record = mysqli_fetch_assoc($query))
{
echo "<table>";
echo "<tr>";
$tbl .= '<td><b>Type: </b>
<select name="type">';
if ($record["type"] == "A"){
$tbl .= '<option selected value ="' . $A . '">A</option>
<option value="' . $B . '">B</option>';
} elseif ($record['type'] == "B"){
$tbl .= '<option selected value="' . $B . '">B</option>
<option value="' . $A . '">A</option>';
}
$tbl .= '</select>
</td>';
echo $tbl;
echo "</tr>";
echo "</table>";
}
I create this code to display a list of countries, I would retrieve the selected country but it does not work
<?php
include 'includes/combo.php';
?>
<?php
echo "<label for='pays'>Pays*</label>
<select id='pays' name='pays '>";
while ($row = mysql_fetch_array($combo)) {
echo "<option>$row[0]</option>";
}
echo "</select> ";
echo "<option>$row[0]</option>";
?>
and recover with a
if(isset($_POST['pays']))
$pays=$_POST['pays'];
else
$pays="";
echo $pays;
but the pays value is not recouped
echo"<option value=" . $row[0] . ">$row[0]</option>";
To retrieve the value of your option
Try this
<?php
include 'includes/combo.php';
?>
<?php
echo '<label for="pays">Pays*</label>
<select id="pays" name="pays">';
while ($row = mysql_fetch_array($combo)) {
echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
}
echo '</select>';
?>
You have one space after name in select
And your option hasn't value attribut
And I again I have to deal with PHP and MySQL :) So, I've got a mysql table and want to have "add" feature. So I can edit my table from browser easily. So far I have done nearly everything but the problem is that variable consisting "number" from my table (or id) won't _POST to other page. It won't _POST directly from textarea or even if I put it in a hidden field(well, have just understood that this is pretty much the same).
Let me show you some examples:
$a = mysql_query("SELECT number FROM peoples ORDER BY number DESC LIMIT 1");
$number_a = mysql_fetch_assoc($a);
$number = $number_a['number']+1;`
That's how I got this variable.
echo '<input name="id" type="hidden" value="'.$number.'" />';
That's how I pass it.
echo $number = mysql_escape_string( $_POST['id'] );
That's how I tried to get it in other php file.
Everything from other textareas passes just fine.
Full code as requested. ADD.PHP:
<?php
$dblocation = "127.0.0.1";
$dbname = "tvp";
$dbuser = "root";
$dbpasswd = "";
$dbcnx = #mysql_connect($dblocation,$dbuser,$dbpasswd);
if (!$dbcnx)
{
echo( "<P>В настоящий момент сервер базы данных не доступен, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
if (!#mysql_select_db($dbname, $dbcnx))
{
echo( "<P>В настоящий момент база данных не доступна, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
echo '<form name="editform" action="adder.php" method="POST">';
echo '<table>';
echo '<tr>';
echo '<td>Номер</td>';
$a = mysql_query("SELECT number FROM peoples ORDER BY number DESC LIMIT 1");
$number_a = mysql_fetch_assoc($a);
$number = $number_a['number']+1;
echo $number;
var_dump($number);
print_r($number);
echo '<td><textarea name="number" >'.$number.'</textarea></td>';
echo '<input name="id" type="hidden" value="'.$number.'" />';
echo '</tr>';
echo '<tr>';
echo '<td>Имя</td>';
echo '<td><textarea name="givenName">'.$man['givenName'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Инициалы</td>';
echo '<td><textarea name="middleInitial">'.$man['middleInitial'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Фамилия</td>';
echo '<td><textarea name="surname">'.$man['surname'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Пол</td>';
echo '<td> <input type="radio" name="gender" value=1 >Man<Br>
<input type="radio" name="browser" value=0>Woman<Br> </td>';
echo '</tr>';
echo '<tr>';
echo '<td>Город</td>';
echo '<td><textarea name="city">'.$man['city'].'</textarea></td>';
echo '</tr>';
echo '<input name="id" type="hidden" value="'.$id.'" />';
echo '<input name="statee" type="hidden" value="'.$man['state'].'" />';
echo '<tr>';
echo '<td>Штат</td>';
?>
<td><select size="3" name="state">
<option disabled>Выберите штат</option>
<option value="AL"
<?php
if($man['state']=="AL"){
echo "selected";
}?>
>Alabama
</option>
//...and so on...
</select></td>
<?php
echo '</tr>';
echo '<tr>';
echo '<td>Телефон</td>';
echo '<td><textarea name="telephone">'.$man['telephone'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>E-mail</td>';
echo '<td><textarea name="emailAddress">'.$man['emailAddress'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Дата</td>';
echo '<td><textarea name="birthday">'.$man['birthday'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Место работы</td>';
echo '<td><textarea name="occupation">'.$man['occupation'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Компания</td>';
echo '<td><textarea name="company">'.$man['company'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Вес</td>';
echo '<td><textarea name="weight">'.$man['weight'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Рост</td>';
echo '<td><textarea name="length">'.$man['length'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Адрес</td>';
echo '<td><textarea name="streetAddress">'.$man['streetAddress'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Почтовый индекс</td>';
echo '<td><textarea name="zipCode">'.$man['zipCode'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Страна</td>';
echo '<td><textarea name="country">'.$man['country'].'</textarea></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" value="Сохранить"></td>';
echo '<td><button type="button" onClick="history.back();">Отменить</button></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
?>
And ADDER.PHP:
<?php
$dblocation = "127.0.0.1";
$dbname = "tvp";
$dbuser = "root";
$dbpasswd = "";
$dbcnx = #mysql_connect($dblocation,$dbuser,$dbpasswd);
if (!$dbcnx)
{
echo( "<P>В настоящий момент сервер базы данных не доступен, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
if (!#mysql_select_db($dbname, $dbcnx))
{
echo( "<P>В настоящий момент база данных не доступна, поэтому
корректное отображение страницы невозможно.</P>" );
exit();
}
$number=$_POST["id"]; echo '<br>';
var_dump($_POST['id']);
print_r($POST['id']);
echo $number;
echo $givenName = mysql_escape_string( $_POST['givenName'] ); echo '<br>';
echo $middleInitial = mysql_escape_string( $_POST['middleInitial'] ); echo '<br>';
echo $surname = mysql_escape_string( $_POST['surname'] ); echo '<br>';
echo $gender = $_POST['gender'] ; echo '<br>';
echo $city = mysql_escape_string( $_POST['city'] ); echo '<br>';
echo $state = mysql_escape_string( $_POST['state'] );echo '<br>';
echo $emailAddress = mysql_escape_string( $_POST['emailAddress'] ); echo '<br>';
echo $telephone = mysql_escape_string( $_POST['telephone'] ); echo '<br>';
echo $birthday = mysql_escape_string( $_POST['birthday'] ); echo '<br>';
echo $occupation = mysql_escape_string( $_POST['occupation'] );echo '<br>';
echo $company = mysql_escape_string( $_POST['company'] ); echo '<br>';
echo $weight = mysql_escape_string( $_POST['weight'] ); echo '<br>';
echo $length = mysql_escape_string( $_POST['length'] ); echo '<br>';
echo $streetAddress = mysql_escape_string( $_POST['streetAddress'] ); echo '<br>';
echo $zipCode = mysql_escape_string( $_POST['zipCode'] ); echo '<br>';
echo $country = mysql_escape_string( $_POST['country'] ); echo '<br>';
$query = "INSERT INTO peoples (number,givenName, middleInitial, surname, gender, city, state, emailAddress, telephone, birthday, occupation, company, weight, length, streetAddress, zipCode, country) VALUES ( '".$number."', '".$givenName."', '".$middleInitial."', '".$surname."', '".$gender."', '".$city."', '".$state."', '".$emailAddress."', '".$telephone."', '".$birthday."', '".$occupation."', '".$company."', '".$weight."', '".$length."', '".$streetAddress."', '".$zipCode."', '".$country."');";
mysql_query ( $query );
?>
Thanks in advance!
first things first. make sure you are echoing out the number to the screen. the code you have written looks correct but it's just a snippet.
add var_dump($number) after you assign your number and see if it's showing the number. if it's not then theres a problem with your sql
the problem is your re-assigning the hidden field id at the bottom of your form
here
echo '<td><textarea name="city">'.$man['city'].'</textarea></td>';
echo '</tr>';
echo '<input name="id" type="hidden" value="'.$id.'" />'; <------- HERE
echo '<input name="statee" type="hidden" value="'.$man['state'].'" />';
echo '<tr>';
echo '<td>Штат</td>';
i'm assuming you never assigned $id to number so, at the top of your form, yours assigning the hidden field id to "$number" but at the bottom of the form your assigning the hidden field id to $id. overwriting the original id field from the top of the form
You are echoing!
echo $number = mysql_escape_string( $_POST['id'] );
Change that with:
$number = mysql_escape_string( $_POST['id'] );
echo $number;
If you wanna echo it.
Hello I want to show a table that have all the values from a mysql table but depending on the values that I will select from a dropdown menu. For example the dropdown list menu have a value named open. I just want to see the rows from the table that have that status. I will need to use Ajax for this?
Here is my code?
$status = $_POST['TipoStatus'];
echo ' Create Service ';
echo '</br>';
echo '</tr><tr><td><label for="TipoStatus"> Status:</label></td><td>';
$query = "SELECT TipoStatus FROM status"; // First Remar
$result = queryMysql($query);
if (!queryMysql($query)) {
echo "Query fail: $query<br />" .
mysql_error() . "<br /><br />";
}
else
{
echo '<select name = "TipoStatus" size = "1">'; // or name="toinsert[]"
// echo '<option value="none" selected="selected">None</option>';
while ($row_1 = mysql_fetch_array($result)) {
echo '<option value="' . htmlspecialchars($row_1['TipoStatus']) . ' selected="$row_1[9]" >' // Third remark
. htmlspecialchars($row_1['TipoStatus'])
. '</option>';
}
echo '</select>';
echo '</p>';
}
echo '<table border="1" >';
echo '<tr>';
echo '</br>';
echo '<th> Service ID</th>';
echo '<th>Title</th>';
echo '<th>Description</th>';
echo '<th>Notes</th>';
echo '<th>Submit By</th>';
echo '<th>Assigned Employee</th>';
echo '<th>Assigned Group</th>';
echo '<th>Category</th>';
echo '<th>Status</th>';
echo '<th>Urgency</th>';
echo '<th>Customer</th>';
echo '<th>Day Created</th>';
echo '</tr>';
$query = ("SELECT ServiceID, Title, Description, Notes, SubmitBy, AssignedEmp, " .
"AssignedGroup, NameCategory, TipoStatus, TiposUrgencia, CustomerName, DayCreation FROM Service where TipoStatus = '$status' ");
$result = queryMysql($query);
echo 'resultado' . mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td> '.$row['ServiceID'] .' </td>';
echo '<td>' .$row['Title']. ' </td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Notes'].'</td>';
echo '<td>'.$row['SubmitBy'].'</td>';
echo '<td>'.$row['AssignedEmp'].'</td>';
echo '<td>'.$row['AssignedGroup'].'</td>';
echo '<td>'.$row['NameCategory'].'</td>';
echo '<td>'.$row['TipoStatus'].'</td>';
echo '<td>'.$row['TiposUrgencia'].'</td>';
echo '<td>'.$row['CustomerName'].'</td>';
echo '<td>'.$row['DayCreation'].'</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo $ticket_select;`enter code here`
echo '</table>';
echo '<form method = "post" action "rnseetickets.php">';
?>
Your code is a little meandering, and I can't quite tell if you're running this from the command line or as a web page, but it seems like you're wanting to filter a SQL result with a drop-down of options? That can be done with a simple form (if you don't mind a page refresh) or with AJAX (if you want it to filter without a page refresh):
// Run SQL
$sql = "SELECT TipoStatus FROM status";
if (!empty($_GET['status'])) {
$sql .= "WHERE `value`=\"".addslashes($_GET['status'])."\"";
}
$result = queryMysql($sql);
// Filter form
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method="get">";
echo "<select name=\"status\"><option>open</option><option>other option</option></select>";
echo "<input type=\"submit\" value=\"Filter\">";
echo "</form>";
// Table
// As you already have it
Greetings. You can submit a forum on the onchange event of the dropdown box.
Check this link:
javascript onchange submit
You can find more examples by searching 'javascript onchange submit'
Now when the user selects something in the drop down box the form will submit and you can use your php code do filter the results.