Php MySQL更新查询不更改数据库中的值
我需要一些帮助,我有一个更新查询,它没有更新数据库中的记录。也许这是我遗漏的一个小错误,一些我可能遗漏的东西,让一双新的眼睛更容易发现Php MySQL更新查询不更改数据库中的值,php,mysql,sql-update,Php,Mysql,Sql Update,我需要一些帮助,我有一个更新查询,它没有更新数据库中的记录。也许这是我遗漏的一个小错误,一些我可能遗漏的东西,让一双新的眼睛更容易发现 <?php $message = '<h4 class="alert_success">A Successfully updated '.$_POST['sell_itemBrand'].' '.$_POST['txtModel'].' - '.$_POST['sell_itemType'].'</h4>'; $color = "
<?php
$message = '<h4 class="alert_success">A Successfully updated '.$_POST['sell_itemBrand'].' '.$_POST['txtModel'].' - '.$_POST['sell_itemType'].'</h4>';
$color = "";
($_POST['colorSel'] =='old' ? $color = $_POST['color'] : $color = $_POST['newColor']);
mysql_query("UPDATE `total_stock` SET `ItemType` = '".$_POST['sell_itemType']."', `ItemBrand` = '".$_POST['sell_itemBrand']."', `ItemModel` = '".$_POST['txtModel']."', `Cost_Price` = '".$_POST['CostPrice']."', `Color` = '".$color."' WHERE `IMEI` = '".$_POST['current_IME']."'") or die(mysql_error());
?>
也许,你的意思是:
$_POST['current_IMEI']
为了您自身的安全,您需要更改代码中的某些内容
在查询中使用$\u POST信息不是一个好的做法,因为有些用户可以对代码执行SQL注入,为了防止这种情况,请使用此函数:
mysql_real_escape_string();
函数mysql_*已被弃用,使用此表单访问数据库不是一个好方法
这样使用:
要连接:
$dbh = new PDO('mysql:host=xxx;port=xxx;dbname=xxx', 'xxx', 'xxx', array( PDO::ATTR_PERSISTENT => false));
要执行查询,请执行以下操作:
$query = 'Any query';
$stmt = $dbh->prepare($query);
$stmt->execute();
while ($result = $stmt->fetch(PDO::FETCH_OBJ)) {
var_dump($result);
}
因此,在您的情况下,您应该这样做:
$query = "UPDATE `total_stock` SET `ItemType` = '?', `ItemBrand` = '?', `ItemModel` = '?', `Cost_Price` = '?', `Color` = '?' WHERE `IMEI` = '?'")
$st = $databaseconnection->prepare($query);
$st->execute(array(mysql_real_escape_string($_POST['sell_itemType']),
mysql_real_escape_string($_POST['sell_itemBrand']),
mysql_real_escape_string($_POST['txtModel']),
mysql_real_escape_string($_POST['CostPrice']),
$color,
mysql_real_escape_string($_POST['current_IME'])));
这是在php中使用数据库的正确形式
希望有帮助。-0.49,感谢您在2014年继续使用mysql\u query
。mysql\u error()的输出是什么?还是安静?最好使用参数化的预处理语句继续讨论mysqli和PDO。这更容易阅读,也不容易出错。凯西的品牌是什么样的?你没有检查错误。在打开之后立即将错误报告添加到文件的顶部,另外,您当前的代码已打开。使用,或与。永远不要相信用户的输入。感谢所有的提示和建议,我刚刚开始学习PHP和MySQL,我显然还有很多东西要学。非常感谢@Guerra,非常感谢。
$query = "UPDATE `total_stock` SET `ItemType` = '?', `ItemBrand` = '?', `ItemModel` = '?', `Cost_Price` = '?', `Color` = '?' WHERE `IMEI` = '?'")
$st = $databaseconnection->prepare($query);
$st->execute(array(mysql_real_escape_string($_POST['sell_itemType']),
mysql_real_escape_string($_POST['sell_itemBrand']),
mysql_real_escape_string($_POST['txtModel']),
mysql_real_escape_string($_POST['CostPrice']),
$color,
mysql_real_escape_string($_POST['current_IME'])));