MySQL查询无法使用php,但查询在phpmyadmin的sql上运行

时间:2016-08-04 08:20:36

标签: php mysql

所以,这是我正在使用的HTML表单

<form action="upload.php" method="post" enctype="multipart/form-data">
<table border="2px">
<tr>
<td>Date</td>
<td>Day</td>
<td>Height</td>
<td>Weight</td>
<td>Biceps(Open)</td>
<td>Biceps(Curled)</td>
<td>Photo</td>
<td>Action</td>
</tr>
<tr>
<td><input type="date" name = "date"></td>
<td><select name="day">
<?php for ($x = 1; $x <= 100; $x++) { ?>
    <option>
    <?php echo $x;
} ?></option>
    </select>
</td>
<td><select name="height">
    <?php for ($x = 170; $x <= 180; $x++) { ?>
    <option>
    <?php echo $x;
} ?></option>
    </select> Centimeters &nbsp;
</td>
<td><select name="weight">
    <?php for ($x = 60; $x <= 80; $x++) { ?>
    <option>
    <?php echo $x;
} ?></option>
    </select> Kilograms &nbsp;
</td>
<td><select name="biceps_o">
    <?php for ($x = 10; $x <= 15; $x++) { ?>
    <option>
    <?php echo $x;
} ?></option>
    </select> Inches &nbsp;
</td>
<td><select name="biceps_c">
    <?php for ($x = 10; $x <= 15; $x++) { ?>
    <option>
    <?php echo $x;
} ?></option>
    </select> Inches &nbsp;
</td>
    Select image to upload:
<td><input type="file" name="fileToUpload" id="fileToUpload"></td>
<td><input type="submit" value="Add" name="submit"></td>
</tr>
</table>
</form>

这是upload.php文件

<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
    $date = $_POST['date'];
    $day = $_POST['day'];
    $height = $_POST['height'];
    $weight = $_POST['weight'];
    $biceps_o = $_POST['biceps_o'];
    $biceps_c = $_POST['biceps_c'];

    echo $sql_query = "INSERT INTO gym (date, day, height, weight, biceps_o, biceps_c, image) VALUES('$date', '$day', '$height', '$weight', '$biceps_o', '$biceps_c', '$target_file')";
    if($sql_result = mysql_query($sql_query))
    echo "Data Successfully Added!";
    else
    echo "Data Entry Failed!";
?>

但是当我运行代码并上传我的数据时,它会提供以下内容

  

文件是图像 - image / jpeg。文件   aid62387-728px-Cross-Your-Eyes-Step-2.jpg已上传.INSERT   INTO健身房(日期,日期,身高,体重,biceps_o,biceps_c,图像)   价值观(&#39; 2016-08-15&#39;,&#39; 1&#39;,&#39; 170&#39;,&#39; 60&#39;,&#39; 10&#39; ,&#39; 10&#39;,   &#39; images / aid62387-728px-Cross-Your-Eyes-Step-2.jpg&#39;)数据输入失败!

好吧,图像上传得很完美,但查询运行不正常。 但是相同的查询

  

INSERT INTO健身房(日期,日期,身高,体重,biceps_o,biceps_c,图像)   价值观(&#39; 2016-08-15&#39;,&#39; 1&#39;,&#39; 170&#39;,&#39; 60&#39;,&#39; 10&#39; ,&#39; 10&#39;,   &#39;影像/ aid62387-728px交你-眼睛步-2.JPG&#39)

当我在phpmyadmin的sql中运行它时,

完全正常。可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我认为没有包括你的数据库连接,比如php

答案 1 :(得分:-1)

您正在使用保留的mysql关键字(日期)。引用查询如下:

$sql_query = "INSERT INTO gym (`date`, `day`, `height`, `weight`, `biceps_o`, `biceps_c`, `image`) VALUES('$date', '$day', '$height', '$weight', '$biceps_o', '$biceps_c', '$target_file')";

并在值周围添加一些转义(如mysql_real_escape_string)或开始使用PDO。