I have changed my code and now I have a new error:
Parse error: syntax error, unexpected '<' in
/webroot/c/o/..../...../www/a/drop_down_car_test.php on line 19
here is the code:
<?php
//////connecting to our sql server
$db_connect=mysql_connect("xxxx", "xxxxxxxxx", "xxxxxxxxxx") or die("not logged in");
//////now selecting our database
$db_select=mysql_select_db("coden003_car") or die(mysql_error());
////this query is used the first time the page loads
$query = mysql_query("SELECT * FROM car ");
echo '<form action="drop_down_car_test.php?" method="GET">';
echo '<table border = \"1\">';
echo '<tr><td>id</td><td>car</td><td>name</td>';
while($row=mysql_fetch_array($query)){
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo "<select name='carDropDown." . $row['id'] . "' >";
<option value="1">ford</option>; <---------line 19
<option value="2">toyota</option>;
<option value="3">gmc</option>;
</select>";
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
}////end while
echo"<table>";
echo '<td bgcolor=#7FFF00><input type="submit" value="Update"></td>';
echo "</form>";
?>
im use to C++ usage of quotes and PHP confuses me.
In your code:
echo '<select name="carDropDown.$row['id']">
You need to make it:
echo "<select name=\"carDropDown.{$row['id']}\">
You were not escaping the ', but also you can't use a variable inside single quotes, but you can use one inside double quotes (array calls must be wrapped in { and }).
And of course the closing ' at the end of that string needs to be changed to "
And you're missing a semicolon at the end of that string.
And you didn't close the block (you forgot a closing }).
Try these:
echo "<select name='carDropDown." . $row['id'] . "' >";
OR
echo "<select name=carDropDown.$row['id'] >";
In PHP you can CONCAT strings like this:
echo "hello" . "world" . $yourVariable . " Something else ";
http://php.net/manual/en/internals2.opcodes.concat.php
Another way to do the <select> name, which will make it a little easier to read on the $_GET[] is to make carDropDown an array with the $row['id'] as the array key.
echo '<select name="carDropDown['.$row['id'].']">
Now on the php side you can do -
foreach ($row['id'] as $id){
$carName=$_GET['carDropDown'][$id]];
echo $carName;}
Related
In this code i am trying to fetch city names into the html dropdown. Kindly corrct me if i am wrong anywhere, it give me an error
<?php
$query = Run("select city_name from City");
echo "<select name="city-name" style="width: 210px;">";
while ($row = mssql_num_rows($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
?>
use mysqli_fetch_array($query) insted of mssql_num_rows($query)
try this one
echo "<select>";
while ($row =mysqli_fetch_array($query))
{
echo "<option value='".$row['city_name']."'>".$row['city_name']."</option>";
}
echo "</select>";
mssql_num_rows returns the number of rows in the result set, it doesn't iterate and return individual rows. Try using mssql_fetch_object instead.
If you start a string with " then you have to escape all occurrences of " inside your string or use '.
For example:
echo "<select name=\"city-name\" style=\"width: 210px;\">";
or
echo '<select name="city-name" style="width: 210px;">';
so you don't accidently close the string.
Also like the others pointed out, you have to use
mysqli_fetch_array($query).
Use this
$query = Run("select city_name from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_fetch_object($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
Use This
$query = Run("select `city_name` from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_num_rows($query))
{
echo "<option value='.$row->city_name.'>".$row->city_name."</option>";
}
echo "</select>";
I've set up a php form that registers a project to our database, it has a drop down that populates from our customer/supplier databases.
I've also set up a function to edit these projects, the problem I have is that when I go to my edit page it just displays the customer/supplier name and not in the drop down but a value box - is there a way to have the edit page display the dropdown but also be selected on the original supplier/customer?
Register project page
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo '<select name='client'>';
while($row = mysql_fetch_assoc($result))
{ `
echo '<option value = ''.$row[name].''>'.$row[name].'</option>';
}`
echo '</select>';
?>
Edit page
<input type='text' name='client' value='<?php echo $client; ?>'/>
I tried a few tutorials and code tweaks but kept getting errors. I am aware of my sql injection problem, at the moment this site is internal.
Any help would be appreciated.
thanks
instead of $row[name] you should use $row['name']
$client= "<select name='client'>"; // you had error here also.
while($row = mysql_fetch_assoc($result))
{
$client.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
$client.= '</select>';
now echo $client to get dropdown.no need of constructing separate select tag now.
for selected use like this:
$client1= "<select name='client'>";
while($row = mysql_fetch_assoc($result))
{
if($row['name'] == $clientValue){
$client.= "<option selected='selected' value = '".$row['name']."'>'".$row['name'].'</option>';
}else{
$client1.= "<option value = '".$row['name']."'>'".$row['name'].'</option>';
}
}
$client1.= '</select>';
on echo of $client1 you will get selected based on the value $clientValue which you have to pass.
On your edit page:
<?php
$result = mysql_query('SELECT name FROM customers ORDER BY name ASC');
echo "<select name=\"customer\">";
while($row = mysql_fetch_assoc($result))
{
if ($row['name'] == $client)
{
echo "<option selected value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
else
{
echo "<option value=\"" . $row['name'] . "\">" . $row['name'] . "</option>";
}
}
echo "</select>";
?>
I also suggest that you change the old extension for mysql. I can't see no SQL Injection problem for now, but you should take care of it even if it is internal, because, from different reasons you will forget to sanitize it later. If you are writing it, then write it correctly.
Now for the problem, you are not using the quotes correctly, hence the errors. Do not use the same type of quotes, but change them, like so:
echo '<select name="client">';
Or if you use double quotes for concatenation, use single inside.
In case you have to use the same, escape them with \
For starters, you have a syntax error here:
echo '<select name='client'>';
(There are probably more quoting errors throughout the code, but I digress...)
As for using a drop-down, what you're looking for is the selected attribute. When you're building the page elements to display the form on the "edit" page, presumably you have the values that you're looking to display. When your loop finds an element which matches the value, select it:
while($row = mysql_fetch_assoc($result))
{
if ($knownValue == $row[name]) {
echo '<option selected value = ''.$row["name"].''>'.$row["name"].'</option>';
} else {
echo '<option value = ''.$row["name"].''>'.$row["name"].'</option>';
}
}
I am writing the programming step by step. At last I met the php code for the result display from the sql query.
I test the echo $sql and that works well.
and I test the rest. This is my php code as like below
$sql = "SELECT code_co.code, code_co.disease_co, code_$lang2.disease_$lang2
FROM code_co LEFT JOIN code_$lang2 ON code_$lang2.code = code_co.code
WHERE code_co.code = '".$code."'";
$result = mysqli_query($con,$sql);
echo "<table border='1' style='background:#dddddd;border-color:green;'>";
echo "<tr>";
echo "<th >Code</th>";
echo "<th >병명</th>";
echo "<th >Disease name</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td >" . $row['code'] . "</td>";
echo "<td >" .$row['disease_co']."</td>";
echo "<td>".$row['disease_."$lang2"']."</td>";
echo "</tr>";
}
echo "</table>";
$row['code'] and $row['disease_co'] are good output. But $row['disease_."$lang2"'] is not.
How should I write that variable correctly?
Please, help me.
Just concat it:
echo "<td>".$row['disease_'.$lang2]."</td>";
or
echo "<td>".$row["disease_$lang2"]."</td>";
use it
echo "<td>".$row['disease_'.$lang2]."</td>";
You can use following code:
$row['disease_'.$lang2]; // (.) dot concatenates variable with disease_.
You can do this, which is just a minor edit to your current code:
//Assuming that the variable name is $lang2
$sql = "SELECT code_co.code, code_co.disease_co, code_{$lang2}.disease_{$lang2} FROM code_co LEFT JOIN code_{$lang2} ON code_{$lang2}.code = code_co.code WHERE code_co.code = '".$code."'";
PHP allows you to expand variables inside double-quotes using the {} braces
Pls what am I doing wrong?
I have an array ($result) having some keys and values in it obtained via doctrine_mongodb and I am trying to display the results in a table using php in different cells.
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
**echo "<td>"$docrow->getName()"</td>";**
echo "<td>1</td>";
echo "<td>2</td>";
Seems like the issue is with the "" (exclamation marks) when I use . Error message i get is:
Parse: syntax error, unexpected '$docrow' (T_VARIABLE), expecting ',' or ';'...
Use dot(.) to concat echo try this
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
echo "<td>".$docrow->getName()."</td>";
echo "<td>1</td>";
echo "<td>2</td>";
Try this, You have missed to add . concat the variable between the strings.
echo "<td>".$docrow->getName()."</td>";
.....^
instead of
echo "<td>"$docrow->getName()"</td>";
Now your code should be,
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
echo "<td>".$docrow->getName()."</td>";
echo "<td>1</td>";
echo "<td>2</td>";
}
echo "<td>$docrow->getName()</td>";
You don't need to unquote to use a variable if using double quotes.
If you left your code the way it was, you would either need to a) put a comma after the "" and one after $docrow->getName(), or b) concatenate the variable into the string.
1. You missed dot before and after $docrow->getName()
Try :
echo "<td>".$docrow->getName()."</td>";
Or :
echo "<td>{$docrow->getName()}</td>";
Read this for more info : String operators
2 . And you missed a bracket after your loop :
foreach ($result as $docrow)
{
echo "<tr height=\"20\" class=\"evenListRowS1\">";
echo "<td>"$docrow->getName()"</td>";
echo "<td>1</td>";
echo "<td>2</td>";
} // <-- you missed this
I have a variable like "Doctor and Surgical" I take this value from Mysql database and store it in a variable. When I echo this variable it gives me only "Doctor"
Can anyone tell me how can I take the above value retrieved from Mysql as an entire variable...
EDIT
Please find the code below
query.php
<form action="process.php" method="post">
<select name="cat">
<?php
$sql="select distinct Category from tbl_1 order by Category asc";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
echo "<option value=".$row[Category].">".$row[Category]."</option>";
}
?>
</select>
<select name="station">
<?php
$sql_1="select distinct Station from tbl_1 order by Station asc";
$query_1=mysql_query($sql_1);
while($row=mysql_fetch_array($query_1))
{
echo "<option value=".$row[Station].">".$row[Station]."</option>";
}
?>
</select>
<input name="C" type="submit" />
</form>
process.php
$myValue =$_POST['cat'];
$myStation=$_POST['station'];
echo $myValue;
echo "<br/>";
echo $myStation;
$mySqlStm ="SELECT Name FROM tbl_1 WHERE Category='$myValue' and Station='$myStation'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()");
if(mysql_num_rows($result2) == 0){
echo("<br/>no records found");
}
ELSE
{
echo "<table border='1'>";
//ECHO THE RECORDS FETCHED
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
}}
Here when I echo $myValue it gives me "Doctor" instead of "Doctor and Surgeon"
I need "Doctor and Surgeon as an entire variable.
You need to quote the value properly:
echo "<option value='".$row[Category]."'>".$row[Category]."</option>";
With your current code, if you look at the source you will probably see something like this:
<option value=Some Category>Some Category</option>
But with proper quotation you'll see the correct value.
You can use this without having to concatenate:
echo "<option value='{$row[Category]}'>{$row[Category]}</option>";
Think of the quotes just like you would a bracket or an html tag. When you open one, you are inside until you close it. In this case, you needs quotes to tell php what to echo out and different quotes that will print for html attributes. It's usually easier to use single quotes for the php echo commands and double quotes for the html code. Then the . just means to stitch a bunch of stuff together without having to put the echo command ten times in a row. So when you have this:
echo '<option value="'.$row[Category].'">'.$row[Category].'</option>';
you can break it down like this (just for your mental clarity, don't actually code this):
echo
'<option value=" '
.
$row[Category]
.
' "> '
.
$row[Category]
.
'</option>'
;
Use single quotes & Just insure the value of $row['Category']
Try it:
echo "".$row['Category']."";