使用mysql/php的INSERT语句中的WHERE子句 [英] WHERE clause in INSERT statement using mysql/php

查看:71
本文介绍了使用mysql/php的INSERT语句中的WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google上搜索后,我知道了, 我不能在INSERT查询中使用WHERE子句. 但是我想在"Book_4"列上插入一个值,其中"Student_ID = 1"

After searching on Google, i came to know that, I can not use a WHERE clause in my INSERT query.. But i want to insert a value on column "Book_4" where "Student_ID = 1"

我该怎么做? 有其他替代方法吗?

How can i do that ?? Is there any alternate to do that ?

将感谢您!

$Query = "INSERT INTO issued_books (Book_4) VALUES ('$IssuedBookNumber')" ;

已 更多详细信息 当我在表格的"Student_ID"列中插入值时,使用插入查询. Student_ID行中的所有列(Student_ID除外)在我的数据库中均显示为0. 我不知道根据DB,这个0是什么意思. 它可以为Null或数字0. 如果它是数字0,则应使用UPDATE语句对其进行更新. 但是,每当我尝试更新它时,它就永远不会使用UPDATE语句进行更新.这就是为什么我要问!

EDITED: More Details Using insert query, when i insert a value in column "Student_ID" in my table. All columns in the row of Student_ID (except Student_ID) shows 0 in my DB. I dun know what this 0 means according to DB. It might be Null or numeric 0. If it is a numeric 0, then it should be updated using the UPDATE statement. But whenever i'm trying to update it, it never updates using UPDATE statement. That's why i'm asking !

P.S:所有列的数据类型均为INT.

P.S: All columns have Datatype INT.

希望您能理解我想说的话:)

Hope you understand what i want to say :)

这是完整的代码. 假设:已经创建了具有值2的Student_ID. IssuedBookNumber = 51

Here is the complete code. Suppose: Student_ID is already created having the value 2. IssuedBookNumber = 51

使用以上值: 结果=创建了一个新行,其中所有列0除外,但"IssuedBookNumber"列的值为51.

Using the above values: Result = A new row is created having all columns 0 except the column "IssuedBookNumber" that is having value = 51.

当我想要的时候,结果应该是: 在Student_ID = 2的行上,Book_4应该为51.

While i want, the result should be: On row Student_ID = 2, Book_4 should be 51.

问题是,当我在Student_ID上插入一个值时,所有其他列在同一行上都变为0.但是,当同一行上的任何列中有除0以外的任何数字时(当我在Student_ID中插入一个值时,该列会自动出现在所有列上).更新查询将起作用..!

The point is, When i inserted a value on Student_ID, all other columns becomes 0 on the same row. But when any of the column on the same row having any number except the 0 (that was automatically came on all columns when i inserted a value in Student_ID). Update Query will work.. !

$IssuedBookNumber = $_POST['IssuedBookNumber'];
     $Student_ID = $_POST['StudentId'];

     $FetchingQuery = "SELECT * FROM issued_books WHERE Student_ID='" . $Student_ID . "'";

     $RunFetchingQuery = mysql_query($FetchingQuery);


     while ( $row = mysql_fetch_array( $RunFetchingQuery ) ) {

          $Book_1 = $row[ 'Book_1' ];
          $Book_2 = $row[ 'Book_2' ];
          $Book_3 = $row[ 'Book_3' ];
          $Book_4 = $row[ 'Book_4' ];
          $Book_5 = $row[ 'Book_5' ];

          }

          if(!empty($Book_4))
          {
               $Update =  "UPDATE issued_books SET Book_4='$IssuedBookNumber' WHERE Student_ID= '$Student_ID'";
             mysql_query ($Update);
          }

          else
          {
               $AddQuery = "INSERT INTO issued_books (Book_4) VALUES ('$IssuedBookNumber')";
    mysql_query ($AddQuery);
          }

推荐答案

那不是INSERT.这是 UPDATE . INSERT语句插入 new 行. UPDATE语句更新现有行.

That not an INSERT. That's an UPDATE. INSERT statements insert a new row. UPDATE statements update an existing row.

UPDATE issued_books
SET Book_4 = '$IssuedBookNumber'
WHERE Student_ID = '$Student_ID'

(我假设您已经正确逃脱了$IssuedBookNumber$Student_ID)

(I'm assuming you've properly escaped $IssuedBookNumber and $Student_ID)

这篇关于使用mysql/php的INSERT语句中的WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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