Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/153474-query-failed-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806372
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806376
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806391
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806401
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/153474-query-failed-help/#findComment-806559
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.