0

I am trying to get information to be saved in one database into 5 tables from user input. There are no errors that pop-up but the data won't save to the DB. Any sample code or typed solutions would be great. Thanks.

Here is my code so far: The arrays are defined on a different page.

$array = array("example");
$random = $array;
shuffle($random);

HTML:

<tr>
<form action="Voting_action.php" method="post">
<td><input type="image" value="<?php echo $img;?>" /> 
<input type="submit"class="buttontable1" value="<?php echo array_pop($random);?>" name="name"/></td>
<td><input type="image" value="<?php echo $img;?>" />
<input type="submit" class="buttontable2" value="<?php echo array_pop($random2);?>" name="name2" /></td>
</form>
</tr>

<tr>
<form action="Voting_action.php" method="post">
<td><input type="image" value="<?php echo $img;?>" />
<input type="submit" class="buttontable1" value="<?php echo array_pop($random3);?>" name="name3"/></td>
<td><input type="image" value="<?php echo $img;?>" />
<input type="submit" class="buttontable2" value="<?php echo array_pop($random4);?>" name="name4" /></td>
</form>
</tr>

<tr>
<form action="Voting_action.php" method="post">
<td><input type="image" value="<?php echo $img;?>" />
<input type="submit" class="buttontable1" value="<?php echo array_pop($random);?>" name="name"/></td>
<td><input type="image" value="<?php echo $img;?>" />
<input type="submit" class="buttontable2" value="<?php echo array_pop($random5);?>" name="name5" /></td>
</form>
</tr>

Here's the PHP mysqli code I have so far with some help from other tutorials and forums:

<?php
include('Voting.php');
$server = '';
$dbname= '';
$username = '';
$password = '';


$link = new mysqli($server, $username, $password, $dbname);

if (mysqli_connect_error()) {
header('Location: /errordocs/500.php');
exit;
}

$link->autocommit(FALSE);


$sql = "INSERT INTO table (name) VALUES (?)";

if ($stmt = $link->prepare($sql)) {
  $stmt->bind_param("sssss", $array);
  $stmt->execute();

$firstinsert = $stmt->affected_rows;
 }
$stmt->close;

$newid = $link->insert_id;

$sql = "INSERT INTO table(name2) VALUES (?)";

if ($stmt = $link->prepare($sql)) {
  $stmt->bind_param("ssi", $array2);
  $stmt->execute();

$secondinsert = $stmt->affected_rows;
 }
$stmt->close;

$newid = $link->insert_id;

$sql = "INSERT INTO table(name3) VALUES (?)";

if ($stmt = $link->prepare($sql)) {
  $stmt->bind_param("ssi", $array3);
  $stmt->execute();


$thirdinsert = $stmt->affected_rows;
 }
$stmt->close;

$newid = $link->insert_id;


$sql = "INSERT INTO table(name4) VALUES (?)";

if ($stmt = $link->prepare($sql)) {
  $stmt->bind_param("ssi", $array4);
  $stmt->execute();

$fourthinsert = $stmt->affected_rows;
 }
$stmt->close;

$newid = $link->insert_id;

$sql = "INSERT INTO table(name5) VALUES (?)";

if ($stmt = $link->prepare($sql)) {
  $stmt->bind_param("ssi", $array5);
  $stmt->execute();


$fifthinsert = $stmt->affected_rows;
 }
$stmt->close;


if (($firstinsert == 1) && ($secondinsert == 1) && ($thirdinsert == 1) && ($fourthinsert == 1) && ($fifthinsert == 1)) {

$link->commit();
$link->autocommit(TRUE);
  return 1;
 } else {
$link->rollback();
$link->autocommit(TRUE);
  return 0;
 }
?>

Thank in advance!

4

0 回答 0