doutuan4361 2012-06-11 00:32
浏览 42
已采纳

通过php添加记录mysql时,尝试从列中提取5个字符

My part_no column has the following format: 000-00000-00 for all records.

I need to extract the five middle characters from part_no and place it in the core column when I create the record.

I can't get my script to work.

I'm not getting any errors. Just not working.

$order = "INSERT INTO cartons_added (add_time, type, part_no, add_type, add_qty, add_ref, add_by, add_notes)
        VALUES
        ('$date',
        '$_POST[type]', 
        '$_POST[part_no]', 
        '$_POST[add_type]', 
        '$_POST[add_qty]', 
        '$_POST[add_ref]', 
        '$_POST[add_by]', 
        '$_POST[add_notes]')";

$result = mysql_query($order);
$query2 = "select part_no from cartons_current";
$sel = mysql_query($query2);
$res = mysql_result($sel);
while($row = mysql_fetch_row($res)) {
    $core_digits = split('-',$row[0]);
    $core =$core_digits[1];
    $query3 = "insert  into cartons_current(core) values($core)";
    $sel2 = mysql_query($query3);
}
  • 写回答

5条回答 默认 最新

  • duankeye2342 2012-06-11 18:26
    关注

    You are right, the script has no error.

    I think the problem is on your SQL that made you can't insert a new row, specifically on the table structure. Maybe you defined a PRIMARY KEY without AUTO_INCREMENT, defined a INDEX or UNIQUE key that is not the core key or there have some other key that did not have default value. Remember that you can't insert a row without defining all required field.

    You script is selecting all part_no and for every part_no you are inserting a new row in the same table, so maybe there is the problem.

    I think what you want is update every result to add they core value, you can do that with UPDATE as this code:

    function getValue($value) {
      return "'" . trim(mysql_real_escape_string($value)) . "'";
    }
    
    mysql_query('INSERT INTO `cartons_added` (`add_time`, `type`, `part_no`, `add_type`, `add_qty`, `add_ref`, `add_by`, `add_notes`)
                 VALUES (' . 
                  getValue($date) . ', ' .
                  getValue($_POST[type]) . ', ' .
                  getValue($_POST[part_no]) . ', ' .
                  getValue($_POST[add_type]) . ', ' .
                  getValue($_POST[add_qty]) . ', ' .
                  getValue($_POST[add_ref]) . ', ' .
                  getValue($_POST[add_by]) . ', ' .
                  getValue($_POST[add_notes]) . 
                 ')');
    
    $partNoQuery = mysql_query('SELECT `part_no` FROM `cartons_current`');
    
    while($partNoResult = mysql_fetch_assoc($partNoQuery)) {
        list($prefix, $core, $suffix) = explode('-', $partNoResult['part_no']);
        mysql_query('UPDATE cartons_current SET `core` = \'' . $core . '\' WHERE `part_no` = \'' . $partNoResult['part_no'] . '\'');
    }
    

    I added getValue function to escape posted data to prevent SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了