[英]Mysql new rows don't have default value inserted
ALTER TABLE `songs`
CHANGE `artist_name` `artist_name` VARCHAR( 255 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'N/A'
^ So that's my table and the default for artist_name is 'N/A'. ^这是我的桌子,artist_name的默认值为'N / A'。 I'm using a form to submit data. 我正在使用表单提交数据。 However, when I submit the data through a form, the row doesn't contain 'N/A' and the value is empty. 但是,当我通过表单提交数据时,该行不包含“ N / A”,并且该值为空。 Anyone know what is going on? 有人知道发生了什么吗?
$video_id=$_POST['video_id'];
$song_name=$_POST['song_name'];
$artist_name=$_POST['artist_name'];
$movie_name=$_POST['movie_name'];
$language=$_POST['language'];
$oldornew=$_POST['oldornew'];
//inserting data order
$order = "INSERT INTO dhb_dhb.songs
(video_id, song_name,artist_name, movie_name,language,oldornew)
VALUES
('$video_id', '$song_name','$artist_name', '$movie_name', '$language', '$oldornew')";
You'd have to unset the specific $_POST
key if it's empty, so NULL
will be inserted which then automatically is assigned the default value. 如果特定的$_POST
键为空,则必须取消设置,因此将插入NULL
,然后会自动为其分配默认值。 Now you're just inserting a blank string, which is not translated into the default column value, since it isn't NULL
. 现在,您只是插入一个空白字符串,该字符串不会转换为默认的列值,因为它不是NULL
。
EDIT The easiest way to do it then is to use an associative array, containing only the keys/values that are not empty: 编辑然后,最简单的方法是使用关联数组,该数组仅包含不为空的键/值:
$data = array('video_id' => $video_id);
foreach(array('artist_name', 'song_name') as $key)
if(!empty($_POST[$key])){
$data[$key] = mysql_real_escape_string($key);
}
}
$columns = "`". implode("`, `", array_keys($data)) ."`";
$values = "'". implode("', '", array_values($data)) ."'";
$query = "INSERT INTO table ({$columns}) VALUES ({$values})";
This hasn't been tested and values should be tested before inserting (eg is $song_id
really an integer?) but this is the general idea. 这还没有经过测试,值应该在插入之前进行测试(例如$song_id
真的是整数吗?),但这是一般的想法。
Any user input can be checked to see if it's set, and if not, use a default value of NULL: 可以检查任何用户输入是否已设置,如果未设置,则使用默认值NULL:
$variable = (!empty($_POST['variable'])) ? $_POST['variable'] : NULL;
Now calling this in a query will set the default value if $_POST['variable']
is empty (or unset). 现在,如果$_POST['variable']
为空(或未设置),则在查询中调用此方法将设置默认值。
I must caution. 我要注意 Whenever you take user input and put directly into a Database Query as you've done in your code example, you're opening up a serious security vulnerability known as SQL Injection . 就像您在代码示例中所做的那样,每当您接受用户输入并将其直接放入数据库查询中时,就会打开一个严重的安全漏洞,称为SQL Injection 。 Consider sanitizing your user input using mysql_real_escape_string 考虑使用mysql_real_escape_string清理用户输入
EDIT: 编辑:
Your code could then be applied by using something like the following: 然后,可以通过使用以下代码来应用您的代码:
<?php
$data = array(
'video_id',
'song_name',
'artist_name',
'movie_name',
'language',
'oldornew',
);
$insert_ary = array();
foreach ($data as $val) {
if (!empty($_POST[$val])) {
$insert_ary[$val] = mysql_real_escape_string($_POST[$val]);
}
}
$columns = implode(', ', array_keys($insert_ary));
$values = implode("', '", $insert_ary);
$sql = "INSERT INTO dhb_dhb.songs ($columns) VALUES('$values')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.