mysqli没有更新数据库

时间:2015-03-05 17:19:52

标签: php mysqli

我正在尝试将我的网站转换为mysqli,我无法更新数据库或在网站上显示结果。此外,页面不是重定向,它只显示一个空白的EditPost.php页面

如果你能告诉我可能做错了什么我会非常感激。

EditPost.php

   <?php

    $db = new mysqli("localhost","admin","password","database"); 

    if(!$db) {
    die('sorry we are having some problbems');
}

if(isset($_POST['submit'])) {

    $id = $_POST['EditID'];
    $itemname = $_POST['itemname'];
    $manufacture = $_POST['manufacture'];
    $model = $_POST['model'];
    $serial = $_POST['serial'];
    $year = $_POST['year'];
    $condition = $_POST['condition'];
    $category = $_POST['category'];
    $desc = $_POST['desc'];
    $dimension = $_POST['dimensions'];
    $location = $_POST['location'];
    $price = $_POST['price'];
    $purchase = $_POST['purchase'];
    $addedby = $_POST['addedby'];
    $notes = $_POST['notes'];
    $ran = $_POST['ran'];
    $electrical = $_POST['electrical'];
    $owner = $_POST['owner'];
    $featured = $_POST['featured'];
    $showmanu = $_POST['showmanu'];
    $showmodel = $_POST['showmodel'];
    $showserial = $_POST['showserial'];
    $showyear = $_POST['showyear'];
    $showdem = $_POST['showdem'];
    $showelec = $_POST['showelec'];
    $showran = $_POST['showran'];
    $showloca = $_POST['showloca'];
    $showown = $_POST['showown'];
    $showpur = $_POST['showpur'];
    $showsale = $_POST['showsale'];

    $query = "UPDATE new_equip SET `itemname`=?, `manufacture`=?, `model`=?, `serial`=?, `year`=?, `condition`=?, `category`=?, `desc`=?, `dimension`=?, `location`=?, `price`=?, `purchase`=?, `addedby`=?, `notes`=?, `ran`=?, `electrical`=?, `owner`=?, `featured`=?, `showmanu`=?, `showmodel`=?, `showserial`=?, `showyear`=?, `showdem`=?, `showelec`=?, `showran`=?, `showloca`=?, `showown`=?, `showpur`=?, `showsale`=? WHERE id=?  LIMIT 1";
    $conn = $db->prepare($query);
    $conn->bind_param("sssssssssssssssssiiiiiiiiiiiii", $_POST['item'], $_POST['manufacture'], $_POST['model'], $_POST['serial'], $_POST['year'], $_POST['condition'], $_POST['category'], $_POST['desc'], $_POST['dimension'], $_POST['location'], $_POST['price'], $_POST['purchase'], $_POST['addedby'], $_POST['notes'], $_POST['ran'], $_POST['electrical'], $_POST['owner'], $_POST['featured'], $_POST['showmanu'], $_POST['showmodel'], $_POST['showserial'], $_POST['showyear'], $_POST['showdem'], $_POST['showelec'], $_POST['showran'], $_POST['showloca'], $_POST['showown'], $_POST['showpur'], $_POST['showsale']);

    if(!$conn->execute()){trigger_error("there was an error....".$db->error, E_USER_WARNING);}
    header('location: inventory.php?Msg=Update');

    $db->close();
        }

    ?>

表格的一部分

<form method="post" action="EditPost.php" enctype="multipart/form-data" class="form-horizontal" accept-charset="UTF-8">
    <div class="form-group">
        <label class="col-md-3">Item ID</label>
        <div class="col-md-8">
            <input type="text" name="EditID" value="<?php echo $row['id']; ?>" class="form-control" />
        </div> <!-- /.col -->
    </div> <!-- /.form-group -->

    <div class="form-group">
        <label class="col-md-3">Item Name</label>
        <div class="col-md-8">
            <input type="text" name="itemname" value="<?php echo $row['itemname']; ?>" class="form-control" />
        </div> <!-- /.col -->
    </div> <!-- /.form-group -->

    <div class="form-group">
        <label class="col-md-3">Manufacture</label>
        <div class="col-md-8">
            <input type="text" name="manufacture" value="<?php echo $row['manufacture']; ?>" class="form-control" />
        </div> <!-- /.col -->
        <input type="checkbox" name="showmanu" value="1" <?php echo ($row['showmanu'] == 1) ? 'checked="checked"' : ''; ?> />
        <span style="float:right; font-size: 10px; margin-top: 4px">Check to show</span>
    </div> <!-- /.form-group -->

这是我试图遵循的教程。 http://coderlearner.com/PHP_MySQLi_Example_Update_Record

在电路板上进行一些搜索后,我找到了一个显示不同&#34; $查询的线程&#34;。这会是我必须使用的吗?

$query = "UPDATE new_equip SET `itemname`=?, `manufacture`=?, `model`=?, `showmanu`=?, `showmodel`=?, `showserial`=? WHERE `id`=?  LIMIT 1";
$conn = $db->prepare($query);
$conn->bind_param('sssiii', $_POST['item'], $_POST['manufacture'], $_POST['model'], $_POST['showmanu'], $_POST['showmodel'], $_POST['showserial']);

1 个答案:

答案 0 :(得分:1)

您在$id参数的类型字符串中缺少一个字符。它应该是:

$conn->bind_param("sssiiii", $item, $manufacture, $model, $showmanu, $showmodel, $showserial, $id);

每个参数都需要一个类型字母。由于您有7个参数,因此必须有7种类型。

此外,由于您的表单使用name="EditID"作为“商品ID”字段,因此您需要使用:

$id = $_POST['EditID'];

匹配它。