dsw1608 2015-07-14 10:30
浏览 165
已采纳

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

i know that there is alot of similar topics posted about similiar problems but none helped me in my case. I am creating an Insert form for table called product. I am passing data with POST method. This is the part that is giving me troubles. Since with POST you pass string values, I need to convert the id_category to INT because the type is set to INT in the database. The problem is that when i convert the string value to INT the value that is returned is 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>";
        }
    }

This is the part of the code that accepts the data and assigns it to 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']);

The table is on pastebin http://pastebin.com/d067ZpS8

  • 写回答

1条回答 默认 最新

  • duangong937906 2015-07-14 10:42
    关注

    So, your select boxes values come like this:

    "{2}"
    

    remove the { and } in the line starting like this

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

    so its more like

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

    that should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效