[英]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 &$... ] )
參數類型:
*使用var_dump($_POST)
檢查$_POST
以查看數據是否正確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.