2 MySQLi Questions - php

I have a couple of mysqli questions.
First. I know I can make groups into an array. I was just wondering if there was a way to get a row from the groups object without making it into an array?
$groups = $this->db->query("SELECT id, name FROM groups");
...
<select name="group">
<?php while($group = $groups->fetch_object()): ?>
<option value="<?php echo $group->id?>"><?php echo $group->name; ?></option>
<?php endwhile; ?>
</select>
...
<?php echo $groups[$user->group_id]; ?>
I know the final line won't work. Is there something like this $groups->fetch_row($group_id)->name?
My second question is to do with garbage collection. How much of a difference in a small application does it make if I free up a result as opposed to not? Instead of freeing up the result after each query could I close the database connection when the database class destructs. Would this have the same effect?

Use fetch_assoc():
<?php while($group = $groups->fetch_assoc()): ?>
<option value="<?php echo $group['id']; ?>"><?php echo $group['name']; ?></option>
<?php endwhile; ?>

Related

Is there a way to retrieve a value that has already been set on a dropdown select option?

I have a create.php which creates a user, and edit.php for editing information of the student.
I'm a beginner in PHP so if this might be a question that has a very easy method, please do tell.
I've been searching for retrieving a value that has already been set on a dropdown, but the only things I've found are:
a.) They only print the selected value, not retrieve the already-set option.
sample code:
<p>Schedule</p>
<select value="<?= $data->schedule; ?>" name = "schedule">
<?php
$schedule = array ('Morning', 'Afternoon');
foreach ($schedule as $sched) { ?>
<option value="<?php echo $sched; ?>"><?php echo $sched; ?></option>
</select>
<?php } ?>
b.) I have no knowledge of javascript, I tried it, but as I have no knowledge of it, I can't seem to make it work.
sample codes I tried:
<p>Schedule</p>
<select id = "my_select" name = "schedule">
<option id = "o1" id="id1">Morning</option>
<option id = "o2" id="id2">Afternoon</option>
</select>
<script type="text/javascript">
$("#my_select").change(function() {
var id = $(this).find('option:selected').attr('id');
});
</script>
I also tried
document.getElementById('schedule').selectedOptions[0].text
but still does not output "afternoon"
This is my last resort as I have no other resource to find.
I added a picture of what I wanted to do since my question might be a little confusing.
Selects use selected attribute not the value to define the selected state.
So in your code check the value and then apply selected.
<p>Schedule</p>
<select name="schedule">
<?php foreach (['Morning', 'Afternoon'] as $sched): ?>
<option value="<?= $sched ?>"<?= ($data->schedule == $sched ? ' selected' : null) ?>><?= $sched ?></option>
<?php endforeach ?>
</select>

In PHP i'm not getting desire id it's returning me as 0

here is my piece of code
i'm encountering a problem,,, below are my subject and test table in database. when i select a subject from dropdown menu on the" ADDTEST.php" page and want to add in Test table it couldn't get sub_id from "Subject Table " and it take "Sub_id" as 0 ...
'ADDTEST.PHP'
<td>Select Subject</td>
<td><select name="sub_id">
<?php
include ("database.php");
$rs=mysql_query("Select * from mst_subject");
while($row=mysql_fetch_array($rs))
{
$sub_id=$row['sub_id'];
?>
<option> <? echo $row['sub_name']; ?></option>
<?
}
?> </select>
<?php
include ("database.php");
if($_POST[submit]=='Save' || strlen($_POST['sub_id'])>0 )
{
$testname=$_POST['test_name'];
$totque=$_POST['totque'];
$sub_id=$_POST['sub_id'];
mysql_query("insert into mst_test(sub_id,test_name,total_que) values
('$sub_id','$testname','$totque')") or die(mysql_error());
echo "<p align=center>Test <b>\"$testname\"</b> Added Successfully.</p>";
unset($_POST);
}
}
?>
Please replace
<option> <? echo $row['sub_name']; ?></option>
with
<option value="<? echo $sub_id; ?>"> <? echo $row['sub_name']; ?></option>
In $_POST['sub_id'] you get the value attribute(s) of the select -> option tags.
Please consider also moving from mysql_ php functions to mysqli or pdo, if possible since mysql_ is marked in php as deprecated and, even from my own experience, may occur problematic and too limited.
Try this
<option value="<?=$sub_id?>"><?=$row['sub_name']?></option>
Instead of
<option> <? echo $row['sub_name']; ?></option>

