简体   繁体   中英

Error when trying to update database entries using html form

I have a character database that displays certain information. When I click "Update Information" it takes me to an HTML form where I'm allowed to update the current information. When I fill in the new information and hit "Update", I get the following error:

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in /home3/tarb89/public_html/aususrpg.net/charbase/updated.php on line 59 Binding parameters failed: (0)

Here is my update.php code:

<?php
    // Create connection
$con=mysqli_connect("xxx","xxx","xxx","xxx");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$id = $_GET['id'];

$result = mysqli_query($con, "SELECT * FROM characters WHERE id = '$id'");
$row=mysqli_fetch_array($result, MYSQLI_ASSOC);

?>

<form id="FormName" action="../charbase/updated.php" method="post" name="FormName">
<table width="700" border="0" cellspacing="10" cellpadding="0">

<tr>
<td width="300" align="right"><label for="name">Name: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["name"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="picture">Avatar: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["picture"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Breed: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["breed"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Gender: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["gender"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Height: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["height"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Age: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["age"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Parent 1: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["sire"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Parent 2: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["dam"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Children: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["children"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Status: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["status"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Player: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["player"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Body Type: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["bodytype"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Genetics: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["genetics"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Base Color: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["basecolor"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Eye Color: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["eyes"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Mane Color: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["mane"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Tail Color: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["tail"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Patterns: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["patterns"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Facial Markings: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["face"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Leg Markings: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["legs"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Body Markings: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["bodymark"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Extras: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["extras"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Trait 1: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["trait1"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Trait 2: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["trait2"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Trait 3: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["trait3"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Trait 4: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["trait4"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Trait 5: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["trait5"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Trait 6: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["trait6"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Defect 1: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["defect1"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Defect2: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["defect2"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Defect 3: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["defect3"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Defect 4: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["defect4"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Defect 5: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["defect5"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Defect 6: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["defect6"] ?>"></td>
</tr><tr>
<td width="300" align="right"><label for="name">Player Notes: </label></td>
<td><input name="name" maxlength="255" type="text" value="<?php print $row["notes"] ?>"></td>
</tr>

<tr>
<td colspan="2" align="center">
<input name="" type="submit" value="Update">
<input name="id" type="hidden" value="<?php echo $id ?>">
</td>
</tr>

</table>
</form>

And here is my updated.php code:

<?php 
// Your database info 
$db_host = 'xxx'; 
$db_user = 'xxx'; 
$db_pass = 'xxx'; 
$db_name = 'xxx';

// POST data
$id = $_POST['id'];
$name = trim($_POST["name"]);
$picture = trim($_POST["picture"]);
$breed = trim($_POST["breed"]);
$gender = trim($_POST["gender"]);
$height = trim($_POST["height"]);
$age = trim($_POST["age"]);
$sire = trim($_POST["sire"]);
$dam = trim($_POST["dam"]);
$children = trim($_POST["children"]);
$status = trim($_POST["status"]);
$player = trim($_POST["player"]);
$bodytype = trim($_POST["bodytype"]);
$genetics = trim($_POST["genetics"]);
$basecolor = trim($_POST["basecolor"]);
$eyes = trim($_POST["eyes"]);
$mane = trim($_POST["mane"]);
$tail = trim($_POST["tail"]);
$patterns = trim($_POST["patterns"]);
$face = trim($_POST["face"]);
$legs = trim($_POST["legs"]);
$bodymark = trim($_POST["bodymark"]);
$extras = trim($_POST["extras"]);
$trait1 = trim($_POST["trait1"]);
$trait2 = trim($_POST["trait2"]);
$trait3 = trim($_POST["trait3"]);
$trait4 = trim($_POST["trait4"]);
$trait5 = trim($_POST["trait5"]);
$trait6 = trim($_POST["trait6"]);
$defect1 = trim($_POST["defect1"]);
$defect2 = trim($_POST["defect2"]);
$defect3 = trim($_POST["defect3"]);
$defect4 = trim($_POST["defect4"]);
$defect5 = trim($_POST["defect5"]);
$defect6 = trim($_POST["defect6"]);
$notes = trim($_POST["notes"]);


$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

$sql = "UPDATE characters SET name = ?, picture = ?, breed = ?, gender = ?, height = ?, age = ?, sire = ?, dam = ?, children = ?, status = ?, player = ?, bodytype = ?, genetics = ?, basecolor = ?, eyes = ?, mane = ?, tail = ?, patterns = ?, face = ?, legs = ?, bodymark = ?, extras = ?, trait1 = ?, trait2 = ?, trait3 = ?, trait4 = ?, trait5 = ?, trait6 = ?, defect1 = ?, defect2 = ?, defect3 = ?, defect4 = ?, defect5 = ?, defect6 = ?, notes = ? WHERE id = ?"; 
if (!$result = $con->prepare($sql))
{
    die('Query failed: (' . $con->errno . ') ' . $con->error);
}

if (!$result->bind_param('sssssssssssssssssssssssssssssssssssi', $name, $picture, $breed, $gender, $height, $age, $sire, $dam, $children, $status, $player, $bodytype, $genetics, $basecolor, $eyes, $mane, $tail, $patterns, $face, $legs, $bodymark, $extras, $trait1, $trait2, $trait3, $trait4, $trait5, $trait6, $defect1, $defect2, $defect3, $defect4, $defect5, $defect6, $notes, $id))

{
    die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}

if (!$result->execute())
{
    die('Execute failed: (' . $result->errno . ') ' . $result->error);
}

$result->close();
$con->close();
echo "Entry updated.";
?>
<br>
<a href="../login">Return to index page.</a>

I'm a pretty big novice at PHP so I'm not quite sure what's wrong here. Thanks for any help!

Look at the values you're passing to bind_param() versus the question marks in prepare() . I can see at a glance that $id is in the wrong place. The error message says the count is wrong as well.

EDIT: something like this in PDO would save you having to count question marks. Totally untested; may not even parse but should point you toward an easier to work with solution.

<?php 
// Your database info 
$db_host = 'xxx'; 
$db_user = 'xxx'; 
$db_pass = 'xxx'; 
$db_name = 'xxx';
try {
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
} catch(PDOException $e) {
    echo $e->getMessage();
    exit;
}
$expected_posts = array(
    "id", "name", "picture", "breed", "gender", "height", "age", "sire", "dam", "children",
    "status", "player", "bodytype", "genetics", "basecolor", "eyes", "mane", "tail", "patterns",
    "face", "legs", "bodymark", "extras", "trait1", "trait2", "trait3", "trait4", "trait5",
    "trait6", "defect1", "defect2", "defect3", "defect4", "defect5", "defect6", "notes"
);
$posts = array();
foreach ($_POST as $k=>$v) {
    if (in_array($k, $expected_posts)) {
        $posts[$k] = trim($v);
    }
}

$sql = "UPDATE characters SET name=:name, picture=:picture, breed=:breed, gender=:gender, height=:height, 
age=:age, sire=:sire, dam=:dam, children=:children, status=:status, player=:player, bodytype=:bodytype, 
genetics=:genetics, basecolor=:basecolor, eyes=:eyes, mane=:mane, tail=:tail, patterns=:patterns, 
face=:face, legs=:legs, bodymark=:bodymark, extras=:extras, trait1=:trait1, trait2=:trait2, 
trait3=:trait3, trait4=:trait4, trait5=:trait6, trait6=:trait7, defect1=:defect1, defect2=:defect2, 
defect3=:defect3, defect4=:defect4, defect5=:defect5, defect6=:defect6, notes=:notes WHERE id=:id";

$stmt = $conn->prepare($sql);
$stmt->execute($posts);
?>
$result->bind_param('sssssssssssssssssssssssssssssssssssi', $name, $picture, $breed, 
$gender, $height, $age, $sire, $dam, $children, $status, $player, $bodytype, $genetics, 
$basecolor, $eyes,  $mane, $tail, $patterns, $face, $legs, $bodymark, $extras, $trait1, 
$trait2, $trait3, $trait4, $trait5, $trait6, $defect1, $defect2, $defect3, $defect4,
$defect5, $defect6, $notes, $id)

for each string that you want to bind you add an s , and for each integer you should add an i . I am not sure my types are right check them and modify any s to i if it is actually an integer that you're updating with.

Here is a link to learn more:

http://jp.php.net/manual/en/mysqli-stmt.bind-param.php

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