Trunction of string when using MySQL select - php

I pulling text that is stored in my database using MySQL select so it is displayed in a form so it can be edited.
When the form is set to the string held in the database gets truncated at the first space so "Foo Bar" would be displayed as "Foo". This doesn't happen when using the tag.
I have made sure that the text field is big enough to hold the entire string and the number of charters isn't limited.
In the database the whole word is stored and no truncation is happening. I set the type to varchar(30) which is enough space to store the whole word. I have also tried changing the type to text and I still have the problem.
I can't seam to find a solution anywhere does anyone have an idea of why this may be happening?
<?php echo "<input type=\"text\" name=\"title[" . $row["id"] . "]\" value=" . $row["title"] . ">"; ?>

You should provide a sample of the HTML produced from your php page. Most likely, your value string is not being quoted, e.g. your HTML form looks like:
<input type=text value=Foo Bar>
instead of
<input type=text value="Foo Bar">
This would produce exactly the effect you are seeing
UPDATE: Based on your example in the comment, you are indeed missing the quotes:
Old code with the problem (missing quotes around value attribute's value):
<?php echo "<input type=\"text\" name=\"title[" . $row["id"] . "]\"
value=" . $row["title"] . ">"; ?>
Fixed code:
<?php echo "<input type=\"text\" name=\"title[" . $row["id"] . "]\"
value=\"" . $row["title"] . "\">"; ?>
Note that you already had the name attribute correctly quoted, but value was not.

Without the relevant code, this is just a guess, but are you setting your form input value attributes without using quotes?
For example, are you doing this?
<input type="text" value=<? echo $value; ?> name="formInput" />
Instead of the correct syntax, which is this?
<input type="text" value="<? echo $value; ?>" name="formInput" />

Related

How to echo two strings added together into one column inside the database?

It seems like an odd question but here it goes. How do I add two strings together in order for them to appear as one inside the database ?
<input type="hidden" name="user" value ="<?php echo $articless['name'] . " " . $user_data->id; ?>" />
This does not work. My desired outcome is to add them together so they appear as two words added together in a column without spaces for example: articlebox. Is it the . " " . between two variables that needs adjusting ?
Just remove the " ". You combine string using the dot (.) only.
<input type="hidden" name="user" value ="<?php echo $articless['name'].$user_data->id; ?>" />

multi line text for input

How can I make multi line text input ?
My code outputs only the first word of any input
For example if I type in "Hello world" it will only print "Hello"
My goal is to display multi line text.
My current code is:
echo "<td><input type=text name=opis value=".$row['opis']." </td>";
I know is the correct tag , but i can't incorporate that into my code.
My code outputs only the first word of any input
For example if I type in "Hello world" it will only print "Hello"
Always wrap you attributes values in quotes. Suppose your $row['opis'] has value "some value here", so in rendered html it will be:
<td><input type=text name=opis value=some value here /></td>
So do you think browser will understand that "some value here" is data of value attribute and not a list of some other attributes?
So, use quotes with attributes' values, always:
echo '<td><input type="text" name="opis" value="' . $row['opis'] . '" /></td>';
As you can see I've changed quotes and also closed input tag ( />).
I am not sure about what you mean with a multi line text input, but in HTML that would be the textarea element.
You can use it and specify how much rows do you want it to show initially. It will normally be bigger than an input[type='text'].
Your code would be something like this:
echo '<td><textarea name="opis" rows="5">'.$row['opis'].'</textarea></td>';
Also, if you want to echo something that requires the use of " you can just do:
echo '<td class="test">'.$content.'</td>';
// or in its case the other way around
echo "<td class='test'>".$content."</td>";
Here's an example of the difference between the two elements:
<input type="text" value='Hello
World
!
I
Am
Here'>
<br><br>
<textarea rows="5">Hello
World
!
I
Am
Here</textarea>

UPDATE database and update where variable

I have a question in regards to the GET function. I currently have a form with a get action. I am accessing that data in another page with the get function. Unfortunately, I have been getting an "error: undefined index" on every one of my values. After reading up on similar problems, I tried using isset (as seen below) and it gets rid of the error, but I am not sure that my data is stored in the variables at all because if I echo the 4 variables, nothing shows up. Could someone give me a push in the right direction?
Data from form:
while($row = mysql_fetch_array($result)) {
echo "<div class=\"addform\"><form method='GET' action=\"update.php\">\n";
echo " <input type=\"text\" value=\"".$row['tfid']."\" name=\"tfid\">\n";
echo " <input type=\"text\" name=\"fname\" value=\"".$row['fname']."\"/>\n";
echo " <input type=\"text\" name=\"lname\" value=\"".$row['lname']."\"/>\n";
echo " <input type=\"text\" name=\"hca\" value=\"".$row['hca']."\"/>\n";
echo " <input type=\"text\" name=\"file\" value=\"".$row['file']."\"/>\n";
echo " <input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n";
echo "<img title='Delete Row' alt=\"Delete\" class='del' src='images/delete.png'/></form></div>\n";
}
echo "</table><br />\n";
and the code for retrieval:
$tfid= isset($_GET["tfid"]) ? $_GET["tfid"] : NULL;
$fname = isset($_GET["fname"]) ? $_GET["fname"] : NULL;
$lname = isset($_GET["lname"]) ? $_GET["lname"] : NULL;
$hca = isset($_GET["hca"]) ? $_GET["hca"] : NULL;
echo $tfid;
echo $fname;
echo $lname;
echo $hca;
After your edit:
After you posted your form code, I noticed the problem may be that the values of your inputs may be empty, as the syntax looks more or less correct. Double check the values via view-source to ensure their validity.
Before your edit:
The errors are ocurring because the variable in fact is not set. Make sure your form looks like this:
<form action="yourpage.php">
<input name="fname" type="text" />
<input name="lname" type="text" />
<input name="hca" type="text" />
<input name="tfid" type="text" />
</form>
As shown above, the _GET variables are looking for the name attribute. A common mistake among beginners (everyone has been guilty of this at one time or another) is the use of id in place of name. id is used for selector purposes whereas name is often used to retrieve values on the server side.
Something to keep in mind....
As #George Cummins mentioned, you should also be aware that $_GET parameters are passed through the URL like so:
http://yoururl.com?var1=data1&var2=data2
In some cases, you may not want the user to see all of the data being sent through your form, so in this case you would want to use $_POST which is essentially a hidden passing of the form's data. In reality, this data is not truly hidden but it certainly is more hidden than the use of $_GET.

PHP MySql $_GET problems

I have tried to search through the forums but I am a novice and am getting more confused.
I am trying to bring an input from a form and use it as a variable in a MySql query. A shortened version of the form is -
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input value=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
I am then trying to store the input into a variable using code -
$newVar = $_GET['leave'];
However I am getting an undefined index error.
Can anyone help with this? Sorry if its a very basic problem :)
The problem is with your HTML. You need to name the input.
echo '<input name="leave" class="text" autocomplete="off" type="text" value="' . $_SESSION['leave'] . '" />';
You declaring the "value attribute twice, you need to declare name:
echo "<form method=\"get\" action=\"\">";
echo "<tr><td>Leave:</td><td><input name=\"".$_SESSION['leave']."\" class=\"text\" autocomplete=\"off\" type=\"text\" value=\"\" /></td></tr>";
echo "</form>";
echo '<form method="get" action="">';
echo "<tr><td>Leave:</td><td><input value='{$_SESSION['leave']}' class='text' autocomplete='off' type='text' name='leave'/></td></tr>";
echo "</form>";
If you use single quotes and doubles quotes alternating, you can make your code look nicer.
For your problem, you're missing your input name:
<input type=".." name="leave" ..>
Also, notice in your output of the field, you have the value set to the session value and an empty value near the end.
value=\"\"

