将字符串转换为INT将返回0

时间:2015-07-14 10:30:42

标签: php mysql string int

我知道有很多关于类似问题的类似主题,但在我的案例中没有一个帮助我。 我正在为表创建一个名为product的Insert表单。我用POST方法传递数据。 这是给我带来麻烦的部分。由于使用POST传递字符串值,我需要将id_category转换为INT,因为数据库中的类型设置为INT。问题是,当我将字符串值转换为INT时,返回的值为0.

<form method="POST" action="insert_product.php" enctype="multipart/form-data">
    <table>
        <tr>
            <td>Kategorija:</td>
<td><select name="category" tabindex="1">
    <option value="">Odaberi kategoriju</option>

<?php
    $sql="SELECT * FROM category ORDER BY category_name";
    $result=mysqli_query($connection,$sql) or die(mysql_error());
    if(mysqli_num_rows($result)>0){
        while($row=mysqli_fetch_assoc($result)){
            echo "<option value='{".$row['id_category']."}'>".$row['category_name']."&nbsp;&nbsp;".$row['id_category']."</option>";
        }
    }

这是接受数据并将其分配给varibles的代码的一部分。

if(isset($_POST['submit']))
{
 $category=mysqli_real_escape_string($connection, $_POST['category'];
 $categoryINT=intval($category);
 $manufacturer=mysqli_real_escape_string($connection,$_POST['manufacturer']);
 $name=mysqli_real_escape_string($connection,$_POST['name']);
 $power=mysqli_real_escape_string($connection,$_POST['power']);
 $catalogue_number=mysqli_real_escape_string($connection,$_POST['catalogue_number']);
 $price=mysqli_real_escape_string($connection,$_POST['price']);
 $description=mysqli_real_escape_string($connection,$_POST['description']);
 $post_image=mysqli_real_escape_string($connection,$_FILES['image']['name']);
 $image_tmp=mysqli_real_escape_string($connection,$_FILES['image']['tmp_name']);

该表位于pastebin上 http://pastebin.com/d067ZpS8

1 个答案:

答案 0 :(得分:0)

因此,您的选择框值如下所示:

"{2}"

从此处开始删除行中的{}

echo "<option value='{".$row['id_category']."}'>

所以更像是

echo "<option value='".$row['id_category']."'>

应该有效

相关问题