【问题标题】:Insert statement from php script to mysql: Database not reading insert statement从 php 脚本向 mysql 插入语句:数据库未读取插入语句
【发布时间】:2011-04-12 03:21:45
【问题描述】:

所以直到插入语句的所有内容都可以完美运行。我知道数据库正在连接,因为我可以使用前两个语句从数据库中选择信息。我也知道 execute_statment3 可以工作,因为没有打印错误,并且当它被放入 sql 时,语句会以应有的方式插入。因此问题出在脚本和 phpmyadmin 之间的通信上。请帮助我已经盯着这个问题两天了,我快疯了。

<?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;


?>

我为此脚本使用了两个数据库,其中一个数据库称为 ImageAlbums,并且有两列称为 AlbumID 和 Album(AlbumID 是主键)。第二个表称为 Images,有四列 ImageID(主键)、AlbumID(外键)、Description 和 Extensions。

【问题讨论】:

    标签: php mysql insert


    【解决方案1】:

    你没有运行语句

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

    试试:

    mysql_query($execute_statement3);
    

    另外,请确保您转义了所有变量。

    【讨论】:

      【解决方案2】:

      确保您在 php 脚本中连接的用户具有插入语句的权限。你可以使用只有选择权限的 db 用户......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多