When I am trying to list all the employee names from the database to assign a job for a particular person in the dropdown box. The Output value only show the initial or the First name of Employee. The other names of the employee (Last names) are not posted in the php.
Actually the names after first spaces are missing.
Here is my code...
<pre>
<tr>
<td><label>Assigning To</label></td><td><label>:</label></td>
<td>
<?php
$result = mysqli_query($conn, "SELECT * FROM userdetails WHERE UserGroup='DigitizationArtist' || UserGroup='DigitizationArtistQC' || UserGroup='GraphicArtist' || UserGroup='GraphicArtistQC')");
echo "<select name='ArtistName' value=''>";
echo "<option value=''></option>";
while($r = mysqli_fetch_array($result))
{
echo "<option value=" .$r['Name'] .">".$r['Name']. "</option>";
}
echo "</select>";
?>
</td>
</tr>
<pre>
This is the getting value php code...
$ArtistName = mysqli_real_escape_string($conn, $_POST['ArtistName']);
I am getting the out for "S Gowri Shankar" is "S"
I am getting the out for "Selva Kumar" is "Selva"
Please advise. What is wrong in these coding? Else the php always ignore the space after text in checkbox values.
You value attributes has missing closing quotes:
echo "<option value=" .$r['Name'] .">".$r['Name']. "</option>";
Should be:
echo "<option value='" .$r['Name'] ."'>".$r['Name']. "</option>";
^ ^
And also use htmlspecialchars for those values with quotations so that they wont mess up the closing on the attributes:
$ArtistName = mysqli_real_escape_string($conn, $_POST['ArtistName']);
$ArtistName = htmlspecialchars($ArtistName, ENT_QUOTES);
Related
<center><?php
include "config.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT Product_Name, id FROM stockcount";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
?>
<select id = 'ProductName' name='productName' onchange="" >Product_Name </option>"
<?php
foreach ($conn->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[Product_Name]>$row[Product_Name
]</option>";
/* Option values are added by looping through the array */
}
?>
</select>
</center><p></P>
This code shows all the product names from the stockcount table.
<?PHP
include ('config.php');
$date = $_POST['date'];
$productname = $_POST['productName'];
$cartons = $_POST['cartons'];
$send_to = $_POST['send_To'];
$sql = "INSERT INTO storerequest (Date, Product_Name, Cartons, Send_To) VALUES ('$date','$productname', '$cartons', '$send_to')";
if ($conn->query($sql)===TRUE) {
header("Location:orderrequest.php");
}
else {
echo "Error is " .$sql."<br>". $conn->error;
}
$conn->close();
?>
This adds the selection from the dropdown to the storerequest table.
Now the problem is if i select an option named "Motor Car" from the down. It inserts in the storerequest table as "Motor" only. When i changed the product name to "Motor-Car" then it inserts as "Motor-Car". It basically stores only the 1st word. Please experts help me solve this problem
You forgot the " signs for the value:
echo '<option value="'.$row[Product_Name].'">'.$row[Product_Name].'</option>"';
need to change echo statement.
Change From
echo "<option value=$row[Product_Name]>$row[Product_Name]</option>";
To
echo "<option value='$row[Product_Name]'>$row[Product_Name]</option>";
Your problem is that you are not wrapping the value of your value attribute in either single or double quotes.
i.e. value=$row[Product_Name]
echo "<option value=$row[Product_Name]>$row[Product_Name]</option>";
You dont have to wrap the attributes value in quotes, BUT YOU DO if the value has a space in it like Motor Car
So you must do
echo "<option value='$row[Product_Name]'>$row[Product_Name]</option>";
Or
echo '<option value="' . $row['Product_Name'] . '">' . $row['Product_Name'] . '</option>';
Yes, or if you need the double quotes for html just escape them :
`echo "$row[Product_Name]";
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 would like to remain my drop down value which I select for submitting after posting the form. My form posts to the same page.
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
echo "Country: <select name='country' value=''>";
while($r = mysql_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id.">".$cname."</option>";
}
echo "</select>"; ?>
Remove your current echo inside the loop and replace it with the following:
if($_POST["country"]==$id)
echo "<option value='".$id."' selected='selected'>".$cname."</option>";
else
echo "<option value='".$id."' >".$cname."</option>";
This will check if the current option being displayed is the one that was submitted and it will select it in that case.
If I understand what you are looking for correctly you need to use the $_POST value of your select to set the selected item...
$query = "SELECT countryName,countryCode FROM tcf_countries";
$result = mysql_query ($query);
$country = '';
echo "Country: <select name='country'>";
while($r = mysqli_fetch_array($result)) {
$id = $r['countryCode'];
$cname = $r['countryName'];
echo "<option value=".$id;
echo ($_POST["country"]==$id) ? ' selected="SELECTED"' : '';
echo ">".$cname."</option>";
}
echo "</select>"; ?>
Setting selected="SELECTED" for the $id that matches $_POST['country'] will make it the selected item in your dropdown.
And, get rid of mysql* functions and use mysqli* functions instead...
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']."";
I have the following code:
<?php
$a= 11;
echo "<select name='rabboSelect' style='width:300px;'>";
$sqlQuery="SELECT * FROM writers";
$result=sql($sqlQuery);
while($row = mysql_fetch_array($result))
{
$a .= "<option value='" .$row["ID"]."'>" . $row["name"] . "<option>";
}
echo str_replace("<option></option>", "", $a);;
echo "</select>";
?>
and in the html it's adding <option></option> after each one, even if I try to delete it:
<select name="rabboSelect" style="width:300px;">11<option value="2">הרב מילר</option><option></option><option value="3">משה דוויד</option><option></option><option value="4">קלמי גריינמן</option><option></option><option value="5">בנימין יעבץ</option><option></option><option value="8">אליהו פרץ</option><option></option></select>
How can I stop this from happening?
Firstly, I wonder why you are setting $a=11; - did you maybe mean $a='';?
Next, you are missing a / in the closing option tag. If you tried View Source rather than viewing the DOM, you'd see that rather than the extra options.