gpt4 book ai didi

php - INSERT INTO 不插入 PHP/MySQL

转载 作者:行者123 更新时间:2023-11-30 00:38:23 25 4
gpt4 key购买 nike

我有在数据库中插入数据的表单,但没有插入任何内容。

上传图像部分工作正常。

当代码仅在表单中输入图像时,代码可以正常工作,但是在我为其他输入添加变量定义后,代码停止工作。

任何帮助将不胜感激,谢谢。

表格:

<form method="post" name="form1" class="insert_form" action="insert-workshop-form.php"  enctype="multipart/form-data">


<table cellspacing="15px" >
<tr valign="baseline">
<td nowrap><label for="event_title">Title</label></td>
<td><input type="text" name="event_title" id="event_title" value="" placeholder="enter title" size="50"></td>
</tr>
<tr valign="baseline">
<td nowrap valign="top"><label for="event_text">Event Details</label></td>
<td><textarea name="event_text" cols="50" rows="5" id="event_text"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap><label for="event_date">Date</label></td>
<td><input type="date" name="event_date" id="event_date" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap ><label for="event_location">Location</label></td>
<td><input type="text" name="event_location" id="event_location" value="" size="60"></td>
</tr>
<tr valign="baseline">
<td nowrap valign="top"><label for="events_notes">Notes</label></td>
<td><textarea name="events_notes" id="events_notes" cols="50" rows="5"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap valign="top"><label for="event_additional_details">Additional<br>
Details</label></td>
<td><textarea name="event_additional_details" id="event_additional_details" cols="50" rows="5"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap ><label for="event_img">Upload Image</label></td>
<td><input type="file" name="event_img" id="event_img" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap >&nbsp;</td>
<td align="right"><input type="submit" value="Post" id="insert_btn"></td>
</tr>
</table>
</form>

执行代码:

<?php


$allowedExts = array("gif","jpeg","pjpeg", "jpg", "png","JIF","JPG","JPEG","PNG","PJPEG","X-PNG","x-png");
$temp = explode(".", $_FILES["event_img"]["name"]);
$extension = end($temp);
if (

(($_FILES["event_img"]["type"] == "image/gif")
|| ($_FILES["event_img"]["type"] == "image/jpeg")
|| ($_FILES["event_img"]["type"] == "image/jpg")
|| ($_FILES["event_img"]["type"] == "image/pjpeg")
|| ($_FILES["event_img"]["type"] == "image/png")
|| ($_FILES["event_img"]["type"] == "image/x-png")

|| ($_FILES["event_img"]["type"] == "image/PNG")
|| ($_FILES["event_img"]["type"] == "image/X-PNG")
|| ($_FILES["event_img"]["type"] == "image/GIF")
|| ($_FILES["event_img"]["type"] == "image/JPEG")
|| ($_FILES["event_img"]["type"] == "image/JPG")
|| ($_FILES["event_img"]["type"] == "image/PJPEG"))

&& ($_FILES["event_img"]["size"] < 9999999)

&& in_array($extension, $allowedExts))



{
if ($_FILES["event_img"]["error"] > 0)
{
echo "Return Code: " . $_FILES["event_img"]["error"] . "<br>";
}
else
{


if (file_exists("../images/" . $_FILES["event_img"]["name"]))
{
echo $_FILES["event_img"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["event_img"]["tmp_name"],
"../images/" . $_FILES["event_img"]["name"]);



require_once('../Connections/bmer_conn.php');
$event_title=$_POST['event_title'];
$event_text=$_POST['event_text'];
$event_date=$_POST['event_date'];
$event_location=$_POST['event_location'];
$event_notes=$_POST['event_notes'];
$event_additional_details=$_POST['event_additional_details'];


$event_img=($_FILES['event_img']['name']);




$insert=mysql_query("INSERT INTO workshop(event_title,event_text,event_date,event_location,event_notes,event_additional_details,event_img) VALUES ('$event_title','$event_text','$event_date','$event_location','$event_notes','$event_additional_details','$event_img')");


header("location:../workshop.php");

if($insert){
echo 'data inserted';}
else
{
echo 'data not inserted';}

}
}
}
else
{
echo "Invalid image ";
}



?>

最佳答案

首先:不要再使用 mysql_* 函数,它们已被弃用,并将在不久的将来版本中删除(或者它们已经被删除了吗?)

其次,您可以使用 PHP in_array 使 if 子句更具可读性。功能。

第三,您应该使用(再次弃用的)函数 mysql_errno(和 mysql_error)检查 MySQL 错误代码,以查看是否存在 MySQL 错误。

我无法准确判断您的错误出在哪里,但请尝试将您的查询修改为:

$insert = mysql_query("INSERT INTO workshop(event_title,event_text,event_date,event_location,event_notes,event_additional_details,event_img) VALUES ('{$event_title}','{$event_text}','{$event_date}','{$event_location}','{$event_notes}','{$event_additional_details}','{$event_img}')");

这样,PHP 将大括号之间的所有内容都视为变量名。或者,您可以使用字符串连接,但这是设计选择的问题。

关于php - INSERT INTO 不插入 PHP/MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com