我是新手,我有问题, 我无法更新表上传,因为我是从那里的表请求插入外键 有人可以帮帮我吗?
table request:
id_request; Primary Key
subjek;
email;
reportto;
pelaksana;
isi;
table upload:
id_upload; Primary Key
id_request; Foreign Key
filename;
filetype;
filesize;
filedata;
如果我想创建新请求,它将在表请求中创建记录,将数据插入表请求,还在表上传中插入id_request,我用last_insert_id填写
同时,我在表上传中插入id_request后,我通过插入或更新查询使用查询插入附件 但总有错误
错误看起来像这样:
警告:mysql_query()[function.mysql-query]:MySQL服务器在第32行的C:\ wamp \ www \ beta \ insert.php消失了
警告:mysql_query()[function.mysql-query]:在第32行的C:\ wamp \ www \ beta \ insert.php中读取结果集的标题时出错
这是第32行
mysql_query($up)or die('Error upload file');
这是我的上市代码
<?php
include('config.php');
session_start();
$jenis = $_POST['jenis'];
$subjek = $_POST['subject'];
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$reportto = $_SESSION['reportto'];
$pelaksana = $_POST['pelaksana'];
$ket = $_POST['isi'];
$uploaddir = 'attach/';
$filedata = addslashes(fread(fopen($_FILES['uploadfile']['tmp_name'], 'r'),
$_FILES['uploadfile']['size']));
$filetype = $_FILES['uploadfile']['type'];
$filesize = $_FILES['uploadfile']['size'];
$filename = $_FILES['uploadfile']['name'];
$query = "INSERT INTO request (waktu, jenis_request, subject, customer, isi, pelaksana)
VALUES (NOW(), '".$jenis."', '".$subjek."', '".$username."', '".$ket."', '".$pelaksana."')";
mysql_query($query)or die('Error, insert query failed');
$ff = mysql_query("insert into upload (id_request) select id_request from request where id_request = LAST_INSERT_ID()");
$up = "update upload set deskripsi = '".$subjek."' , filetype = '".$filetype."', filename = '".$filename."', filedata = '".$filedata."', filesize = '".$filesize."' where id_request = last_insert_id()";
mysql_query($up)or die('Error upload file');
$uploadfile = $uploaddir . $filename;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploadfile))
{
echo "File telah diupload\n";
echo '$filename\n';
// header("location: home.php");
}
else
{
echo "File gagal diupload";
}
?>
答案 0 :(得分:0)
首先,请记住清理数据!您的代码容易受到SQL注入攻击。要获取插入ID,请执行此操作
$sql = "INSERT INTO ...";
$result = mysql_query($sql);
$new_row_id = mysql_insert_id();
在你的代码中,你不打开mysql连接,选择数据库等。如果你没有在包含的config.php文件中这样做,那就是导致错误。
修改强>:
我无法真正理解你的剧本,因为它似乎是用另一种语言,但这就是我想出来的
<?php
include('config.php');
session_start();
//filter the data
$jenis = filter_var($_POST['jenis'], FILTER_SANITIZE_STRING);
$subjek = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$pelaksana = filter_var($_POST['pelaksana'], FILTER_SANITIZE_STRING);
$ket = filter_var($_POST['isi'], FILTER_SANITIZE_STRING);
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$reportto = $_SESSION['reportto'];
$uploaddir = 'attach/';
$filedata = addslashes(fread(fopen($_FILES['uploadfile']['tmp_name'], 'r'),
$_FILES['uploadfile']['size']));
$filetype = $_FILES['uploadfile']['type'];
$filesize = $_FILES['uploadfile']['size'];
$filename = $_FILES['uploadfile']['name'];
$sql = "INSERT INTO request (waktu, jenis_request, subject, customer, isi, pelaksana) VALUES (NOW(), '{$jenis}', '{$subjek}', '{$username}', '{$ket}', '{$pelaksana}')";
$result = mysql_query($sql);
$id_inserted_request = mysql_insert_id();
$sql = mysql_query("INSERT INTO upload (deskripsi, filetype, filename, filedata, filesize, id_request) VALUES('{$subjek}', '{$filetype}', '{$filename}', '{$filedata}', '{$filedata}', '{$filesize}', '{$id_inserted_request}') ");
mysql_query($sql)
$uploadfile = $uploaddir . $filename;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploadfile))
{
echo "File telah diupload\n";
echo "{$filename}\n";
// header("location: home.php");
}
else
{
echo "File gagal diupload";
}
?>