我为此浏览了无数不同的帮助菜单和主题,但仍然遇到问题。我只是想将上传图像的文件路径插入 MySQL 数据库。我尝试将图像传递给变量,然后使用查询将其推送到数据库,但它不起作用。我的代码在下面,表单在上面,php 在下面:
<html>
<body>
<h1>test</h1>
<form action="insert.php" method="post" enctype="multipart/form-data">
Name <input type="text" name="name" /><br><br>
Description <input type="text" name="desc" /><br><br>
Price Low<input type="text" name="price_low" /><br><br>
Price High <input type="text" name="price_high" /><br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
3.Send this file: <input name="userfile" type="file" />
4.<input type="submit" value="Send File" /
<input type="submit" />
</form>
a
</body>
</html>
<html>
<?php
//upload image
$uploaddir = '';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
//end of upload image
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
$con = mysql_connect("localhost","admintest","gen");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test2", $con);
$sql="INSERT INTO products (name, description, price_low, price_high)
VALUES
('$_POST[name]','$_POST[desc]','$_POST[price_low]','$_POST[price_high]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>