PHP radio buttons returning wrong value

I have PHP radio buttons being generated by the following code:
while($a_row = mysql_fetch_array($answers_result))
{
// print each answer choice
?>
<input type='radio' name='question_<?php echo $current_question['id'] ?>'
value='<?php echo $a_row['prompt']?>'><?php echo $a_row['prompt']?>
<br />
<?php
}
I am then defining the user's choice with this code:
$user_answer = $_POST["question_{$_SESSION['current_question']['id']}"];
However, this isn't always returning the value, which should be whatever $a_row['prompt'] is.. If it is the first radio button in the list, it returns "answer 1", etc.. It is not getting the correct values from my sql table. Am I doing something wrong in my code that is causing it to not actually get the "value" of each radio button when the user selects it and submits the form?
You need to escape the ' ' as you start with them
<input type='radio' name='question_<?php echo $current_question[\'id\']; ?>'
value='<?php echo $a_row[\'prompt\'];?>'><?php echo $a_row['prompt'];?>
Also note that your need to finish your PHP command with ; that not the case.
To avoid ' ' trouble I recompend using " " when you know that you will have to add some ' ' in your code, that way they do not interfere.
<input type='radio' name="question_<?php echo $current_question['id']; ?>"
value="<?php echo $a_row['prompt'];?>"><?php echo $a_row['prompt'];?>

Categories