1

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.

1
  • 1
    What are the results of var_dump($row) and var_dump($_POST)? Commented Dec 6, 2011 at 14:19

2 Answers 2

1

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 $inputname has 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 name attribute can start with a number, but that's not the cause of the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

As others mention, the issue doesn't seem to be integer conversion so much as a problem with the logic.

Perhaps a "poor man's debugging session" might shed some light on the error: To get a better idea of what your script is doing as-is, change the mysql_query lines to simply echo the query to the browser (ie, echo "UPDATEwordsSETright='yes' WHEREid='$word'"; )

You'll then be able to see the SQL statements being made (which are most likely not what you think they are), and should help you see the error more plainly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.