简体   繁体   English

从php脚本到mysql的插入语句:数据库未读取插入语句

[英]Insert statement from php script to mysql: Database not reading insert statement

So everything up to the insert statement works perfectly. 因此,直到插入语句为止的所有内容都可以正常运行。 I know the database is connecting because I can select information from the database with the first two statements. 我知道数据库正在连接,因为我可以使用前两个语句从数据库中选择信息。 I also know that the execute_statment3 works because no errors are being printed off and when it is put into the sql the statement is inserted the way it should be. 我也知道execute_statment3可以正常工作,因为没有错误被打印出来,当将其放入sql中时,该语句将按应有的方式插入。 Therefore the problem lies somewhere with the communication between the script and phpmyadmin. 因此,问题出在脚本与phpmyadmin之间的通信中。 Please help I have been staring at this problem for two days and am going rather crazy. 请帮助我盯着这个问题已经两天了,而且我会发疯。

<?php

session_start();

$hostname = 'localhost';
$username = '####';
$password = '####';


$connection = mysql_connect($hostname, $username, $password) 
or die ('Connection error!!!');


$database = '####';
mysql_select_db($database);  


$uid = $_SESSION['ID'];  
$album = $_POST['albumname'];  
$description = $_POST['description'];  
$filename = $_FILES["upload_file"]["name"];  
$filetype = $_FILES["upload_file"]["type"];
$filesize = $_FILES["upload_file"]["size"];
$file_on_server = $_FILES["upload_file"]["tmp_name"];  


if ($filetype == "image/jpeg") {  
    $file_copy_name = date(m.d.y_H.i.s) . ".jpg";  
        copy($file_on_server, "uploads/" . $file_copy_name);  


    print "<br>";  
    print "<img src = \"uploads/$file_copy_name\">";    


    print "<br>";  
    $ret = system("pwd");  


    $picture = "uploads/$file_copy_name";  
}


$execute_statement = "SELECT * FROM ImageAlbums WHERE Album = '$album'";


$results = mysql_query($execute_statement) or die ('Error executing SQL statement!!!');


while($item = mysql_fetch_array($results))


{ 
  $album2 = $item['Album'];
}

if ($album2 == $album)


{
    $execute_statement2 = "SELECT * FROM ImageAlbums WHERE Album = '$album'";


    $results2 = mysql_query($execute_statement2) or die ('Error executing SQL statement2!!!');


        while ($row2 = mysql_fetch_array($results2)) {


        $AID = $row2["AlbumID"];
        }


    $execute_statement3 = "INSERT INTO Images (`ImageID`, `AlbumID`, `Description`, `Extensions`) VALUES ('NULL', '$AID', '$description', '$file_copy_name')";      


    ($execute_statement3) or die ('Error executing SQL statement3!!!');

}


print "<br>";
print "<br>";
print $execute_statement3;
print "<br>";
print "<br>";
print $AID;
print "<br>";
print "<br>";
print $picture;


?>

I am using two databases for this script one of the databases is called ImageAlbums and has two columns called AlbumID and Album (AlbumID being a primary key). 我为此脚本使用了两个数据库,其中一个数据库称为ImageAlbums,并且有两列称为AlbumID和Album(AlbumID是主键)。 The second table is called Images and has four columns ImageID (primary key), AlbumID (foreign key), Description and Extensions. 第二个表称为图像,并具有四列ImageID(主键),AlbumID(外键),描述和扩展名。

You are not running the statement 您没有运行该语句

($execute_statement3) or die ('Error executing SQL statement3!!!');

Try: 尝试:

mysql_query($execute_statement3);

Also, make sure you escape all the variables. 另外,请确保您转义了所有变量。

make sure that the user you are connecting with in the php script has privileges for insert statements. 确保您在php脚本中与之连接的用户具有插入语句的特权。 you could be using a db user with only select privs... 您可能只使用具有选择特权的数据库用户...

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

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