English 中文(简体)
php Input field coming across as Integer
原标题:

EDIT 2: After writing up an incredibly long explanation in more detail I, of course, discovered my problem and it had nothing to do with the question I asked. It was caused because I was creating a custom object of mine, assigning an uploaded images name to it s "LogoName" property...then creating a new version later in code, not assigning that property to the new object and then trying to save the new object (with no LogoName set) to the database.

Sorry to have wasted your time. Thank you for your answers. They have all been upvoted. END EDIT 2

I have a form in a php site. The form has the usual City, State, Zip input options. City looks like this:

<label for="city">City</label><input type="text" name="city" value="<?php echo $business->city; ?>" id="city">

Zip looks like this:

<label for="zip">Zip</label><input type="text" name="zip" value="<?php echo $business->zip; ?>" id="zip">

When I check my $_POST the values look like this: (using FirePHP)

[ city ] =>  St. Louis 
[ zip ] => 12345

So naturally when I put those values into my object, and try to save that object to the database (which has Zip as a varchar) I get errors because Zip is recognized as an integer.

How do I tell the form to force the Zip value in the $_POST as a string?

EDIT: I didn t even think about this but maybe it s relevant. The form is set up to allow an image upload as well so the form has enctype="multipart/form-data" set. Could that be causing this issue?

Other than that I don t know what to think as I am using FirePHP to log the $_POST information pretty much as soon as the form loads on Submit. I can t think of anything that would cause this issue.

最佳回答

I tried your example, but get

    [zip] => 12345
    [city] => Anywheresville

Normally, one doesn t have to worry about integers not looking like strings. I m hard pressed to think of a case where a reasonable conversion wouldn t happen automatically. May be you could post the code which forms the structure and generates the query?

问题回答

You could cast it to a string like this

$zip = (string) $_POST[ zip ];

You can do strval($var);
or $val.""; :)





相关问题
Dynamic creation of ASP.NET Form Elements

I m trying to build a form which generates itself as it is used. I have created a really simplistic example, loosely related to what I m trying to do below, but which demonstrates the problem. The ...

php Input field coming across as Integer

EDIT 2: After writing up an incredibly long explanation in more detail I, of course, discovered my problem and it had nothing to do with the question I asked. It was caused because I was creating a ...

IE7 onSubmit return false in function may fail?

I found this forum thread in google, but no one here seems to encounter the same problem, so I would like to know if onsubmit= return false; really fails in some IE7 browsers, meaning that it has ...

Required form field communication

I ve always communicated required form fields on web applications to users by hanging an asterisk off the end of the control or the label and having a little sentence along the lines of "required ...

WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Can the behaviour of new HTML5 form types be overridden?

I was wondering if anyone knew if it were possible to override the default behaviour of browsers that support the new HTML input types such as type="email" and type="date"? I appreciate that I could ...

热门标签