i'm newbie, i gotta problems, i cannot update table upload since i was insert foreign key from table request in there anyone can help me?
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;
if i wanna create new request, it will create record in table request, insert the data to table request, but also insert id_request in table upload, i fill in with last_insert_id
in the same time, after i was insert in开发者_JS百科to id_request in table upload, i was insert attachment using query to by insert or update query but there always error
the error was looking like this :
Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\wamp\www\beta\insert.php on line 32
Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\wamp\www\beta\insert.php on line 32
this is line 32
mysql_query($up)or die('Error upload file');
this is my listing code
<?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";
}
?>
first of all, remember to sanitize your data! Your code is vulnerable to SQL injection. To get the insert id, you do this
$sql = "INSERT INTO ...";
$result = mysql_query($sql);
$new_row_id = mysql_insert_id();
In your code, you do not open a mysql connection, choose a db, etc. If you're not doing that in the included config.php file, then that's causing the error.
EDIT:
I couldn't really understand your script since it seems to be in another language, but this is what I came up with
<?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";
}
?>
精彩评论