简体   繁体   English

如何将PHP变量中的NULL值插入MySQL,远离SQL注入?

[英]How to Insert NULL Value from PHP Variable to MySQL, staying away from SQL Injection?

I have a Song Uploading Form, where I will not directly input NULL value in MySQL like: mysql_query("INSERT INTO songs ( album_id ) VALUES (NULL)". I will insert NULL from PHP Variable to MySQL, and surely being safe from SQL Injection. 我有一个Song Uploading Form,我不会在MySQL中直接输入NULL值,如:mysql_query(“INSERT INTO songsalbum_id )VALUES(NULL)”。我将从PHP变量中插入NULL到MySQL,当然可以安全地从SQL注射。

My SQL Table is: 我的SQL表是:

CREATE TABLE IF NOT EXISTS `songs` (
  `song_id` int(4) NOT NULL,
  `song_name` varchar(64) NOT NULL,
  `artist_id` int(4) NOT NULL,
  `album_id` int(4) DEFAULT NULL,
  `genre_id` int(4) DEFAULT NULL
  PRIMARY KEY (`song_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

My FORM and FORM HANDLER Code is (PHP, HTML) like below: 我的FORM和FORM HANDLER代码是(PHP,HTML),如下所示:

<?php

if(isset($_REQUEST['SongForm']))
{
$song_name = trim($_POST['song_name']);
$artist_id = trim($_POST['artist_id']);
$album_id = $_POST['album_id']; if($album_id == 0) { $album_id = 'NULL'; } // I even tried using NULL instead of 'NULL'
$genre_id = $_POST['genre_id']; if($genre_id == 0) { $genre_id = 'NULL'; }

$query = mysql_query("
INSERT INTO `songs` (`song_name`, `artist_id`, `album_id`, `genre_id`) 
VALUES ('".$song_name."', '".$artist_id."', '".$album_id."', '".$genre_id."')
");
}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>" name="SongForm" id="SongForm">
<table style="width: 100%">
<tr>
<td><b>Song Name</b></td>
<td><input name="song_name" value="" type="text" required /></td>
</tr>
<tr>
<td><b>Artist Name</b></td>
<td>
<select id="artist_id" name="artist_id">
<option value ="0">No Artist</option>
<option value ="1">Jennifer Lopez</option>
</select>
</td>
</tr>
<tr>
<td><b>Album Name</b></td>
<td>
<select id="album_id" name="album_id">
<option value ="0">No Album</option>
<option value ="1">Rebirth</option>
</select>
</td>
</tr>
<tr>
<td><b>Genre Name</b></td>
<td>
<select id="genre_id" name="genre_id">
<option value ="0">No Genre</option>
<option value ="1">Epic Records</option>
</select>
</td>
</tr>
<tr>
<td><b>&nbsp;</td></b>
<td><input name="SongForm" type="submit" value="Upload Song" /></td>
</tr>
</table>
</form>

But after this I get the Result in MySQL like: 但在此之后我得到了MySQL中的结果:

Serial ID: 1 Song Name Name: I, Love Artist ID: 1 Album ID: 1 Genre ID: 1 序列号:1歌曲名称:I,Love Artist ID:1专辑ID:1 Genre ID:1

Now, when I am NOT SELECTING "Album ID" and "Genre ID" for any song, it should Input "NULL" inside MySQL. 现在,当我没有为任何歌曲选择“专辑ID”和“类型ID”时,它应该在MySQL内输入“NULL”。 But it is inputting "0". 但它输入“0”。

Therefore the result is coming like: 因此结果如下:

Serial ID: 1 Song Name Name: I, Love Artist ID: 1 Album ID: 0 Genre ID: 0 序列号:1歌曲名称:I,Love Artist ID:1 Album ID:0流派ID:0

Please give me a solution so that I can input NULL when I choose "No Album" and "No Genre". 请给我一个解决方案,以便在选择“No Album”和“No Genre”时输入NULL。

Please don't make me confused explaining irrelevant topics. 请不要让我困惑解释不相关的主题。

Thanks to the friends who supported explaining answers, though any of the answers didn't give me proper solution yet. 感谢支持解释答案的朋友,尽管任何答案都没有给我正确的解决方案。

In your code, check your variable is empty, if they are empty, use NULL instead of the variable. 在您的代码中,检查您的变量是否为空,如果它们为空,则使用NULL而不是变量。 To pass a NULL to MySQL , try 要将NULL传递给MySQL ,请尝试

INSERT INTO table (field,field2) VALUES (NULL,3)

I tried the same thing that you did- set the variable to NULL, null, 'NULL', "NULL", even wrote null as a constant in the SQL string- nothing. 我尝试了同样的事情 - 将变量设置为NULL,null,'NULL',“NULL”,甚至在SQL字符串中将null写为常量 - 没有。 The field in the database was always set to 0. Then I tried updating the table after inserting the row and that worked. 数据库中的字段始终设置为0.然后我尝试在插入行后更新表并且工作正常。

"UPDATE songs SET album_id = NULL WHERE etc."

The INSERT statement will insert a 0 if the column is specified at all. 如果完全指定了列,则INSERT语句将插入0。 You could modify the SQL statement to not include the column that you want to be NULL, for example: 您可以修改SQL语句,使其不包含您想要为NULL的列,例如:

$query = mysql_query("
INSERT INTO `songs` (`song_name`,`artist_id`) VALUES ('".$song_name."','".$artist_id."')
");

and that would leave the remaining columns NULL. 这将使剩余的列保持为NULL。

http://www.w3schools.com/sql/sql_insert.asp http://www.w3schools.com/sql/sql_insert.asp

skip to Insert Data Only in Specified Columns 跳到仅在指定列中插入数据

use if($album_id == 0) { $album_id = NULL; } 使用if($album_id == 0) { $album_id = NULL; } if($album_id == 0) { $album_id = NULL; } instead of if($album_id == 0) { $album_id == "NULL"; } if($album_id == 0) { $album_id = NULL; } ,而不是if($album_id == 0) { $album_id == "NULL"; } if($album_id == 0) { $album_id == "NULL"; } . if($album_id == 0) { $album_id == "NULL"; }

"NULL" is a string.

Remove the quotes and change ==(Checking equality) to =(Assigning a value to variable) in $album_id == "NULL"; 删除引号并在$ album_id ==“NULL”中将==(检查相等)更改为=(为变量赋值);

$album_id = trim($_POST['album_id']); if($album_id == 0) { $album_id = NULL; }
$genre_id = trim($_POST['genre_id']); if($genre_id == 0) { $genre_id = NULL; }
$film_id = trim($_POST['film_id']); if($film_id == 0) { $film_id = NULL; }
$category_id = trim($_POST['category_id']); if($category_id == 0) { $category_id = NULL; }

EDIT For the current question: 编辑对于当前的问题:

Lets assume $album_id=='NULL' , $genre_id=='NULL' , $artist_id==1 and $song_name=='deeper' 让我们假设$album_id=='NULL'$genre_id=='NULL'$artist_id==1$song_name=='deeper'

This query (the your one): 这个查询(你的):

"INSERT INTO `songs` (`song_name`, `artist_id`, `album_id`, `genre_id`) 
 VALUES ('".$song_name."', '".$artist_id."', '".$album_id."', '".$genre_id."')"

Will get you: 会得到你:

"INSERT INTO `songs` (`song_name`, `artist_id`, `album_id`, `genre_id`) 
 VALUES ('deeper', '1', 'NULL', 'NULL')"

Which is not what you want. 这不是你想要的。 Get rid of the slashes arround integers and nulls. 摆脱整数和整数的斜线。 This query: 这个查询:

"INSERT INTO `songs` (`song_name`, `artist_id`, `album_id`, `genre_id`) 
 VALUES ('$song_name', $artist_id, $album_id, $genre_id)"

Will get you: 会得到你:

"INSERT INTO `songs` (`song_name`, `artist_id`, `album_id`, `genre_id`) 
 VALUES ('deeper', 1, NULL, NULL)"

Which is what you want. 这是你想要的。


To avoid sql injection make another little change: 为了避免sql注入做了一点改变:

// do not use addslashes when your server uses magic_quotes (probably it does not)
$song_name = addslashes(trim($_POST['song_name']));
// if there is number, it will stay unchanged
// otherwise it is changed to 0, which is safe
$artist_id = $_POST['artist_id'] + 0;
$album_id = $_POST['album_id'] + 0; if($album_id == 0) { $album_id = 'NULL'; }
$genre_id = $_POST['genre_id'] + 0; if($genre_id == 0) { $genre_id = 'NULL'; }

When you want to ensure your script will work properly independently on magic_quotes, define this function: 如果要确保脚本在magic_quotes上独立运行,请定义此函数:

function gpc_addslashes($str) {
    return (get_magic_quotes_gpc() ? $str : addslashes($str));
}

and use it instead of addslashes() for the song name. 并使用它而不是addslashes()为歌曲名称。

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

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