努力更新一行我知道我通过再次var_dump()
$ _POST
来发布正确的值,但它必须是我用来更新表格的方法:
<?php
/******************************************************************
** ACTION SCRIPT TO UPDATE THE CATEGORY AFTER CHANGES ARE MADE **
******************************************************************/
if(isset($_POST["submit"])){
include_once'../../config.php';
try {
$dbh = new PDO("mysql:host=localhost;dbname=dashboardr",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE cat_list SET cat_title = :cat_title, cat_color = :cat_color, cat_icon = :cat_icon WHERE cat_id = :cat_id";
$query = $dbh->prepare($sql);
$query->execute(array(":cat_title"=>$_POST["cat_title"],
":cat_color"=> $_POST["cat_color"]),
":cat_icon"=> $_POST["cat_icon"]),
":cat_id"=> $_POST["cat_id"]));
if ($query) {
header ('Location: ../list_cat.php?success=2');
}
else{
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
以下是表格:
<?php include 'header.php'; ?>
<?php
require_once '../../db_con.php';
if(!empty($_GET['cat_id'])){
$cat = intval($_GET['cat_id']);
try{
$results = $dbh->prepare('SELECT * FROM cat_list WHERE cat_id = ?');
$results->bindParam(1, $cat);
$results->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$cat = $results->fetch(PDO::FETCH_ASSOC);
if($cat == FALSE){
echo '<div class="container">';
echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
echo '<a href="userList.php" style="margin: 40px auto; display: block; text-align: center;">Get me outta here!</a>';
echo'</div>';
die();
}
}
?>
<form action="actions/update_cat.php" method="POST" id="rtf" name="">
<input type="hidden" value="<?php echo $cat['cat_id'] ?>" name="cat_id" />
<input type="text" value="<?php echo $cat['cat_title'] ?>" name="cat_title" /><br><br>
<div class="catOptionsMenu">
<!-- COLOUR PICKER FOR CATEGORY HEADERS -->
<input type="radio" id="radio1" name="cat_color" value="#d31b26">
<label for="radio1"><div class="redSelect" onclick="button_click('#d31b26');"></div></label>
<input type="radio" id="radio2" name="cat_color" value="#f9c04c">
<label for="radio2"><div class="yellowSelect" onclick="button_click('#f9c04c');" ></div></label>
<input type="radio" id="radio3" name="cat_color" value="#ec9292">
<label for="radio3"><div class="pinkSelect" onclick="button_click('#ec9292');"></div></label>
<input type="radio" id="radio4" name="cat_color" value="#b7d04e">
<label for="radio4"><div class="greenSelect" onclick="button_click('#b7d04e');"></div></label>
<input type="radio" id="radio5" name="cat_color" value="#637a91">
<label for="radio5"><div class="slateSelect" onclick="button_click('#637a91');"></div></label>
<input type="radio" id="radio6" name="cat_color" value="#AEA8D3">
<label for="radio6"><div class="purpleSelect" onclick="button_click('#AEA8D3');"></div></label>
<input type="radio" id="radio13" name="cat_color" value="#72bce9">
<label for="radio13"><div class="blueSelect" onclick="button_click('#72bce9');"></div></label>
<br><br>
<!-- ICON PICKER FOR CATEGORY HEADERS -->
<input type="radio" id="radio7" type="radio" name="cat_icon" value='<i class="fa fa-phone" style="font-size: 2em;"></i>'>
<label for="radio7"><div class="iconSelect" onclick="button_click_icon1()"><i class='fa fa-phone' style='font-size: 2em;'></i></div></label>
<input type="radio" id="radio8" type="radio" name="cat_icon" value='<i class="fa fa-graduation-cap" style="font-size: 2em;"></i>'>
<label for="radio8"><div class="iconSelect" onclick="button_click_icon2()"><i class='fa fa-graduation-cap' style='font-size: 2em;'></i></div></label>
<input type="radio" id="radio9" type="radio" name="cat_icon" value='<i class="fa fa-users" style="font-size: 2em;"></i>'>
<label for="radio9"><div class="iconSelect" onclick="button_click_icon3()"><i class='fa fa-users' style='font-size: 2em;'></i></div></label>
<input type="radio" id="radio10" type="radio" name="cat_icon" value='<i class="fa fa-question-circle" style="font-size: 2em;"></i>'>
<label for="radio10"><div class="iconSelect" onclick="button_click_icon4()"><i class='fa fa-question-circle' style='font-size: 2em;'></i></div></label>
<input type="radio" id="radio11" type="radio" name="cat_icon" value='<i class="fa fa-file-text" style="font-size: 2em;"></i>'>
<label for="radio11"><div class="iconSelect" onclick="button_click_icon5()"><i class='fa fa-file-text' style='font-size: 2em;'></i></div></label>
<input type="radio" id="radio12" type="radio" name="cat_icon" value='<i class="fa fa-at" style="font-size: 2em;"></i>'>
<label for="radio12"><div class="iconSelect" onclick="button_click_icon6()"><i class='fa fa-at' style='font-size: 2em;'></i></div></label>
</div>
<br><br><br>
<!-- CATEGORY HEADER DEMO BUILD VIEW -->
<div class="indexBox">
<div style="<?php echo 'background-color: '.$cat["cat_color"].'' ?>" class="indexBoxHeader" id="box">
<siv id="icon"><?php echo ''.$cat["cat_icon"].'' ?></div>
<div class="indexBoxFooter">
<div class='printchatbox' id='printchatbox'></div>
</div>
</div>
<br><br>
<input onclick="formsubmit()" type="submit" value="Update Category" name="submit"/>
</form>
我相信它是动作脚本中的UPDATE查询 - 我知道它不是那么安全但是我会整理它。
答案 0 :(得分:2)
正确设置execute
的参数。这是一个数组,现在你有一些奇怪的东西:
$query->execute(array(":cat_title"=>$_POST["cat_title"],
":cat_color"=> $_POST["cat_color"]), // extra )
":cat_icon"=> $_POST["cat_icon"]), // extra )
":cat_id"=> $_POST["cat_id"]));
应该是:
$query->execute(array(
":cat_title"=>$_POST["cat_title"],
":cat_color"=> $_POST["cat_color"],
":cat_icon"=> $_POST["cat_icon"],
":cat_id"=> $_POST["cat_id"]
));