php更新到mysql中丢失了一些东西

时间:2013-10-12 16:54:36

标签: php database select mysqli

我正在尝试从编辑页面将产品信息更新到mysql数据库中,但我显示它没有做任何事情,甚至没有显示任何错误。

我错过了什么?

<?php 
$dbcs = new mysqli("localhost", "root", "password", "shopping");


// Check connection
if (mysqli_connect_errno($dbcs))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
// Parse the form data and update company information to the system
if (isset($_POST['product_name'])) {

    $pid = $_POST['thisID'];
    $product_name = $_POST['product_name'];
    $product_category = $_POST['product_category'];
    $product_product_retail_price = $_POST['product_retail_price'];
    $product_price = $_POST['product_price'];

    // See if that company name is an identical match to another company in the system
    $sql = "UPDATE product SET
    product_name='$product_name', 
    product_category='$product_category', 
    product_retail_price='$product_retail_price',
    product_price='$product_price'
    WHERE product_id='$pid'" or die(mysql_error());  

    header("location: product.php");

    exit();
}


// Gather these companies full information for inserting automatically into the edit form below on page
if (isset($_GET['pid'])) {
    $targetID = $_GET['pid'];
    $sql = "SELECT * FROM product WHERE product_id='$targetID' LIMIT 1";
  $result=mysqli_query($dbcs,$sql);

      while($row = mysqli_fetch_array($result)){ 
               $product_id = $row["product_id"];
             $product_name = $row["product_name"];
             $product_category = $row["product_category"];
             $product_retail_price = $row["product_retail_price"];
             $product_price = $row["product_price"];
             $screenshot =$row["screenshot"];
        }
}

mysqli_close($dbcs);
?>

3 个答案:

答案 0 :(得分:2)

$sql = "UPDATE product SET
product_name='$product_name', 
product_category='$product_category', 
product_retail_price='$product_retail_price',
product_price='$product_price'
WHERE product_id='$pid'";

$query= mysqli_query($dbcs,$sql);

if(!$query)
{
print "error";

}
else 
{
header("your page link");
}

p.s Thankx纠正Hanky Panky:)

答案 1 :(得分:1)

嗯......也许在定义插入$ sql后运行$result = mysqli_query($dbcs,$sql);

另外,在mysqli_query()之后使用die()不在$ sql字符串定义中。

此外,在查询中使用它们之前,通过mysqli_real_escape_string()运行所有这些变量。

答案 2 :(得分:1)

这是错误的:

$sql = "UPDATE product SET
    product_name='$product_name', 
    product_category='$product_category', 
    product_retail_price='$product_retail_price',
    product_price='$product_price'
    WHERE product_id='$pid'" or die(mysql_error());  

您没有执行该查询。使用mysqli_query()

执行此操作
$sql = "UPDATE product SET
    product_name='$product_name', 
    product_category='$product_category', 
    product_retail_price='$product_retail_price',
    product_price='$product_price'
    WHERE product_id='$pid'";

$result = mysqli_query($dbcs,$sql);