简体   繁体   English

MySQL更新和插入-随机不会插入

[英]MySQL update and insert - randomly won't insert

I'm having a really strange issue with what I believe is a simple query. 我遇到一个非常奇怪的问题,我认为这是一个简单的查询。 This was code I used to learn PHP a few years ago and was trying to repurpose it for a really simple time tracking application. 这是几年前我用来学习PHP的代码,并试图将其用于真正简单的时间跟踪应用程序。 When time permits, I'll work on making a new version with mysqli, but in the meantime I just want to get it working. 如果时间允许,我将使用mysqli制作新版本,但与此同时,我只想使其正常运行。 I'm still learning, so it's not easy for me. 我还在学习,所以这对我来说并不容易。

When a form posts, I'm basically gathering the data and trying to 1) update an existing value in one table based on submitted information and 2) create a new row in another table to store all entered values from the previous form. 表单发布后,我基本上是在收集数据,并试图1)根据提交的信息更新一个表中的现有值,以及2)在另一表中创建新行以存储上一个表单中的所有输入值。

Here's how I had written it: 这是我写的方式:

 <?php $uid = $_COOKIE["uid"]; $cid = $_POST['customer_id']; $pid = $_POST['project_id']; $hoursAdded = $_POST['hoursAdded']; mysql_query("UPDATE projects SET hours=hours+'$hoursAdded' WHERE id='$pid'") or die(mysql_error()); mysql_query("INSERT INTO hours (id,uid,pid,cid,userhours) VALUES ('','$uid','$pid','$cid','$hoursAdded')") or die(mysql_error()); ?> 

Everything works fine, but occasionally the SECOND mysql_query statement doesn't actually INSERT anything into the hours table. 一切正常,但有时SECOND mysql_query语句实际上并未在小时表中插入任何内容。 The First query always writes, but the second sometimes just doesn't do anything - no errors - nothing. 第一个查询总是写,但是第二个查询有时什么也不做-没有错误-什么也没有。

Essentially, the sum of the hours submitted in the HOURS table should equal the TOTAL hours updated in the projects table. 从本质上讲,“小时”表中提交的小时总数应等于“项目”表中更新的总小时数。 Because the second query sometimes doesn't write, I end up with a project that has, for example, 200 TOTAL hours, and the sum of all hours for that project in the HOURS table totalling much less than 200. 因为第二个查询有时不写,所以我最终得到一个项目,该项目具有例如200 TOTAL小时,而HOURS表中该项目的所有小时总和少于200。

Any ideas what's going on? 有什么想法吗? Why would it sometimes write and other times not? 为什么有时写而其他时候不写呢?

Thanks and sorry if the problem is obvious. 如果问题很明显,谢谢和抱歉。 This is still new to me. 这对我来说仍然是新的。

You have syntax errors. 您有语法错误。 Try this out 试试看

<?php

    $uid = $_COOKIE["uid"];
    $cid = $_POST['customer_id'];
    $pid = $_POST['project_id'];
    $hoursAdded = $_POST['hoursAdded'];


    mysql_query("UPDATE projects SET hours=hours + $hoursAdded 
            WHERE id=$pid") or die(mysql_error());

    mysql_query("INSERT INTO hours (uid,pid,cid,userhours) 
            VALUES ($uid,$pid,$cid,$hoursAdded)") 
            or die(mysql_error());        
 ?>

I have removed single quotes from all the variables inside the mysql_query() . 我从mysql_query()内部的所有变量中删除了单引号。 Adding single quotes to a variable makes the of type string . 在变量中添加单引号会使string类型为。 I would also suggest check if the php variables you have defined are empty or not before you insert them in the query. 我还建议您在将其插入查询之前检查您定义的php变量是否为空。 I have also remove id from hours table as its set to autoincrement therefore no use of it in the query. 我还将hours表中的id删除,因为它设置为自动递增,因此在查询中不使用它。 It will automatically increment when a new row is added 添加新行时它将自动递增

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM