我是php的初学者,但我正在创建一个使用php和mysql的音乐下载网站项目。 我想在一个列中上传多首歌曲,但在代码下方无效。
我有三张桌子:专辑,艺术家和音乐。
album table
+================================
| album_id | album_name | poster|
+================================
| | | |
| | | |
+===============================+
arist table
+==========================
| artist_id | artist_name |
+==========================
| | |
| | |
+=========================+
music tabel
+=====================================+
| artist_name | album_name | song_name|
+=====================================+
| | | |
| | | |
+=====================================+
我想在所有上表中一次插入所有数据。 我的代码如下:
<?php
$album_name1=$_REQUEST['txtmovie1'];
$album_name2=$_REQUEST['txtmovie2'];
$album_name3=$_REQUEST['txtmovie3'];
$album_name4=$_REQUEST['txtmovie4'];
$album_name5=$_REQUEST['txtmovie5'];
$artist1=$_REQUEST['txtartist1'];
$artist2=$_REQUEST['txtartist2'];
$artist3=$_REQUEST['txtartist3'];
$artist4=$_REQUEST['txtartist4'];
$artist5=$_REQUEST['txtartist5'];
if(isset($_REQUEST['cmdSubmit']))
{
$con = mysql_connect("localhost","root") or die("");
$db=mysql_select_db("omg",$con);
//Movie Poster Upload
//poster1
$target_file="upload/".$_FILES['upsongs1']['name'];
$source_file=$_FILES['upsongs1']['tmp_name'];
if(move_uploaded_file($source_file, $target_file))
{
$poster1=$_FILES['upsongs1']['name'];
}
echo $poster1;
//poster2
$target_file="upload/".$_FILES['upsongs2']['name'];
$source_file=$_FILES['upsongs2']['tmp_name'];
if(move_uploaded_file($source_file, $target_file))
{
$poster2=$_FILES['upsongs2']['name'];
}
echo $poster2;
//poster3
$target_file="upload/".$_FILES['upsongs3']['name'];
$source_file=$_FILES['upsongs3']['tmp_name'];
if(move_uploaded_file($source_file, $target_file))
{
$poster3=$_FILES['upsongs3']['name'];
}
echo $poster3;
//poster4
$target_file="upload/".$_FILES['upsongs4']['name'];
$source_file=$_FILES['upsongs4']['tmp_name'];
if(move_uploaded_file($source_file, $target_file))
{
$poster4=$_FILES['upsongs4']['name'];
}
echo $poster4;
//poster5
$target_file="upload/".$_FILES['upsongs5']['name'];
$source_file=$_FILES['upsongs5']['tmp_name'];
if(move_uploaded_file($source_file, $target_file))
{
$poster5=$_FILES['upsongs5']['name'];
}
echo $poster5;
//Song Upload
//song1 Upload
$targets1_file="song/".$_FILES['txts1']['name'];
$sources1_file=$_FILES['txts1']['tmp_name'];
if(move_uploaded_file($sources1_file, $targets1_file))
{ $s1=$_FILES['txts1']['name'];
}
//song2 Upload
$targets2_file="song/".$_FILES['txts2']['name'];
$sources2_file=$_FILES['txts2']['tmp_name'];
if(move_uploaded_file($sources2_file, $targets2_file))
{ $s2=$_FILES['txts2']['name'];
}
//song3 Upload
$targets3_file="song/".$_FILES['txts3']['name'];
$sources3_file=$_FILES['txts3']['tmp_name'];
if(move_uploaded_file($sources3_file, $targets3_file))
{ $s3=$_FILES['txts3']['name'];
}
//song4 Upload
$targets4_file="song/".$_FILES['txts4']['name'];
$sources4_file=$_FILES['txts4']['tmp_name'];
if(move_uploaded_file($sources4_file, $targets4_file))
{ $s4=$_FILES['txts4']['name'];
}
//song5 Upload
$targets5_file="song/".$_FILES['txts5']['name'];
$sources5_file=$_FILES['txts5']['tmp_name'];
if(move_uploaded_file($sources5_file, $targets5_file))
{ $s5=$_FILES['txts5']['name'];
}
$qry1=("INSERT INTO music(artist_name, album_name, song_name ) VALUES('$artist1','$album_name1', '$s1'),
VALUES('$artist1','$album_name2', '$s2'),('$artist1','$album_name3', '$s3'), ('$artist1','$album_name4', '$s4'),('$artist1','$album_name5', '$s5')");
$qry2=("INSERT INTO album(album_name, poster)
VALUES('$album_name1','$poster1'),
('$album_name2','$poster2'),
('$album_name3','$poster3'),
('$album_name4','$poster4'),
('$album_name5','$poster5')");
?>
<form name="form1" method="POST" action="admin_new2.php" enctype="multipart/form-data" id="main" onSubmit="return check();">
<table>
<thead><tr>
<th>Song</th>
<th>Movie/Album</th>
<th>Artist</th>
<th>Poster</th>
</tr></thead><tbody>
<tr >
<td>
<input type="file" name="txts1">
<input type="file" name="txts2">
<input type="file" name="txts3">
<input type="file" name="txts4">
<input type="file" name="txts5">
</td>
<td>
<input type="text" name="txtmovie1">
<input type="text" name="txtmovie2">
<input type="text" name="txtmovie3">
<input type="text" name="txtmovie4">
<input type="text" name="txtmovie5">
</td>
<td id="artist-clm">
<input type="text" name="txtartist1">
<input type="text" name="txtartist2">
<input type="text" name="txtartist3">
<input type="text" name="txtartist4">
<input type="text" name="txtartist5">
</td>
<td id="poster">
<input type="file" name="upsongs1">
<input type="file" name="upsongs2">
<input type="file" name="upsongs3">
<input type="file" name="upsongs4">
<input type="file" name="upsongs5">
</td></tr>
</tbody></table>
<center><input type="submit" name="cmdSubmit" value="submit"></center>
</form>
答案 0 :(得分:0)
基本上,这是因为当您使用一个查询插入多个记录时,不得重复VALUES关键字。
INSERT INTO music(artist_name, album_name, song_name ) VALUES('$artist1','$album_name1', '$s1'),
('$artist1','$album_name2', '$s2'),
('$artist1','$album_name3', '$s3'),
('$artist1','$album_name4', '$s4'),
('$artist1','$album_name5', '$s5')
然后,为了您的下一步,我知道您是初学者,但您的代码未经过优化。而是重复所有内容4次,查看循环或将数据存储到数组中:)
这将减少您的代码大小并保持更容易。
答案 1 :(得分:0)
您可以循环和数组一次插入多个记录。
<?php
if(isset($_REQUEST['cmdSubmit']))
{
$con = mysql_connect("localhost","root") or die("");
$db=mysql_select_db("omg",$con);
//Movie Poster Upload
$s1=$poster1=$artist1=$album_name1 = '';
for($i=1;$i<=5;$i++)
{
$artist1=$_REQUEST['txtartist'.$i];
$album_name1=$_REQUEST['txtmovie'.$i];
$target_file="upload/".$_FILES['upsongs'.$i]['name'];
$source_file=$_FILES['upsongs'.$i]['tmp_name'];
if(move_uploaded_file($source_file, $target_file))
{
$poster1=$_FILES['upsongs'.$i]['name'];
}
echo $poster1;
$targets1_file="song/".$_FILES['txts'.$i]['name'];
$sources1_file=$_FILES['txts'.$i]['tmp_name'];
if(move_uploaded_file($sources1_file, $targets1_file))
{ $s1=$_FILES['txts'.$i]['name'];
}
$query .= "('$artist1','$album_name1', '$s1'),";
$query1 .= "('$album_name1','$poster1'),";
}
$qry1=("INSERT INTO music(artist_name, album_name, song_name ) VALUES '".$query."'");
$qry2=("INSERT INTO album(album_name, poster)VALUES('".$query1."'");
}
?>
像这样你可以插入多个记录。
答案 2 :(得分:-1)
$targets1_file="song/".$_FILES['txts1']['name'];
$sources1_file=$_FILES['txts1']['tmp_name'];
if(move_uploaded_file($sources1_file, $targets1_file))
{ $s1=$_FILES['txts1']['name'];
}