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

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测