herghost Posted April 10, 2009 Share Posted April 10, 2009 Hi, Can anyone see why this is taking me to the updatefailed.php? I just cant work it out! This enters information into a database or updates if already present, then creates a session <?php //Start session session_start(); //Include database connection details require_once('include/database.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $userid = $_SESSION['SESS_USERID']; $bandname = $_SESSION['SESS_BANDNAME']; $genre = clean($_POST['genre']); $formed = clean($_POST['formed']); $member0 = clean($_POST['member0']); $member1 = clean($_POST['member1']); $member2 = clean($_POST['member2']); $member3 = clean($_POST['member3']); $member4 = clean($_POST['member4']); $member5 = clean($_POST['member5']); $position0 = clean($_POST['position0']); $position1 = clean($_POST['position1']); $position2 = clean($_POST['position2']); $position3 = clean($_POST['position3']); $position4 = clean($_POST['position4']); $position5 = clean($_POST['position5']); //Input Validations if($formed == '') { $errmsg_arr[] = 'Year Formed is Missing'; $errflag = true; } $sql = mysql_query("SELECT * FROM banddata WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata (userid, bandname, genre, formed, position0, member0, position1, member1, position2, member2, position3, member3, position4, member4, position5, member5,) VALUES ('$userid','$bandname','$genre', '$formed' '$position0', '$member0','$position1', '$member1', '$position2', '$member2','$position3', '$member3','$position4', '$member4', '$position5', '$member5')"; } else { //Create update query $qry = "UPDATE banddata SET bandname = '$bandname', genre = '$genre', formed = '$formed', position0 = '$position0', member0 ='$member0', position1 = '$position1', member1= '$member1', position2 = '$position2', member2 ='$member2', position3 = '$position3', member3 ='$member3', position4 = '$position4', member4 ='$member4', position5 = '$position5', member5 = '$member5' WHERE userid = '$userid'"; } $result = @mysql_query($qry); if($result) { if(mysql_num_rows($result) == 1) { session_regenerate_id(); $banddata = mysql_fetch_assoc($result); $_SESSION['SESS_GENRE'] = $banddata['genre']; $_SESSION['SESS_FORMED'] = $banddata['formed']; $_SESSION['SESS_POSITION0'] = $banddata['position0']; $_SESSION['SESS_MEMBER0'] = $banddata['member0']; $_SESSION['SESS_POSITION1'] = $banddata['position1']; $_SESSION['SESS_MEMBER1'] = $banddata['member1']; $_SESSION['SESS_POSITION2'] = $banddata['position2']; $_SESSION['SESS_MEMBER2'] = $banddata['member2']; $_SESSION['SESS_POSITION3'] = $banddata['position3']; $_SESSION['SESS_MEMBER3'] = $banddata['member3']; $_SESSION['SESS_POSITION4'] = $banddata['position4']; $_SESSION['SESS_MEMBER4'] = $banddata['member4']; $_SESSION['SESS_POSITION5'] = $banddata['position5']; $_SESSION['SESS_MEMBER5'] = $banddata['member5']; session_write_close(); header("location: member_home.php"); exit(); }else { //failed header("location: updatefailed.php"); exit(); } }else { die("Query failed"); } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/ Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 1) Your query may be failing, change this line: [code] $result = mysql_query($qry) or die(mysql_error()); 2) This IF is failing. if(mysql_num_rows($result) == 1) { [/code] Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806354 Share on other sites More sharing options...
mrMarcus Posted April 10, 2009 Share Posted April 10, 2009 probably because the query failed .. from either an incorrect userid or something along those lines. make sure values going into the database are proper. Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806355 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 I say this all the time - put or die(mysql_error()); after any queries you do. And also, DON'T SUPPRESS ERRORS WITH AN @!!! That way you have NO way of knowing what went wrong... Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806368 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 The fact that the code took the path it did means that the query executed. Edit: There would have been a die("Query failed") if the query failed to execute. The actual problem is that mysql_num_rows() only works for queries that return a result set. You need to use mysql_affected_rows() to test for how many rows were affected by an INSERT or UPDATE query. Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806372 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 Ok well I tried with the myslq error where asked and received the same response, I have reverted back to an older version without the creating sessions, it now looks like this: <?php //Start session session_start(); //Include database connection details require_once('include/database.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $userid = $_SESSION['SESS_USERID']; $bandname = $_SESSION['SESS_BANDNAME']; $genre = clean($_POST['genre']); $formed = clean($_POST['formed']); $member0 = clean($_POST['member0']); $member1 = clean($_POST['member1']); $member2 = clean($_POST['member2']); $member3 = clean($_POST['member3']); $member4 = clean($_POST['member4']); $member5 = clean($_POST['member5']); $position0 = clean($_POST['position0']); $position1 = clean($_POST['position1']); $position2 = clean($_POST['position2']); $position3 = clean($_POST['position3']); $position4 = clean($_POST['position4']); $position5 = clean($_POST['position5']); //Input Validations if($formed == '') { $errmsg_arr[] = 'Year Formed is Missing'; $errflag = true; } $sql = mysql_query("SELECT * FROM banddata WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata (userid, bandname, genre, formed, position0, member0, position1, member1, position2, member2, position3, member3, position4, member4, position5, member5,) VALUES ('$userid','$bandname','$genre', '$formed' '$position0', '$member0','$position1', '$member1', '$position2', '$member2','$position3', '$member3','$position4', '$member4', '$position5', '$member5')"; } else { //Create update query $qry = "UPDATE banddata SET bandname = '$bandname', genre = '$genre', formed = '$formed', position0 = '$position0', member0 ='$member0', position1 = '$position1', member1= '$member1', position2 = '$position2', member2 ='$member2', position3 = '$position3', member3 ='$member3', position4 = '$position4', member4 ='$member4', position5 = '$position5', member5 = '$member5' WHERE userid = '$userid'"; } $result = mysql_query($qry) or die(mysql_error()); //Check whether the query was successful or not if($result) { header("location: member_home.php"); exit(); }else { die(mysql_error()); } ?> How would I now create the sessions for teh new data like the member0 etc Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806376 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Yeah - PFMaBiSmAd, that is true actually... Yeah, why not try following his advice, and instead of putting if($result) try if(mysql_affected_rows($result) > 0) or something. Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806382 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 like this? $result = @mysql_query($qry); if(mysql_affected_rows($result) > 0) { session_regenerate_id(); $banddata = mysql_fetch_assoc($result); $_SESSION['SESS_GENRE'] = $banddata['genre']; $_SESSION['SESS_FORMED'] = $banddata['formed']; $_SESSION['SESS_POSITION0'] = $banddata['position0']; $_SESSION['SESS_MEMBER0'] = $banddata['member0']; $_SESSION['SESS_POSITION1'] = $banddata['position1']; $_SESSION['SESS_MEMBER1'] = $banddata['member1']; $_SESSION['SESS_POSITION2'] = $banddata['position2']; $_SESSION['SESS_MEMBER2'] = $banddata['member2']; $_SESSION['SESS_POSITION3'] = $banddata['position3']; $_SESSION['SESS_MEMBER3'] = $banddata['member3']; $_SESSION['SESS_POSITION4'] = $banddata['position4']; $_SESSION['SESS_MEMBER4'] = $banddata['member4']; $_SESSION['SESS_POSITION5'] = $banddata['position5']; $_SESSION['SESS_MEMBER5'] = $banddata['member5']; session_write_close(); header("location: member_home.php"); exit(); }else { //failed header("location: updatefailed.php"); exit(); } Still gives same result Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806391 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2009 Share Posted April 10, 2009 Remove all the @ from in front of your function calls. On a development server, you should have display_errors set to ON and on a live server you should have display_errors set to OFF. There is no need to put @ in your code to suppress error messages. What happened to the if($result) { logic? For all we know one of your queries works and the other one does not. You always need to test if a query worked or not before you access any of the returned values. Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806401 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 I know the insert and update queryies work, its just the assigning values to sessions which is causing it not to work, can I just assign these sessions outside of the if loop? Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806550 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Settings sessions should make no difference... Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806555 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 I know this does what it is meant to: <?php //Start session session_start(); //Include database connection details require_once('include/database.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $userid = $_SESSION['SESS_USERID']; $bandname = $_SESSION['SESS_BANDNAME']; $genre = clean($_POST['genre']); $formed = clean($_POST['formed']); $member0 = clean($_POST['member0']); $member1 = clean($_POST['member1']); $member2 = clean($_POST['member2']); $member3 = clean($_POST['member3']); $member4 = clean($_POST['member4']); $member5 = clean($_POST['member5']); $position0 = clean($_POST['position0']); $position1 = clean($_POST['position1']); $position2 = clean($_POST['position2']); $position3 = clean($_POST['position3']); $position4 = clean($_POST['position4']); $position5 = clean($_POST['position5']); //Input Validations if($formed == '') { $errmsg_arr[] = 'Year Formed is Missing'; $errflag = true; } $sql = mysql_query("SELECT * FROM banddata WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata (userid, bandname, genre, formed, position0, member0, position1, member1, position2, member2, position3, member3, position4, member4, position5, member5,) VALUES ('$userid','$bandname','$genre', '$formed' '$position0', '$member0','$position1', '$member1', '$position2', '$member2','$position3', '$member3','$position4', '$member4', '$position5', '$member5')"; } else { //Create update query $qry = "UPDATE banddata SET bandname = '$bandname', genre = '$genre', formed = '$formed', position0 = '$position0', member0 ='$member0', position1 = '$position1', member1= '$member1', position2 = '$position2', member2 ='$member2', position3 = '$position3', member3 ='$member3', position4 = '$position4', member4 ='$member4', position5 = '$position5', member5 = '$member5' WHERE userid = '$userid'"; } $result = mysql_query($qry); $result = mysql_query($qry) or die(mysql_error()); //Check whether the query was successful or not if($result) { header("location: member_home.php"); exit(); }else { die(mysql_error()); } ?> All I want to do is store the value of genre, formed, member0 etc so I can echo it on a different page, perhaps I am going the wrong about this. Quote Link to comment https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806559 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.