dongsheng9203 2015-12-16 16:19
浏览 20
已采纳

我在个人主页功能方面需要帮助


I have a problem with php.
The problem:
If you saw the code before probably you see two query function that inserting in a different tables, the problem that the first query is inserting without any problem, but the second query doesn't work, I don't know why please help me.
In the first query I insert into a table a data for a news and the second query I need to insert a picture to give more details for the readers.
This is the code:

<?php
    ob_start();
    session_start();
    include("../includes/config.php");
    if(!$_SESSION['admin']){
        header("Location: index.php");
    }
?>
<html>
<body align="center">
<?php
    if(isset($_POST['add'])){
        $title = $_POST['title'];
        $topic = $_POST['topic'];
        if(empty($topic) || empty($title)){
            echo"please don't leave the feilds empty";
        }else{
            //file vars
            $url = $_FILES["upload"]["tmp_name"];
            $path = mysqli_real_escape_string($connect,file_get_contents($url));
            $name = mysqli_real_escape_string($connect,$_FILES["upload"]["name"]);
            $type = mysqli_real_escape_string($connect,$_FILES["upload"]["type"]);
            if(substr($type,0,5) == "image"){
                $sql = "INSERT INTO news(title,topic) VALUES('$title','$topic')";
                $query = mysqli_query($connect,$sql);
                $id = mysqli_insert_id($connect);
                $sqll = "INSERT INTO pic(name,type,content,for) VALUES('$name','$type','$path','$id')";
                $queryy = mysqli_query($connect,$sqll);
                if($query && $queryy){
                    echo"Worked";
                }else{
                    echo"error";
                }
            }else{
                echo"it's not an image";
                echo $type;
            }
        }
    }
?>
<form method="post" action="addnew.php" enctype="multipart/form-data">
    <input type="text" name="title" style="text-align:center" placeholder="title"/><br/>
    <textarea name="topic" placeholder="topic" style="width:800px;height:500px;resize:none;text-align:right;font-size:23">
    </textarea>
    <br/>
    <input type="file" name="upload"/><br/>
    <input type="submit" name="add" value="sed">
</form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dsfhd78978 2015-12-16 16:26
    关注

    For the second query please escape column names:

    $sqll = "INSERT INTO pic(`name`,`type`,`content`,`for`) VALUES('$name','$type','$path','$id')";
    

    because such names as for etc. are reserved mysql keywords and you are obviously getting error on this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法