php - 插入查询到MYSQL数据库

标签 php html mysql sql forms

我是 php 的新手,所以我一直在尝试制作一个 php 页面,将信息添加到 Mysql 数据库。但是,每当我在表单上按提交时,它都会指向 php 页面,但什么也没有发生。空白页。

这是我目前所拥有的。

表格

<table class="table" >
  <h2>Resturants</h2>
  <tbody>
    <form data-toggle="validator" enctype="multipart/form-data" action="editRestaurants.php" method="post">
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Title :</label></td>
        <td><input type="text" class="form-control" id="title" name="title" required></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Text :</label></td> 
        <td><textarea style="width:300px;height:100px" class="form-control" id="text" name="text" required></textarea><br/></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Link :</label></td> 
        <td><input type="text" class="form-control" id="link" name="link" required></td>
      </tr>
      <tr>
        <td><label for="inputName" class="control-label" style="font-size:17px;">Photo:</label></td>
        <td><input type="file" id="image" class="form-control" name="image" accept="image/jpeg" required></td>
      </tr>
      <tr>
        <td><input type="submit" value="Submit" style="font-size:17px;" id="submit"></td>
      </tr>
    </form>
  </tbody>
</table>

这是php文件

include('connection.php'); 
session_start(); 
$title=$_POST['title']; 
$text=$_POST['text']; 
$link=$_POST['link']; 
$image=$_FILES['image']; 
$imagename=$image['name']; 
if(empty($imagename)) 
{ 
    $imagename="defualt.jpg"; 
} 
if(!empty($title) && !empty($text)) 
{ 
    $query=mysqli_query($GLOBALS["___mysqli_ston"], 'INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,'$title','$imagename','$text','$link',CURRENT_DATE())');


    if(!empty($image)) 
    { 
    $target_dir= "NewsImages/"; 
    $target_file = $target_dir . basename($imagename); 
    move_uploaded_file($image["tmp_name"], $target_file); 

    } 

    $_SESSION['status']="Successful"; 
} 
else 
{ 
    $_SESSION['status']="Please check all fields"; 

} 

header('Location:cms.php'); 

最佳答案

您的$query 存在语法错误。以下代码将起作用:

$query=mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,'$title','$imagename','$text','$link',CURRENT_DATE())");

我已经简化了您的代码。现在,它将在重定向到 cms.php 之前检查文件是否已成功上传。

if(empty($imagename)) { 
    $imagename="defualt.jpg";
}

if(!empty($title) && !empty($text)) {
    $query=mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `resturant`(`ID`, `Title`, `ImagePath`, `Text`, `Link`, `Date`) VALUES (NULL,'$title','$imagename','$text','$link',CURRENT_DATE())");

    $target_dir= "NewsImages/"; 
    $target_file = $target_dir . basename($imagename);

    if(move_uploaded_file($image["tmp_name"], $target_file)) {
        $_SESSION['status']="Successful";
        header('Location:cms.php');
        exit;
    } else {
        $_SESSION['status']="Something went wrong while uploading the image.";
    }

} else { 
    $_SESSION['status']="Please check all fields";
}

关于php - 插入查询到MYSQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015307/

相关文章:

php - 具有自动递增功能的用户可格式化 ID

javascript - 通过位置验证用户 JAVASCRIPT

php - 从 MySQL 获取 PHP 变量时出现问题

php - php 中针对连接查询返回 SQL 错误

php mysql搜索脚本,如何使匹配结果加粗

jquery - 如何将下面的JS转换为JQuery?

javascript - 通过 HTML 和 Javascript 进行音频流和录音

php - Laravel - 如何在查询中使用 session 用户名

mysql 左连接,按计数分组

c# - 将二进制数据从 C# 上传到 PHP