0

我正在尝试使用 mysqli 将多行插入数据库,但它不起作用......

注意:我的目标是在图像上传成功后将文本和文件字段名称一起插入到数据库中。任何想法?

这是html表单...

<form action="send.php" method="post">
First Name:<input type="text" name="fname" required><br>
Last Name:<input type="text" name="lname" required><br> 
Age:<input type="text" name="age" required><br> 
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

这是我在 send.php 中所拥有的......当我尝试将图像路径插入到数据库时它可以工作,但是当我包含第一个表单中的文本字段名称时它不起作用..

// your save code goes here

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
  }

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
$sub= 1;

$mysqli = new mysqli("localhost", "root", "", "simple_login");

// TODO - Check that connection was successful.

$photo= "images/" . $_FILES["file"]["name"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$age   =$_POST["age"];


$stmt = $mysqli->prepare("INSERT INTO test (photo, Firstname, Lastname, Age) VALUES (?, ?, ?, ?)");

// TODO check that $stmt creation succeeded

// "s" means the database expects a string
$stmt->bind_param("s", $photo, $fname, $lname, $age);

$stmt->execute();

$stmt->close();

$mysqli->close();

echo "<font size='7' color='white'><b> Success! Your photo has been uploaded.</b></font>";
}

}
}
else
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}


?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" />


<?php
ini_set( "display_errors", 0);
if($sub==0)
{
?> 
<label  for="file"><font  size="5"><b>Choose Photo:</b></font></label>
<input id="shiny" type="file" name="file" onchange="file_selected = true;" required>
<input id="shiny" type="submit" value="Upload" name="submit">
<?php
}
?>


</form>
4

2 回答 2

0

我对你的布局有点疑惑。你有两种不同的形式,一种用于文本输入,一种用于图像?

因此,只有图像路径的值被插入。图片已上传,但未设置 $_POST['fname']、$_POST['lname'] 和 $_POST['age'] 的值。

您应该以一种形式制作并提交。

于 2013-01-06T00:42:08.800 回答
0

代替:

$stmt->bind_param("s", $photo, $fname, $lname, $age);

数据库列中的每个文本字段

$stmt->bind_param("ssss", $photo, $fname, $lname, $age);

或者

同一数据库列中的所有文本字段

$stmt->bind_param("s", $photo.','$fname.','.$lname.','.$age);


bind_param ( string $types , mixed &$var1 [, mixed &$... ] )参数类型:

  • 对应的变量具有整数类型
  • d对应变量的类型为 double
  • s对应的变量为字符串类型
  • b对应的变量是一个 blob,将在数据包中发送

*检查数据是否正确$_POSTvar_dump($_POST)

于 2014-07-21T14:17:12.580 回答