尝试通过php表单更新mysql记录.需要帮忙? [英] Trying to update mysql record via php form. Need help?

查看:66
本文介绍了尝试通过php表单更新mysql记录.需要帮忙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有要用于更新mySQl行的表单.我已完成搜索检索脚本来查找记录.现在我需要更新它.

I have form that I want to use to update my a mySQl row. I have my search retrieval script done to find the record. Now I need to update it.

提交表单时,我确认part_no已更新,但未显示在数据库中.

When I submit the form, I get a confirmation the part_no was updated, but it doesn't show up in the database.

我做错了什么?有人在我的脚本中看到任何错误吗?

What am I doing wrong? Does anyone see any errors in my script?

谢谢.

Erik

这是我的剧本:

<?PHP
session_start();
?>

<?php


$orig_time=$_POST['orig_time'];
$type=$_POST['type'];
$part_no=$_POST['part_no'];
$description=$_POST['description'];
$count=$_POST['count'];
$size=$_POST['size'];
$min=$_POST['min'];
$max=$_POST['max'];
$qty=$_POST['qty'];

if ($part_no == "") echo "! No identifier retrieved";
else
echo "Amending record $part_no";

$host="localhost";
$username="XXXXXX";
$password="XXXXXX";
$db_name="naturan8_hero";
$tbl_name="cartons_current";

mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

mysql_query("UPDATE cartons_current SET orig_time='$orig_time', type='$type',   
description='$description', count='$count', size='$size', min='$min', max='$max',  
qty='$qty', WHERE reference='$part_no'");

echo "<BR>$part_no was updated.<BR><BR>";
?>

推荐答案

查询中存在语法错误.另外,使用错误检查器查看失败的原因.并确保您实际上有$part_no来标识要更新的正确行:

You have a syntax error in your query. Also, use an error checker to see what failed. And be sure you actually have a $part_no to identify the correct row to update:

再次提醒您注意SQL注入!

And, again, beware of SQL injections!

$orig_time = mysql_real_escape_string($_POST['orig_time']);
// do the same for the other $_POST indexes before passing them to the query!
// if values are numeric, you can cast them to INT to make sure they are numbers
// e.g. $count = intval($_POST['count']);



mysql_query("UPDATE `cartons_current` 
          SET `orig_time` ='".$orig_time."', 
          `type` = '".$type."',
          `description` = '".$description."', 
          `count` = '".$count."', 
          `size` = '".$size."', 
          `min` ='".$min."', 
          `max` ='".$max."',
          `qty` ='".$qty."'   
          WHERE `reference` = '".$part_no."'") or trigger_error(mysql_error());

更新:

尝试调用 mysql_affected_rows()来查看结果您的操作.另外,请考虑

Try calling mysql_affected_rows() to see the outcome of your operation. Also, consider that

使用UPDATE时,MySQL不会更新新值所在的列 与旧值相同.这可能会导致 mysql_affected_rows()可能实际上不等于行数 匹配,仅行数受字面意义影响的行数 查询.

When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.

这篇关于尝试通过php表单更新mysql记录.需要帮忙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