简体   繁体   中英

Inserting Multiple Rows With MySQLi

I am trying to insert multiple rows to database using mysqli but it's not working...

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...

<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..

// 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.

You should make it in one form and submit that.

Replace:

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

with

Each text field in your database column

$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);


bind_param ( string $types , mixed &$var1 [, mixed &$... ] ) parameter types:

  • i corresponding variable has type integer
  • d corresponding variable has type double
  • s corresponding variable has type string
  • b corresponding variable is a blob and will be sent in packets

*Check $_POST with var_dump($_POST) to see if data is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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