passing variable in functions php
i have this code where i insert post details to mysql and another function
to add photo details to mysql. how can i get the last insert id of the
post to insert into the photos table as post_id
function add_img($whichimg,$title)
{
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "INSERT INTO photos (post_id,title, src) VALUES
('$this->$postid','$title','$whichimg')";
$add_to_db = $conn->query($sql) or die(mysqli_error());
return $add_to_db;
}
function add_post($subject,$content)
{
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "INSERT INTO posts (subject, content) VALUES
('$subject','$content')";
$add_to_db = $conn->query($sql) or die(mysqli_error());
$postid=$mysqli->insert_id;
}
add_post is called first.. add_img is called several times afterwards
depending on the number of photos
Is there a way to call thi $id in this function onto another ?
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = $this->get_server_var('HTTP_CONTENT_DISPOSITION') ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$this->get_server_var('HTTP_CONTENT_DISPOSITION')
)) : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
preg_split('/[^0-9]+/',
$this->get_server_var('HTTP_CONTENT_RANGE')) : null;
$size = $content_range ? $content_range[3] : null;
$files = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
$subject = $this->handle_form_subject($file, $index);
$content = $this->handle_form_content($file, $index);
$id=$this->add_post($subject,$content);
This is the part where i want to be able to relate to the previous $id
protected function handle_file_upload($uploaded_file, $name, $size, $type,
$error,
$index = null, $content_range = null) {
$file = new stdClass();
$file->name = $this->get_file_name($name, $type, $index, $content_range);
$file->size = $this->fix_integer_overflow(intval($size));
$file->type = $type;
if ($this->validate($uploaded_file, $file, $error, $index)) {
$title = $this->handle_form_title($file, $index);
//Add the call to the database to add the data. Notice the three
variables
//which are passed to the add_img function.
$this->add_img($id,$file->name,$title);
At the moment that id is not recognized
No comments:
Post a Comment