我对php有问题。
问题是:
如果在看到代码之前,您可能看到两个在不同表中插入的查询函数,那么第一个查询插入的问题是没有任何问题,但是第二个查询不工作,我不知道为什么请帮助我。
在第一个查询中,我向表中插入新闻的数据,而第二个查询则需要插入图片为读者提供更多的细节。
这是代码:
<?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>发布于 2015-12-16 16:26:14
对于第二个查询,请转义列名:
$sqll = "INSERT INTO pic(`name`,`type`,`content`,`for`) VALUES('$name','$type','$path','$id')";因为诸如for等名称都是保留的mysql关键字,因此显然在这方面存在错误。
https://stackoverflow.com/questions/34317001
复制相似问题