1

我想将数据更新到 mysql 数据库我知道查询。查询没问题。。

我面临的问题是我应该如何将 id 页面发送到其他页面..

假设我在页面 Skill_edit.php?id=7 上,然后单击更新按钮,然后使用表单的 POST 方法从 Skill_edit.php 转到 Skills.php。

但是现在我如何将页面的 id 甚至表中的行 id 发送到“skills.php”

我的数据库:

在此处输入图像描述

我在 Skill_edit.php 中的表格

     <form class="form-horizontal row-fluid" method="post" action="skills.php">

        <?php //Skill Name ?>
        <div class="form-row control-group row-fluid">
          <label class="control-label span3" for="with-placeholder">Skill Name</label>
          <div class="controls span7">
            <input type="text" name="skill_name" id="with-placeholder" class="row-fluid" value="<?php echo $showskillname; ?>">
          </div>
        </div>

       <?php //Skill Description ?>
        <div class="form-row control-group row-fluid">
          <label class="control-label span3" for="elastic-textarea">Skill Desc <span class="help-block">My Skill Description</span> </label>
          <div class="controls span7">
            <textarea name="skill_desc" rows="3" style=" height:80px;"  class="row-fluid autogrow" id="elastic-textarea"><?php echo $showskilldesc; ?></textarea>
          </div>
        </div>

        <?php //Selecting Language ?>
        <div class="form-row control-group row-fluid">
          <label class="control-label span3">Select with search</label>
          <div class="controls span7">
            <select name="skill_rating" data-placeholder="Choose a Language...." class="chzn-select">
              <option value="<?php echo $showskillrating; ?>"><?php echo $showskillrating; ?></option>
              <option value="1">1 Star</option>
              <option value="2">2 Star</option>
              <option value="3">3 Star</option>
              <option value="4">4 Star</option>
              <option value="5">5 Star</option>
            </select>
          </div>
        </div>


       <?php //Buttons ?>
        <div class="form-actions row-fluid">
          <div class="span7 offset3">
            <button name="updatebtn" style=" float:left; margin-left:40px;" type="submit" class="btn btn-primary">Save changes</button>
            <button formaction="skills.php" style=" float:right; margin-right:40px;" type="submit" class="btn btn-secondary">Cancel</button>
          </div>
        </div>
      </form>

我在其上获取数据的 Skill.php 页面

if(isset($_POST['updatebtn']))
{
$updatedskillname = mysql_real_escape_string($_POST['skill_name']);
$updatedskilldesc = mysql_real_escape_string($_POST['skill_desc']);
$updatedskillrating = mysql_real_escape_string($_POST['skill_rating']);
$last_updated = mysql_real_escape_string(date('F j, Y, g:i a'));

$update=update_skill($updatedskillname, $updatedskilldesc, $updatedskillrating, $last_updated);
}

这是函数的内部

//Update Skill
//skills.php
function update_skill($updatedskillname, $updatedskilldesc, $updatedskillrating, $last_updated)
{
        $query="UPDATE cv_skills SET
                            skill_name='$updatedskillname',
                            skill_desc='$updatedskilldesc',
                            skill_rating='$updatedskillrating',
                            last_updated='$last_updated' WHERE skill_id='$pageid'";
$result=mysql_query($query);
$error=mysql_error();

return $error;
}

那么我怎样才能在我的查询中获得 Skill_id 呢?

4

3 回答 3

3

有几种可能的方法可以做到这一点,但一种方法是:

<form class="form-horizontal row-fluid" method="post" action="skills.php?id=<?php echo $_GET['id']; ?>">

然后,这将回发给自己,而 id 仍然完好无损。另一种方法是您可以使用隐藏字段,以便将 id 发布回表单。

隐:

<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
于 2013-01-25T18:23:32.907 回答
0

最好的选择是将 id 实际存储在会话或 cookie 变量中,并将其用作限定符——但那是另一天的事了。

<?php
//create a login form at post to this script at login...

/*
<form action="" method="post">
<label>Email:</label><br />
<input type="text" name="email" /><br /><br />
<label>Password:</label><br />
<input type="password" name="password" /><br /><br />
<label>Remember Me:</label><br />
<input type="checkbox" name="remember" /><br /><br />
<input type="submit" name="submit" value="Login" /><br /><br />
</form>
*/

//connect to your database here...
//something like..
include('../includes/db_connection.php');

/*
Table "users" fields would contain userid(int), email(varchars), password(varchars) at a minimum...
*/

$submit = $_POST['submit'];
if(isset($submit)) {

//grab the post info at login...
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

//md5 encryption at a minimum...
$password = md5($password);

//this is just to declare the timeframe for the cookie....
$remember = strip_tags($_POST['remember']);

//query the users table..
//for demo purposes, this is a basic where normally
//I would wrap this into a function.
$sql = mysql_query("SELECT * FROM users WHERE email='$email'");
$row = mysql_fetch_array($sql);

//set-up the userid for the cookie...
$userid = strip_tags($row['userid']);

//if user exists
if(mysql_num_rows($sql)>0){

//compare the password
if(strcmp($row['password'],$password)==0){

//set the cookie with the the userid...
if (isset($remember)) {

/* Set cookie to last 1 year */
$expire = time()+60*60*24*365;
setcookie('userid', $userid, $expire);        
}

else {

/* Set cookie until the user logs out */
$expire = false;
setcookie('userid', $userid, false); 
}
//send the user over to their secure account...
header('Location: your_secure_login_area.php');

/*
You would then add the following to the top of any secure page that would be applied to the end-user...

<?php
//you can now reference the userid for any db call...
$userid = $_COOKIE['userid'];
if (isset($userid)) {
}
else if(!isset($userid) || $userid == '' || $_GET['userid'] != $_COOKIE['userid']) {
header('Location: http://yourloginpage.php');
}
?>

*/

}
}

else {
$reponse = 'Error: Invalid Information.';
}
}
?>
于 2013-01-25T18:44:05.677 回答
0

使用 session 或 cookie 来传递 id。因为用户总是可以在提交表单之前篡改 id 名称并修改值。

于 2013-01-25T19:01:42.460 回答