PHP文章和图片问题

时间:2013-12-13 14:40:25

标签: php

这仍然无效,所以我现在在这里发布完整的代码......

<form id="contact-form" action="fileovi/dodaj_novost.php" method="post">
          <fieldset>
            <div class="coll-1">
              <div class="txt-form">Naslov[hr]</div>
              <label class="name">
                <input type="text" name="naslov_hr">
                <br>
            </div>
            <div class="clear"></div>
            <div class="coll-1">
              <div class="txt-form">Naslov[en]</div>
              <label class="name">
                <input type="text" name="naslov_en">
                <br>
            </div>
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="coll-1">
              <div class="txt-form">Naslov[de]</div>
              <label class="name">
                <input type="text" name="naslov_de">
                <br>
            </div>
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="coll-1">
              <div class="txt-form">Link slike</div>
              <label class="name">
                <input type="file" name="image[]" enctype="multipart/form-data"/><br />
                <input type="file" name="image[]" enctype="multipart/form-data"/><br />
              <br>
            </div>
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="coll-big">
              <div class="txt-form"><center>Tekst[hr]</center></div>
              <label class="name">
                <textarea id="tekst" name="tekst_hr"></textarea>
                <br>
            </div>
            <div class="clear"></div>
            <div class="coll-big">
              <div class="txt-form"><center>Tekst[en]</center></div>
              <label class="name">
                <textarea id="tekst1" name="tekst_en"></textarea>
                <br>
            </div>
            <div class="clear"></div>
            <div class="coll-big">
              <div class="txt-form"><center>Tekst[de]</center></div>
              <label class="name">
                <textarea id="tekst2" name="tekst_de"></textarea>
                <br>
            </div>
            <div class="clear"></div>

            <a href="javascript:;" onclick="javascript: document.getElementById('contact-form') .submit()" class="btn btn-2">Dodaj!</a>

        </form>

这是我创建的php函数......

function dodaj_novost()
{
global $mysqli;
$mysqli->query("SET NAMES utf8");
$mysqli->query("SET CHARACTER SET utf8");
$mysqli->query("SET COLLATION_CONNECTION='utf8_general_ci'");


//sanitize variables

$naslovhr = $_POST['naslov_hr'];
$naslovhr = $mysqli->real_escape_string($naslovhr);

$nasloven = $_POST['naslov_en'];
$nasloven = $mysqli->real_escape_string($nasloven);

$naslovde = $_POST['naslov_de'];
$naslovde = $mysqli->real_escape_string($naslovde);

$teksthr = $_POST['tekst_hr'];
$teksthr = $mysqli->real_escape_string($teksthr);

$teksten = $_POST['tekst_en'];
$teksten = $mysqli->real_escape_string($teksten);

$tekstde = $_POST['tekst_de'];
$tekstde = $mysqli->real_escape_string($tekstde);


//sanitize variables END

$dan = date('d');
$mjesec = date('M');
$godina = date('Y');

$sql="INSERT INTO novosti (naslovhr, nasloven, naslovde, teksthr, teksten, tekstde, dan, mjesec, godina) VALUES ($naslovhr,$nasloven, $naslovde,$teksthr,$teksten,$tekstde,$dan,$mjesec,$godina)";
$query = $mysqli->query("$sql");


//Add picture!

    $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
    $max_size = 2048 * 1024; // max file size (200kb)
    $path = 'uploads/'; // upload directory

    if(isset($_FILES['image'])){
        for($i=0; $i<count($_FILES['image']['name']); $i++){
            if( @is_uploaded_file($_FILES['image']['tmp_name'][$i]) )
                {
                    // get uploaded file extension
                    $ext = strtolower(pathinfo($_FILES['image']['name'][$i], PATHINFO_EXTENSION));
                    // looking for format and size validity
                    if (in_array($ext, $valid_exts) AND $_FILES['image']['size'][$i] < $max_size)
                    {
                        // unique file path
                        $filename = uniqid(). '.' .$ext;
                        // move uploaded file from temp to uploads directory
                        if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $path.$filename))
                        {
                            $status = $path.$filename;
                            $link = 'http://'.$domena.'/'.$path.'/'.$filename;
                            $upit = "INSERT INTO slike_novosti (link, id_posta) VALUES ($link, $id_posta)";
                            $upit = $mysqli->query("$upit");
                            if ($upit == 'true'){
                                echo 'Successfull!';
                            } else {
                                echo 'Not sucessfull!';
                            }
                        }
                        else {
                            $status = 'Upload Fail: Unknown error occurred!';
                        }
                    }
                    else {
                        $status = 'Upload Fail: Unsupported file format or It is too large to upload!';
                    }
                }
                else{
                        //image is not uploaded!
                        $status = ' ';
                    }

                echo '<br>'.$status.'<br>';
        }
    } else {
        echo 'Nema slike!';
    }

//Add picture END!!

}

由于某些原因,这段代码仍无效,如果有人可以指出我正确的方向如何解决这个问题..我会很开心! :)干杯。

2 个答案:

答案 0 :(得分:1)

起初:

$sql1 = "INSERT INTO slike_novosti (slika, link_slike) VALUES ('$link_slike','$slika')";

检查变量的顺序?我想,一定是:

$sql1 = "INSERT INTO slike_novosti (slika, link_slike) VALUES ('$slika','$link_slike')";

第二个:使用PDO组件,你有一个非常糟糕的代码和SQL注入。

答案 1 :(得分:0)

$variables中删除单引号。对于PHP,'$ var'是一个值为$ var的变量。如果你想要它们引用,请使用“$ var”。见variables

所以这将工作

$sql="INSERT INTO novosti (naslovhr, nasloven, naslovde, teksthr, teksten, tekstde, 
         link_slike, dan, mjesec, godina) VALUES  
         $naslovhr,$nasloven,$naslovde,$teksthr,
         $teksten,$tekstde,$link_slike,$dan,$mjesec,$godina)";