...

View Full Version : Problem with script structure ??



jeddi
06-09-2006, 07:27 AM
I am having a problem getting my script to continue processing after calling (request_once) another script.

There are three scripts involved.
Main one (Homes_add.php) calls the second (a_picts.php) which is a form validating script - which ofcourse calls its form (a_picts_fm.php)

Again - it is clear that the problem is not in returning from a_picts_fm back to a_picts - BUT the problem is returning from a_picts back to the main script ( homes_add script.)

I wonder if there is a fundamental structural problem ?

The main script (homes_add)
calls ( require_once ) the a_picts which processes the form.
The form itself on submisson re-runs the a_picts which when finshed should return to the main script (homes_add) and continue from where it left the off.

To summarise the problem is that the a_pict.php stops at the last line afer executing this :

echo " *** and got here";

IT DOESN'T RETURN TO HOMES_ADD.PRG

- dont know why ???

I have stripped a lot of the code to try and make it clearer.

:) If anyone can help solve this - I'd much appreciate it as I am a bit stuck ! :)


HERE IS THE MAIN - HOMES_ADD.PHP SCRIPT
IT CALLS THE PICTURE HANDLING SCRIPT



<?php
/*
* homes_add.php
*
* Called by : homes_ad_btn.php
*
* Call a_picts.php to get the pictures
*
* processes the homes_add_fm.php which is the
* FORM for adding a home.
*/

if (!isset($_POST['run_mn'])){

PREPARES DATA AND CALLS THE MAIN FORM ( NOT PICTURE FORM)

else { // BIG else - SO MAIN FORM HAS RUN

VALIDATES FORM DATA

/*
* Now we get the pictures.
*/
if( $pict == "y"){
require_once("a_picts.php"); // < -- HERE IS THE CALLING
exit(); // < -- I TRIES TAKING THIS OUT
} // endif
else{
$N_pix1=$N_pix2=$N_pix3=$N_pix4=$N_pix5="none";
} // end else

// I WANT TO RETURN BACK HERE

$sql = "UPDATE homes SET
pix1 = '$N_pix1',
pix2 = '$N_pix2',
pix3 = '$N_pix3',
pix4 = '$N_pix4',
pix5 = '$N_pix5'

WHERE ad_ref = '$A_ref' ";

mysql_query($sql)
or die("could not execute HOMES UPDATE PICTURES query");

/*
* Update the client table
*/
ETC ETC

} // end BIG else

?>





THIS IS THE SECOND SCRIPT THAT CALLS AND THEN PROCESSES
THE FORM




if(isset($_POST['run'])){ // BIG IF

PROCESSES THE PICT_FM.PHP FORM

echo "got here";
} // end BIG IF

else {
$message1 = "none";
$message2 = "none";
require_once ("a_picts_fm.php"); // CALLS THE FORM
exit();
} // end else

echo " *** and got here"; // THIS WHERE IT STOPS

?>



AND THIS IS THE FORM THAT THE ABOVE REQUIRES




<?php
/*
* picts_fm.php
*
* Called by : picts.php
* IT THEN GOES BACK TO PICTS.PHP
*
* FORM for adding images.
*
*/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">


<form name="main_fm" enctype ="multipart/form-data" action="a_picts.php" method="POST">
<input type = 'hidden' name='run' value="on">
<input type="hidden" name ="MAX FILE SIZE" value="500000">
<input type = 'hidden' name='adref' value="<?php echo $A_ref ?>">

<div style='position:absolute; left:40px; top:320px' >
<b>First image:</b><br />
<input type="file" size="50" id = "u1" name="upLoad1" onchange="showImg1()"><br />
<input type="text" size="50" id = "n1" name="fName1" readonly>
<input type="button" value ="Clear" onclick="document.main_fm.upLoad1.value='';document.main_fm.fName1.value='';document.getElementById('image1') .src='';document.getElementById('image1').style.display='none'">
</div>

<div style='position:absolute; left:500px; top:262px' >
<img height='160' width='160' id="image1" style='display:none' />
</div>
</form>
</body>
</html>

takin
06-09-2006, 11:10 AM
From what you've shown above, the code is being fully executed. It seems to me that in most cases, there's nothing to query. Even on the first pass where you have to show the form, you're running a query. That query will probably be

UPDATE homes SET
pix1 = '',
pix2 = '',
...
WHERE ad_ref='xxx';

The best way to test this is to echo the query to the screen before you run it. Even better, wrap your queries in a function so that you can do that automatically for every query (and 'sanitise' it) then remove for all queries once you're satisfied with your code.

jeddi
06-09-2006, 02:23 PM
Hi - thanks for the response

You wrotr something very interesting that I need to learn about :)


Even better, wrap your queries in a function so that you can do that automatically for every query (and 'sanitise' it) then remove for all queries once you're satisfied with your code.

Can you please give me an example on this type of function and how I should use it ?

Thanks

takin
06-09-2006, 02:36 PM
My general function is like this:


function query($query) {
if (!$result[0] = mysql_query($query)) {
die ("query failed: $query\n" . echo mysql_error());
}
$result[1] = mysql_num_rows();
}

...

$query = "SELECT ...";
$result = query($query);
if ($result[1]) {
while ($row = mysql_fetch_assoc($result[0])) {
...
}
}

But you could always put an 'echo $query' in the first line of the function and even a strip_slashes($query) before that.

jeddi
06-09-2006, 04:23 PM
Thanks

- just one bit I don't follow

what does this mean ?
if ($result[1]) {

takin
06-10-2006, 02:28 AM
In the function, I said

$result[1]=mysql_num_rows()
to check whether there were any results.

In the main body, I use that variable to check whether I should process the data or not. I often find myself getting errors early in a databases life because there's nothing to process.

Come to think of it, you don't need the if ($result[1]) if you're using a while loop. Only if you jump straight into the processing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum