php echo only in a specific order - php

i have a problem:
if(isset($_POST['send'])){
$id = mysql_real_escape_string($_POST['id']);
$query = mysql_query("select * from somewhere where id='$id'");
$row = mysql_fetch_array( $query );
if(!mysql_num_rows($query)==1){
echo('error');
}
}
After this i have this echo from db:
<input type="text" name="up_name" value="<?php echo $row['name'];?>" id="up_name"/>
and this echo:
<select>
<?php
$up_id=$_POST['up_id'];
$sqlDateUser=mysql_query("SELECT `something` from `somewhere` where id='".$id."'");
$res=mysql_fetch_assoc($sqlDateUser);
$somethig_selected=$res['something'];
$something=mysql_query("SELECT `den` FROM `jud`");
while($row = mysql_fetch_row( $something)){
$selected=($row[0]==$somethig_selected)?'selected':'';
echo "<option value='".$row[0]."' ".$selected.">".$row[0]."</option>";
}
?>
</select>
In this order all it is ok but if i change it, first echo doesn't work. I need to display and others rows after these and i don't know what is the problem with the second echo. Can someone tell me what is the problem?

It may be a naming issue. For example, you have your second query assigning your values for mysql_fetch_row into a variable named $row.
That also is the same name you've given your first query, where you are assigning your mysql_fetch_array.
I would probably try naming them something different like $row_get_user_info and $row_get_den to differentiate them. Also it would make it easier if, when you moved your first echo line, that you also move the query that goes along with it. (Keep them together.)

Related

How to make do-while loop works for its not looping at all?

I have these codes. I chose the do-while loop, so I can do the looping, but somehow it doesn't work.
So, in my database, there are at least 5 rows from the "SOALTXT" field, that have id=S01, which mean, tht it should do looping for 5 times. But somehow it only shows one row.
Like this:
not expected output:
<?php
include "koneksi.php";
$query = mysqli_query($connection,"SELECT * FROM buatsoal_db ORDER BY ID DESC");
$result = mysqli_fetch_array($query);
$id = $result['ID'];
?>
<?php
do{ ?>
<form method= 'post'>
<input name='next' type='submit' id='next' value='next'>
</form>
<?php if(isset($_POST['next'])){
> $row = mysqli_fetch_array ($query);
echo $row['SOALTXT'];
?>
<br>
<br>
<?php
}
?>
<?php }
while ($id =='S01');
?>
For the output, I expect 5 'next ' button, and when it clicked, show up each one 'soaltxt'.
The variable $id is not being updated inside the do while loop. Just add another
$id = $row['ID']
before the echo in the loop.
Edit:
I was going to post a new snippet but the answer from #Omar Abbas is along the lines of what I intended. I would get the form outside of the loop and add a hidden input to pass an index to the next page so you know how many times you pressed next so you know how many rows to display.
Learning to ask the right questions is important in this industry. Important parts of it are good phrasing and as few assumptions as possible.
Answer to your question you are using mysqli_fetch_array() find details about mysqli_fetch_array now you also need to check on which index your id resides in the $row like $row[0] or whatever that index might be, and assign this value to the $id variable like $id = $row[0] and then put that into while($id = 'S01')
possible solution, only fetch the records that have id = S01, use mysqli_fetch_assoc() and use While loop, find details mysqli_fetch_assoc.
$query = mysqli_query($connection,"SELECT * FROM buatsoal_db WHERE ID=S01");
$result = mysqli_fetch_array($query);
while ($row=mysqli_fetch_assoc($result)){
echo $row['ID'];
}

Taking a value from an array and storing it into a variable for a SQL search

Perhaps there may be an easier way to do this however, I need the project to select a patient from the drop down menu. Then when the dropdown menu has got a value, the text field needs to take the NHS number from that drop down menu (array) so that it can be posted elsewhere.
<select name="patient" class="textbox" required>
<option value="">Select a patient</option>
<?php
include 'dbconnection.php';
$sql = "SELECT * FROM clients ORDER by firstname ASC";
$result = mysqli_query($conn, $sql);
$result = $conn-> query($sql);
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row["firstname"]." ".$row["lastname"]; ?>">
<?php echo $row["firstname"]." ".$row["lastname"] .", ".$row["addressl1"].", ".$row["addressl2"].", ".$row["county"].", ".$row["postcode"].", ".$row["nhsnum"]; ?></option>
<?php
$nhs = $row['nhsnum'];
}
?>
</select>
<?php
$sql = "SELECT * FROM clients WHERE nhsnum = $nhs ";
$result = mysqli_query($conn, $sql);
$result = $conn-> query($sql);
while($row=mysqli_fetch_array($result))
{
?>
<input type="text" placeholder="NHS number" readonly value=" <?php echo $row["nhsnum"]; ?>">
<?php
}
?>
As you may see, I have created dummy variables of $nhs however its a static variable and doesnt change upon user selection from the drop down list. Can anyone explain how I can merge the two together.
DB setup
i think you should declare the $nhs outside the while loop
Use AJAX, as already suggested, or a form submit button. Your second query should be where your AJAX or submitted form goes. Use $_GET or $_POST, if you are using get or post method, to intercept the option value. Assign that value to your $nhs, then use as you have.
Set the option value to the $nhs value you want, not the person’s name. Example using $_POST
if(isset($_POST['patient'])){
$nhs=$_POST['patient'];
}else{
// whatever code you want to handle a non-submitted value.
}
Add additional code to prevent SQL injection.

Php if and else

I'm trying to make this code work but I don't know why it won't. Basically I want it to display a name if the nickname column in the database is null. And if it's not null it should display the nickname. Also I'm somewhat noob so keep that in mind when responding.
$namn = mysql_query("SELECT name FROM Horseinfo WHERE name = '$somevariable'");
$nicknamn = mysql_query("SELECT nickname FROM Horseinfo WHERE name = '$somevariable'");
<? $row = mysql_fetch_array($nicknamn,$namn);
if(is_null($nicknamn)) {?>
<div style='font-size:18px; padding-bottom:3px; margin-top:0px;'>records for <? echo $row['name'];?></div>
<?} else {?>
<div style='font-size:18px; padding-bottom:3px; margin-top:0px;'>records for <? echo $row['nickname'];?></div>
<?}?>
Based on assumption I would say that your nickname column does not contain a database NULL value, rather it would be an empty string instead (this totally depends on the routine filling the Horseinfo table). You also only need one SQL query to fetch both name and nickname.
My suggestion would be to use empty() instead:
// try to use mysqli_* instead of mysql_* functions, mysqli_query() expects parameter 1 to be a database connection resource
$res = mysqli_query($connection, "SELECT name, nickname FROM Horseinfo WHERE name = '$somevariable'");
if ($res && mysqli_num_rows($res)>0) {
$row = mysqli_fetch_row($res);
$horseName= empty($row['nickname']) ? $row['name'] : $row['nickname'];
?>
<div style='font-size:18px; padding-bottom:3px; margin-top:0px;'>records for <? echo horseName;?></div>
<?php } ?>

Exporting information from MYSQL to Checkboxes

I am having issues getting information out of mysql into multiple checkboxes.
The query im using is this.
<?php
$usergroupid = $_SESSION['UserGroupID'];
$sql="SELECT * FROM sites WHERE UserGroupID='{$usergroupid}' ORDER BY sites.Description";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
$description=$row["sites.Description"];
{
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $description; ?>">
<?php
}
?>
but this only inputs 1 checkbox and has no text after it when there are multiple rows in the table.
Well for starters, never use the same id twice in HTML (you go through a for loop and make each element have the same id, not a good thing). Fix that issue first (make the HTML input element's id include some kind of id from the row)
Then, the real problem comes from the fact that you put the
$description=$row["sites.Description"];
line before your opening brace for the while statement. It should be
while ($row=mysql_fetch_array($result))
{
$description=$row["sites.Description"];
instead.
I would change the code to:
<?php
$usergroupid = $_SESSION['UserGroupID'];
$sql="SELECT * FROM sites WHERE UserGroupID='{$usergroupid}' ORDER BY sites.Description";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
$description=$row["sites.Description"];
?>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $description; ?>">
<?php
}
?>
The problem was that the "{" should've been written directly after "while ($row=mysql_fetch_array($result))".
Also, I would strongly recommend stop using mysql_query if possible, since mysql_query is now deprecated (read more about it here: http://php.net/manual/en/function.mysql-query.php).
It's not the only issue your code has. For example the formatting, I mean you can't really read it. Learn to properly format your code, sort it up. That will actually help you to prevent other errors.
And as commented, it will also help you to read and understand your code.
And now as the third tip: If you ask a question with properly formatted code you will also get better answers here on the website. So please keep your issues important and do all the best you can do to get help here on site.
<?php
$usergroupid = $_SESSION['UserGroupID'];
$sql = sprintf(
"SELECT * FROM sites WHERE UserGroupID = %d ORDER BY sites.Description",
(int)$usergroupid
);
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$description = $row["sites.Description"];
echo '<input name="checkbox[]" type="checkbox" id="checkbox[]" value="',
$description, '">';
}
You should use mysqli extension instead mysql(is deprecated)
$sql="SELECT * FROM sites WHERE UserGroupID='{$usergroupid}' ORDER BY sites.Description";
$i=0;
$result=mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result)){
$description=$row["sites.Description"];
$xxx= "<input name='checkbox[]' type='checkbox' id='checkbox_$i' value='$description'>";
$i++;
}
echo $xxx;
?>
$description have to be inside the while.
id="checkbox_$i Add autoincrement to make diferent the ids

No result from options selected from a database column (php-mysql)

In the PHP code below i want to select all the column values of a table and make them options of a select form. The result is that i dont get any options at all. Could someone help? Thanks
<?php
// ....
$userid=$_SESSION['userid'];
echo "<select>";
$sql = "SELECT * FROM users where userid='".$userid."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<option>" .$row['company']. "</option>";
}
echo "</select>";
mysqli_close();
?>
It may just be a typo but you have mixed mysql and mysqli functions, you have stated mysql_query and mysql_fetch_array, whereas you have closed it with mysqli_close, could be an issue depending on what you wrote above this code or indeed if this is not just a typo.
Other things to try would be try the query in your mysql client / phpmyadmin and see if it comes up with any results or errors.

Categories