尝试使用html表单更新数据库条目时出错

时间:2014-02-21 19:26:25

标签: php html sql mysqli

我有一个显示某些信息的角色数据库。当我点击“更新信息”时,它会将我带到一个HTML表单,我可以在其中更新当前信息。当我填写新信息并点击“更新”时,我收到以下错误:

警告:mysqli_stmt :: bind_param():类型定义字符串中的元素数与第59行/home3/tarb89/public_html/aususrpg.net/charbase/updated.php中的绑定变量数不匹配 绑定参数失败:(0)

这是我的update.php代码:

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

这是我的updated.php代码:

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

我是PHP的新手,所以我不太确定这里有什么问题。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

查看您传递给bind_param()的值与prepare()中的问号。我可以一眼看出$id在错误的地方。错误消息表明计数也是错误的。

编辑:在PDO中这样的东西可以节省你不得不计算问号。完全未经测试;可能甚至没有解析,但应该指向一个更容易使用解决方案。

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

答案 1 :(得分:0)

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

对于要绑定的每个字符串,添加s,对于每个整数,您应添加i。我不确定我的类型是否正确检查它们并修改任何si,如果它实际上是您正在更新的整数。

以下链接了解详情:

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