searls03 Posted March 11, 2011 Share Posted March 11, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/ Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 Please use tags in the future. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186230 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 Because you print out the success message no matter what. Try checking how many rows got updated with - mysql_rows_affected. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186231 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186234 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 I am having this probelm on one other page also Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186272 Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 have your fixed your IF statement yet? Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186280 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 What am I supposed to put instead? Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186284 Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186287 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 You know what, i believe it was working the entire time.......the page that was supposed to echo it was not doing it right. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186289 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186298 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 here is code that should insert it but it says -1 records updated: From the manual: Returns the number of affected rows on success, and -1 if the last query failed. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186300 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 ok, so how do i fix it? Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186302 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 ok, so how do i fix it? Inspect your query and find the error. You're missing a comma. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186304 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 now it returns 0 which if i read it right is delete a record. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186307 Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 head+desk=ouch. Just says that 0 records were updated. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186308 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 oh sorry, I thought the manual said if zero was returned it meant deleted. Ok so how do I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186309 Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 fix your input. chances are you didn't select anything that would result in a change. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186311 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 do I need to have a primary key? I need to figure out how to pull id from table members and put it into table scoutinfo. do I need to do a get id or what? Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186313 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186314 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 there is nothing there in the database. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186315 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 there is nothing there in the database. Then what did you expect to update? I'm confused. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186317 Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 if there's nothing in the database, there's nothing to update. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186318 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 well i inserted something and it didn't update it. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186320 Share on other sites More sharing options...
searls03 Posted March 11, 2011 Author Share Posted March 11, 2011 ok, it works if i take out the id trying to insert. How do I make it so that it will insert. I want to take current session id id and put it into the id field in the table Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186321 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230352-why-wont-this-update-the-database/#findComment-1186322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.