简体   繁体   English

使用MySQLi插入多行

[英]Inserting Multiple Rows With MySQLi

I am trying to insert multiple rows to database using mysqli but it's not working... 我正在尝试使用mysqli向数据库中插入多行,但无法正常工作...

Note: my aim is to insert both text and file field names together in to database after the image uploaded successfully. 注意:我的目的是在成功上传图像后将文本和文件字段名称一起插入数据库。 Any idea? 任何想法?

Here is the html form... 这是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> 

here is what I have in send.php....when I try to insert the image path to database it works but it doesn't work when I include the text field names from the first form.. 这是我在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>

I'm a little puzzled about your layout. 我对您的布局有些疑惑。 You have two separate forms, one for the text inputs and one for the image? 您有两种单独的形式,一种用于文本输入,另一种用于图像?

Because of this, only the values of the image path gets inserted. 因此,仅插入图像路径的值。 The image is uploaded, but the values $_POST['fname'], $_POST['lname'] & $_POST['age'] are not set. 图片已上传,但未设置$ _POST ['fname'],$ _ POST ['lname']和$ _POST ['age']值。

You should make it in one form and submit that. 您应该以一种形式进行提交。

Replace: 更换:

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

with

Each text field in your database column 数据库列中的每个文本字段

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

or 要么

All text fields in the same own database column 同一数据库列中的所有文本字段

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


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

  • i corresponding variable has type integer 相应的变量具有整数类型
  • d corresponding variable has type double d对应变量的类型为double
  • s corresponding variable has type string s对应的变量具有字符串类型
  • b corresponding variable is a blob and will be sent in packets b对应的变量是blob,将以数据包的形式发送

*Check $_POST with var_dump($_POST) to see if data is correct. *使用var_dump($_POST)检查$_POST以查看数据是否正确。

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

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