Jump to content

Why wont this update the database


searls03

Recommended Posts

I use mysql and this code that I will show says that the info was sucessfully updated, but it wasnt.  please help.  By the way, I am designing a website for a boy scout troop that has asked me to help.  any help appreciated:

<?php
session_start(); // Must start session first thing
/*
Created By Adam Khoury @ [url=http://'http://www.flashbuilding.com/']www.flashbuilding.com[/url]
-----------------------June 20, 2008-----------------------
*/
// Here we run a login check
if (!isset($_SESSION['id'])) {
echo 'Please <a href="login.php">log in</a> to access your account';
exit();
}
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$id = $_SESSION['id'];

// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$rank = $row['rank'];

}



// Process the form if it is submitted
if ($_POST['rank']) {
$rank = $_POST['rank'];
$sql = mysql_query("UPDATE members SET rank='$rank' where id='$id'");
echo 'Your account info has been updated, visitors to your profile will now see the new info.<br /><br />
<meta HTTP-EQUIV="REFRESH" content="3; url=http://final.net46.net/myprofile.php">';
exit();
} // close if post
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="scout.php" onsubmit="return validate_form ( );">
  <label for="rank"></label>
  <select name="rank" id="rank">
    <option value="Scout">Scout</option>
    <option value="Tenderfoot">Tenderfoot</option>
    <option value="Second Class Scout">Second Class Scout</option>
    <option value="First Class Scout">First Class Scout</option>
    <option value="Star Scout">Star Scout</option>
    <option value="Life Scout">Life Scout</option>
    <option value="Eagle Scout">Eagle Scout</option>
    <option value="<?php echo $rank; ?>" selected="selected"></option>
  </select>
  <input type="submit" name="submit" id="submit" value="Save" />
</form>
</body>
</html>

Link to comment
Share on other sites

here is a code I based it off of:

<?php
session_start(); // Must start session first thing
/*
Created By Adam Khoury @ [url='http://www.flashbuilding.com/']www.flashbuilding.com[/url]
-----------------------June 20, 2008-----------------------
*/
// Here we run a login check
if (!isset($_SESSION['id'])) {
echo 'Please <a href="login.php">log in</a> to access your account';
exit();
}
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$id = $_SESSION['id'];



// Process the form if it is submitted
if ($_POST['username']) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$username = $_POST['username'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$cell = $_POST['cell'];
$email = $_POST['email'];

$sql = mysql_query("UPDATE members SET name='$name', phone='$phone', username='$username', address='$address', city='$city', state='$state', zip='$zip', cell='$cell', email='$email'  WHERE id='$id'");
echo 'Your account info has been updated, visitors to your profile will now see the new info.<br /><br />
<meta HTTP-EQUIV="REFRESH" content="3; url=http://final.net46.net/myprofile.php">';
exit();
} // close if post
?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$name = $row["name"];
$phone = $row["phone"];
$username = $row["username"];
$address = $row["address"];
$city = $row["city"];
$state = $row["state"];
$zip = $row["zip"];
$cell = $row["cell"];
$email = $row["email"];
$accounttype = $row["accounttype"];
$rank = $row["rank"];
$badges = $row["badges"];
}
?>

 

with a few modifications obviously, and that one works just fine.

Link to comment
Share on other sites

First, figure out exactly what you're doing is doing to the database like Maq said:

 

// Process the form if it is submitted
if ($_POST['rank']) {
$rank = $_POST['rank'];
$sql = mysql_query("UPDATE members SET rank='$rank' where id='$id'");
printf("Records updated: %d\n", mysql_affected_rows());

 

If needed, figure out the problem

echo mysql_error();

 

Add that and then tell us what's wrong.

Link to comment
Share on other sites

for rank and badges, I have set up a new table called scoutinfo.  I need to know how to insert the session id into the row id in scout info.  here is code that should insert it but it says -1 records updated:

<?php
session_start(); // Must start session first thing
/*
Created By Adam Khoury @ [url=http://'http://www.flashbuilding.com/']www.flashbuilding.com[/url]
-----------------------June 20, 2008-----------------------
*/
// Here we run a login check
if (!isset($_SESSION['id'])) {
echo 'Please <a href="login.php">log in</a> to access your account';
exit();
}
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$id = $_SESSION['id'];

// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$rank = $row['rank'];

}



// Process the form if it is submitted
if ($_POST['rank']) {
$id = $_POST['id'];
$badges = $_POST['badges'];
$rank = $_POST['rank'];
$sql = mysql_query("UPDATE scoutinfo SET id='$id',  rank='$rank' badges='$badges' where id='$id'");
printf("Records updated: %d\n", mysql_affected_rows());
exit();
} // close if post
?>

Link to comment
Share on other sites

oh sorry, I thought the manual said if zero was returned it meant deleted.  Ok so how do I fix this?

No, nothing was deleted.  If it returned 0 then you didn't update anything which most likely means your WHERE condition didn't match on anything.  Echo out your query and check your DB manually to make sure an entry actually exists.

Link to comment
Share on other sites

well i inserted something and it didn't update it.

Still confused.  You just said there was nothing in the database.  UPDATE is used to alter values of a record that already exists, in other words, has already been INSERTed.  INSERT is used to put a brand new record in the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.