错误更新Mysql的列中的数据

时间:2016-01-03 17:30:41

标签: php mysql

对不起,问一个简单的问题,

但我真的不知道这个问题是什么。

$id = $_POST['id']; //i get the value from form
$query = "UPDATE jewelry SET stock = stock - $quantity WHERE jewelid = '".$id."'"
mysql_query($query); 

但如果我使用此代码:

$query = "UPDATE jewelry SET stock = stock - $quantity"

它正常工作,但每个列都被减去..所以我们必须使用where,但如果我使用上面的where,它就不会工作..

我的代码中的错误在哪里?

1 个答案:

答案 0 :(得分:0)

试试这个:

    2016-01-03 11:09:30.359  INFO 6028 --- [           main] com.example.AddressServiceApplication    : Starting AddressServiceApplication on Rishit with PID 6028 (started by Rishit Shah in D:\Rishit\Java workspaces\AddressService) 
2016-01-03 11:09:30.364  INFO 6028 --- [           main] com.example.AddressServiceApplication    : No active profile set, falling back to default profiles: default 
2016-01-03 11:09:30.449  INFO 6028 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33cb5951: startup date [Sun Jan 03 11:09:30 CST 2016]; root of context hierarchy 2016-01-03 11:09:31.655  INFO 6028 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 
2016-01-03 11:09:32.792  INFO 6028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8084 (http) 
2016-01-03 11:09:32.814  INFO 6028 --- [          main] o.apache.catalina.core.StandardService   : Starting service Tomcat 2016-01-03 11:09:32.816  INFO 6028 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30 2016-01-03 11:09:32.965  INFO 6028 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 2016-01-03 11:09:32.965  INFO 6028 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2521 ms 
2016-01-03 11:09:33.628  INFO 6028
    --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/] 
2016-01-03 11:09:33.637  INFO 6028 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*] 
2016-01-03 11:09:33.639  INFO 6028
    --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
2016-01-03 11:09:33.639  INFO 6028 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
 2016-01-03 11:09:33.639  INFO 6028
    --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
 2016-01-03 11:09:34.221  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33cb5951: startup date [Sun Jan 03 11:09:30 CST 2016]; root of context hierarchy 2016-01-03 11:09:34.315  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
 2016-01-03 11:09:34.317  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
 2016-01-03 11:09:34.371  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
 2016-01-03 11:09:34.371  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-01-03 11:09:34.421  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-01-03 11:09:34.588  INFO 6028 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 
2016-01-03 11:09:34.753  INFO 6028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8084 (http) 
2016-01-03 11:09:34.764  INFO 6028 --- [          main] com.example.AddressServiceApplication    : Started AddressServiceApplication in 4.867 seconds (JVM running for 5.705) 
2016-01-03 11:10:03.737  INFO 6028 --- [nio-8084-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet' 2016-01-03 11:10:03.737  INFO 6028 --- [nio-8084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started 2016-01-03 11:10:03.759  INFO 6028 --- [nio-8084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms

如果$query = "UPDATE jewelry SET stock = 'stock - " . $quantity . "' WHERE jewelid = " . $id; 是int,则无需添加jewelid只需执行= '" . $id . "'而不需要单引号