PHP Problems

I Have some issues, Can’t get the result $rn from this.

echo ("    <td width=231 colspan=2 height=7 colspan=2><font size=2>Ras");
echo ("      <select size=1 name=H_rasid>");
echo ("        <option>Välj ras</option>");
$retval = mysqli_query($con, "SELECT * FROM ras WHERE $ras=$U_rasval ORDER BY Ras_namn");

if(mysqli_num_rows($retval) > 0){  
 while($row = mysqli_fetch_assoc($retval)){  
$rg=$row['Ras_grp'];
$rn=$row['Ras_namn'];
$ri=$row['Ras_id'];
    echo("<option value=$ri>");
    echo("$rn");
    echo("</option>");
 } //end of while  
}else{  
echo "0 results";  
}  
 echo ("      </select>");
  echo ("</td>");

@tonyh when you post code, you need to format it for the forum. Just place 3 backticks ``` on a separate line before the code and again after the code.

I’ve done it for you this time.

I assume you mean this.

Does the table ras contain a column named Ras_namn? Not RAS_namn, not ras_namn, but Ras_namn?

(You sure you havent misspelt ‘name’?)

What debugging have you done so far? Presumably you’ve checked that $ras contains a valid column name? Do you get any error messages?

It’s not really ideal to echo "0 results"; inside your html <select>.

Define:

What are you getting and what do you expect?

Some points that will simplify what you are doing -

  1. don’t echo static markup. Just drop out of php and put the static markup in line.
  2. echo isn’t a function. leave out the ()
  3. the first option/prompt must have an empty string for a value so that you can easily validate it (this would also be required if you make the field ‘required’ in the browser)
  4. you should list out the columns you are SELECTing in a query and only list the columns you are using.
  5. don’t dynamically chose a column in a query, assuming that is what you are doing with the $ras variable, without fully and completely validating that the chosen column is a permitted value.
  6. don’t put external, dynamic values directly into queries, where any sql special character can break the sql query syntax, which is how sql injection is accomplished. use a prepared query instead. if using prepared queries with the mysqli extension seems overly complicated and inconsistent, it is. this would be a good time to switch to the much simpler and more modern PDO extension.
  7. don’t copy variables to other variables for nothing. just use the original variables that data is in.
  8. don’t use three echo statements where one will accomplish the task.
  9. you should always surround attribute values with quotes, to prevent any ‘stop’ character in a value from being able to break the html markup.
  10. you should apply htmlentities() to any dynamic value you output in a html context, to prevent any html entity in a value from being able to break the html markup, which is how cross site scripting is accomplished.

The script is a PHP form for dog owners to make an entry for dogshows
I cleaned the script up a little bit but i can’t the the selected value to follow to the next step / script (the welcome page). I want the selected $rn (breed name), $ri (breed id) and the $rg ( breed group) to follow to the next step but there they are blanc.

echo ("    <td width=231 colspan=2 height=7 colspan=2><font size=2>Ras");
echo ("      <select size=1 name=H_rasid>");
echo ("        <option>Välj ras</option>");
$retval = mysqli_query($con, "SELECT * FROM ras WHERE $ras=$U_rasval ORDER BY Ras_namn");

while($row = mysqli_fetch_assoc($retval)){  
  $rg=$row['Ras_grp'];
  $rn=$row['Ras_namn'];
  $ri=$row['Ras_id'];
    echo("<option value=$ri>");
    echo("$rn");
    echo("</option>");
}

echo ("      </select>");
echo ("</td>");

Because you haven’t posted the complete code for the form page or the form processing code, we cannot directly help with the problem. There could be a dozen different reasons why what you are doing doesn’t work. We need to see all the relevant code in order to narrow down the possibilities.

Also, please translate and read the first reply in this thread by @Gandalf on how to post code on the forum.

Yes, please do. :slight_smile:

I’ve formatted the code in your last post for you, but please format it yourself in future, or it won’t show correctly (or at all).

Do all of your variables - $ras, $U_rasval mainly - contain the values that you expect them to? If you execute the query in phpmyadmin using those same values, does the query work?