Sizzling Code
Sizzling Code

Reputation: 6070

PHP Update mysql Database Table from id

I want to update data to mysql database i know the query. query is not a problem..

The Problem i am facing is that how should i send id page to other page..

lets suppose i am on page skill_edit.php?id=7 and i click update button then it goes to skills.php from skill_edit.php using POST method of form.

but now how can i send id of page or even id of row in table to "skills.php"

My Database:

enter image description here

My Form in 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>

my skill.php page on which i get the data

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

Here is the inside of the function

//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;
}

so how can i get skill_id in my query??

Upvotes: 1

Views: 7731

Answers (3)

Tyler Andersen
Tyler Andersen

Reputation: 1

The best option would be to actually store the id within a session or cookie variable and use it as a qualifier -- but that's for another day.

<?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.';
}
}
?>

Upvotes: 0

Ganesh Rathinavel
Ganesh Rathinavel

Reputation: 1335

use session or cookie to pass id. as the user can always tamper the id name and modify the value before form submission.

Upvotes: 0

UnholyRanger
UnholyRanger

Reputation: 1971

There's a couple possible ways you can do this but one would be to do:

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

This will then post back to itself with the id still in tact. The other way would be you could use a hidden field so you POST the id back to the form.

Hidden:

<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">

Upvotes: 3

Related Questions