Jump to content

Multi Mysqli Update & Insert Error


SalientAnimal
Go to solution Solved by mac_gyver,

Recommended Posts

Hi there, can anyone tell me why this query would not run? 

<?php

include_once '../includes/db_connect.php';
include_once '../includes/functions.php';
include_once '../includes/session_management.php';

$error_msg = "";

// create string of queries separated by ;
$query  =  	"UPDATE 
						members 
			SET
						level = $level
			WHERE 
						user_id = ?";
						

		var_dump($_POST);exit; 					
						
$query .= 	"INSERT INTO 
						members_info 
			(
						id
			, 			fname
			, 			known_as
			, 			lname
			,			gender
			, 			race
			,			start_date
			, 			department
			, 			level
			, 			msisdn
			, 			dob
			, 			details
			, 			emergency_contact
			, 			emergency_msisdn
			) 
			VALUES 
			(
						'$user_id'
			, 			'$fname'
			, 			'$known_as'
			, 			'$lname'
			, 			'$gender'
			, 			'$race'
			,			'$start_date'
			, 			'$department'
			, 			'$level'
			, 			'$msisdn'
			, 			'$dob'
			, 			'$details'
			, 			'$emergency_contact'
			, 			'$emergency_msisdn'
			);";

// execute query - $result is false if the first query failed
$result = mysqli_multi_query($mysqli, $query);

if ($result) 
{
    do {
        // grab the result of the next query
        if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') {
            echo "Query failed: " . mysqli_error($mysqli);
        }
		
		} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results
		
		{
		//var_dump($_POST);exit;
		header('Location: ../error.php?err=Registration failure: INSERT');
		exit;

        }
		include "../success.html";
		exit;
} else {
    echo "Update Query  Failed..." . mysqli_error($mysqli);
}





 

 

 

In IE it just seems to keep running and in Firefox I get a message saying "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Edited by SalientAnimal
Link to comment
Share on other sites

I removed the redirect from my code but I still get the same error.

 

IE does not return any error, it just hangs in a continuous looping saying 'Waiting for localhost'

In Firefox, I immediately go to a page that says

"

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

 

What I'm not sure about though, is if my page might be using the old outdate "post" page because when I hit submit I have the incorrect post page appearing in my address bar. To try fix this, I have tried restarting the server, clearing my browser cache, as well as restarting, apache, and MySQL.

 

But it still runs the incorrect post page.

Edited by SalientAnimal
Link to comment
Share on other sites

I have cleared browser history twice now, closed everything. And reopened the files, made sure the correct ones were edited etc.

 

I even viewed the source code and the correct source code is showing. But like I say, with it directing to the incorrect page, I don't even know if the page is being processed correctly, and if the code is correct or not.

Link to comment
Share on other sites

Ok so I feel like a bit of a fool not remmebering this earlier, but had a section where I protect my pages, and this page was not accessilbe which is why it kept redirecting to the incorect location.

 

The problem though is I'm get the underfined variable error message.

 

I have done

var_dump($_POST);exit;

and confirmed that the information is getting posted. I have also updaed my code in the submit for to

<?php

include_once '../includes/db_connect.php';
include_once '../includes/functions.php';
sec_session_start();
if (login_check($mysqli) == true) 
	{
    $logged = 'in';
	} 

$error_msg = "";
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$id = $_SESSION['user_id'];


	//var_dump(login_check($mysqli));
	//var_dump($_SESSION); exit; 
	//var_dump($_POST);exit; 


$query  =  	"UPDATE 
						members 
			SET
						level = '$POST_[level]'
			WHERE 
						user_id = ?";
$query .= 	"INSERT INTO 
						members_info 
			(
						id
			, 			fname
			, 			known_as
			, 			lname
			,			gender
			, 			race
			,			start_date
			, 			department
			, 			level
			, 			msisdn
			, 			dob
			, 			details
			, 			emergency_contact
			, 			emergency_msisdn
			) 
			VALUES 
			(
						('$_POST[user_id]'
			, 			'$_POST[fname]'
			, 			'$_POST[known_as]'
			, 			'$_POST[lname]'
			, 			'$_POST[gender]'
			, 			'$_POST[race]'
			,			'$_POST[start_date]'
			, 			'$_POST[department]'
			, 			'$_POST[level]'
			, 			'$_POST[msisdn]'
			, 			'$_POST[dob]'
			, 			'$_POST[details]'
			, 			'$_POST[emergency_contact]'
			, 			'$_POST[emergency_msisdn]'
			);";

// execute query - $result is false if the first query failed
$result = mysqli_multi_query($mysqli, $query);

if ($result) 
{
    do {
        // grab the result of the next query
        if (($result = mysqli_store_result($mysqli)) === false && mysqli_error($mysqli) != '') {
            echo "Query failed: " . mysqli_error($mysqli);
        }
		
		} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli)); // while there are more results

} else {
    echo "Update Query  Failed..." . mysqli_error($mysqli);
}





 

Parse error: syntax error, unexpected '$result' (T_VARIABLE) in C:\Development_Tracker\htdocs\process\register.addinfo.php on line 65
Edited by SalientAnimal
Link to comment
Share on other sites

  • Solution

you need to forget about using a multi query, for the following reasons -

 

1) when running queries you need to know which one is failing and why and for related queries, there's usually no point in running following queries after the point of a failure, they will likely fail for the same reason,

 

2) your first query is partly using prepared query syntax and it's putting raw user data into the query. there is no prepared multi query and you need to escape/cast external data to prevent query errors and to prevent sql injection.

 

3) the query statements must be separated by a ;. you don't have one after the first query.

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.