I have a little problem… And I have looked up everywhere how to convert an integer to a string. I found several ways of doing it, but with no success. So right now I have no idea of what the problem might be. I thought that I should ask you if also other people have the same problem as I do.
So what doesn’t work:
$inputname = rand(1, 10000);
$inputname = "$inputname";
echo '
<h3>Translate to English</h3>
<form name="word" method="post" action="">
<table border="0">
<tr><td><label>Swedish:</label></td><td width="200"><input type="text" name="swe" maxlength="100" value="'.$swe.'" readonly /></td></tr>
<tr><td><label>English:</label></td><td width="200"><input type="text" name="'.$inputname.'" maxlength="100" /></td></tr>
<tr><td></td><td><input type="submit" name="nextword" value="Next Word" /></td></tr>
</table>
</form>
';
if (isset($_POST['nextword'])) {
$eng = $_POST[$inputname];
$swe = $_POST['swe'];
$word = $row['id'];
if($eng == $row['eng']){
mysql_query("UPDATE `words` SET `right`='yes' WHERE `id`='$word'");
}
else{
mysql_query("UPDATE `words` SET `right`='no' WHERE `id`='$word'");
}
I don’t get any errors, but when I check my database it says right=no even if I typed in the correct word. If I change $inputname to a string using $inputname = “string”; it works… So why doesn’t the convert work?
I have tried (string)$var, $var = “$var”, mysql_real_escape_string($var) but with no success…
If you have any ideas of how to solve this please respond.
Although I haven’t seen the contents of your variables, you have a logical problem: When you open your page the first time, a form is created with a random name for the English input. Then you submit the form and generate a new random name, so these will probably never ever match.
For example you send in a variable named
$_POST['1234']and then you look for$_POST[$inputname]where$inputnamehas just been generated randomly.Giving a random name to your form variable is a bad idea because it is not that straightforward to receive it after the form has been submitted.
Apart from that I don’t think your
nameattribute can start with a number, but that’s not the cause of the problem.