繁体   English   中英

使用MySQLi插入多行

[英]Inserting Multiple Rows With MySQLi

我正在尝试使用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>

我对您的布局有些疑惑。 您有两种单独的形式,一种用于文本输入,另一种用于图像?

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

您应该以一种形式进行提交。

更换:

$ 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,将以数据包的形式发送

*使用var_dump($_POST)检查$_POST以查看数据是否正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM