Php 将查询插入MYSQL数据库
我是php新手,所以我一直在尝试制作一个php页面,向Mysql数据库添加信息。但当我在表单上按submit时,它会指向php页面,但什么也没发生。空白页 这是我到目前为止所拥有的 形式Php 将查询插入MYSQL数据库,php,html,mysql,sql,forms,Php,Html,Mysql,Sql,Forms,我是php新手,所以我一直在尝试制作一个php页面,向Mysql数据库添加信息。但当我在表单上按submit时,它会指向php页面,但什么也没发生。空白页 这是我到目前为止所拥有的 形式 <table class="table" > <h2>Resturants</h2> <tbody> <form data-toggle="validator" enctype="multipart/form-data" action="ed
<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>
您的
$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";
}
===========================================[HTML的开始]============================
|
餐厅
标题:
正文:
链接:
照片:
==================================[HTML结束]========================
===========================================================
===============================[PHP的开始]=======================
我阅读了您的代码并尝试运行它。试试这个代码。它会帮助你的
1) connection.php
2) form.php
请不要使用mysql.*
函数。那么我应该使用什么?请使用MySQLi或PDO。我有一个基于mysql的完整网站!我怎样才能改变这一切呢?更多信息请访问此网站:我尝试过,但仍然没有任何效果!它将直接指向.php页面并停留在那里!甚至没有任何东西添加到我迁移到的数据库中Mysqli@MuaathAli我简化了你的代码。现在,它将检查图像是否已上载,而不是检查图像是否为空。希望这有帮助!代码工作得很好!非常感谢。但是标题('Location:cms.php')根本不起作用。不仅仅是在这方面。在我所有的php中files@MuaathAli看看这个
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";
}
==========================[START OF HTML]============================
|<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" name="submit"></td>
</tr>
</form>
</tbody>
</table>
=====================[END OF HTML]========================
===========================================================
======================[START OF PHP]=======================
<?php
include('connection.php');
session_start();
$title=$_POST['title'];
$text=$_POST['text'];
$link=$_POST['link'];
$image=$_FILES['image'];
$imagename=$image['name'];
$submit=$_FILES['submit'];
if(isset($_POST['submit'])){
if(empty($imagename))
{
$imagename="defualt.jpg";
}
if(!empty($title) && !empty($text))
{
$query=mysql_query('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');
}
?>
=========================[END OF PHP ]=============================
I read your code and try to run it. try this code. It will help you
1) connection.php
<?php
$con = mysqli_connect("localhost","root","","resturant");
?>
2) form.php
<?php
session_start();
include('connection.php');
if($_POST['submit']){
echo $title=$_POST['title']; echo "<br>";
echo $text=$_POST['text']; echo "<br>";
echo $link=$_POST['link']; echo "<br>";
echo $image=$_FILES['image']['name']; echo "<br>";
if(!empty($title) && !empty($text) && !empty($image))
{
$sql = "INSERT INTO resturant(Title, ImagePath, Text, Link, Date) VALUES ('$title','$image','$text','$link',CURRENT_DATE())";
$query = mysqli_query($con,$sql) or die(mysql_error());
$path="NewsImages/$image";
move_uploaded_file($_FILES['image']['tmp_name'],$path);
$_SESSION['status']="Successful";
}
else
{
$_SESSION['status']="Please check all fields";
}
header('Location:cms.php');
}
?>
<table class="table">
<h2>Resturants</h2>
<tbody>
<form data-toggle="validator" enctype="multipart/form-data" action="" 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;" name="submit"></td>
</tr>
</form>
</tbody>
</table>
3) cms.php
<?php
session_start();
echo $_SESSION['status'];