Remember selected option within select menu

I've googled a lot and found alternative solutions but in my case things are a bit different.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php endforeach; ?>
</select>
I need to display the selected option after a POST request in between the option tags but since I already have a value for that I cannot find a way to do so. The idea is that I have one form with a couple of select menus. From the first one I select a database. The second one is for selecting a table from the already chosen database and another select menu for the columns. The problem is that I'm sending a new request for both the database and table and the chosen database cannot be remembered (it just 'resets' the select menu and starts from the first value).
Here's the whole code
Right now I need to reselect the database which I've previously chosen in order to display the columns from a table.
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?= $row; ?>"
<?php if ($row == $_POST['database']){echo " selected";}?>>
<?= $row; ?>
</option>
<?php endforeach; ?>
</select>
Wouldn't this work?
$dbms=$_POST['database'];
<select name='database'>
<?php foreach($databases as $row): ?>
<option value="<?php echo $row; ?>"
<?php if ($row == $dbms) echo " selected"; ?>
> <?php echo $row; ?></option>
<?php endforeach; ?>
</select>

Retrieving data instead of data id from mysql

Hi i am working on php and mysql.
I have a form where in i am accessing data from one table and upon selection i am inserting that data in to another table. But my major constraint is the selected data id is being stored instead of the data value.
kindly let me know how to get the data value instead of the data id.
Below is the sample code.
<td>Status:</td>
<td> <select name="status" id="status">
<?php $svar = mysql_query("select * from status");
while($sresult = mysql_fetch_array($svar)){ ?>
<option value="<?php echo $sresult['id']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php echo $sresult['status']; ?></option>
<?php } ?>
</select> </td>
Now i am inserting the data from the above form in to another database using the following query:
$sql="INSERT INTO Createticket (......,status) VALUES(..........,'$status')";
Although you left out quite a chunk of your code which would have helped check 100%, I think I can make an educated guess at what your attempting.
It's simply because you are setting the value of the select box as the id, not the value....
Change this line:
<option value="<?php echo $sresult['id']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php echo $sresult['status']; ?></option>
to:
<option value="<?php echo $sresult['status']; ?>" <?php if($sresult['id']==$row['status']){ echo "selected"; }?> /><?php echo $sresult['status']; ?></option>
That way, when you post the form, you are posting the value of the field 'status', as the status value not the id value of $result.
the value you are assigning to the option field is $sresult['id'] so the same id is stored in the second table to set the status
replace the
value = " <?php echo $sresult['id'] ?> "
with in oprtions filed to
value = " <?php echo $sresult['status'] ?> "

Echoing row name where posted ID, for pre-selected form value

This will hopefully be an easy one, but I'm lacking the skills!
<select name="search_category" id="select1" >
<option value="">By Category</option>
<?php if (!empty($_POST['search_category'])) { ?>
<option value="<?php echo $_POST['search_category']; ?>" selected="selected"><?php echo $_POST['search_category']; ?></option>
<?php }?>
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
The above is one of many select in a search module. It returns a dynamic list of options from a query higher up in my page. My goal is to have the option last searched pre-selected. Everything works as intended, but my problem is minimal really; the value of the posted search category is an ID($row->id. What I am hoping to do is use the associated $row->name for display, but keep the id for value so my search function still works.
In other words, I'm hoping to do something like:
<?php echo $row->name; WHERE ID = $_POST['search_category']
Is there an easy way to do that in the above code, or will I need to add a special query at the top of my page, fetching the individual row name that matches the posted id?
Thanks!
EDIT: To simplify, I already have a query that returns row->id and row->name, which I use in a foreach loop to populate my option values and names. I simply need a way, or a line that I can add to my query to also get the value of the row->name that matches the POSTED id.
i would write a short function which gives this functionality even for others applications.
just like
<?php
function($id, $table) {
select ... etc
}
?>
For security Reasons I would suggest to use Prepared Statements or mysql_escape
Hope i could help
Perhaps
SELCET('id', 'name' FROM yourTable WHERE 'name' = $_POST['ID'])
you mean something like this or would you select the dropdown option
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"
<?php if($row->name == $_POST['search_category']) : ?>
selected="selected"<?php } ?>>
<?php echo $row->name; ?>
</option>
<?php endforeach; ?>

Categories