Jump to content

if empty/isset


Q695
Go to solution Solved by Q695,

Recommended Posts

I heed help with a logic/basic scripting check that allows people to change data if it's not being pulled:

if( empty($_GET['____________'])){
insert statement

} else if( isset($_GET['___________'])){

update statement

}
it's going to the insert statement when it should be going to the update statement.

 

Link to comment
Share on other sites

I'm thinking I should try rewriting it from step 1, because many things are working, but some seem screwed up.  Below is the code, but now it's not naming the image, or uploading it after I tried fixing it:

image uploader

<?php
//$path folder of location being uploaded to on recieving page
//$path full path of image
//$table_name name of table being checked
//$var_check name of variable in table
//$path_rand image path in directory
//$user primary criteria
//$id criteria item
//$limitor secondary criteria
//$item secondary criteria item
//$picture picture item name

if ($_FILES['creature_image']['error']=0){
	// loads image variables
	$name = $_FILES['creature_image']['name'];
	$temp = $_FILES['creature_image']['tmp_name'];
	$type = $_FILES['creature_image']['type'];
	$size = $_FILES['creature_image']['size'];
	
	//size of uploaded image
	$img_size = getimagesize( $temp );
	$width=	$img_size[0];
	$height= $img_size[1];
	
	
	
	//requirements
	$maxwidth = 1280;
	$maxheight = 1280;
	$maxsize= 1048576;
	$allowed = array('image/jpeg' , 'image/png' , 'image/gif');
	
	//revisions
	$new_width=300;
	$new_height=300;


if (in_array($type, $allowed)){
	if ( empty($_GET['creature']) ){
	$m=0;
	
		while ($m <= 100) {
		//random charicter generator
			$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$randomString = '';
			for ($i = 0; $i < 64; $i++) {
				$randomString .= $characters[rand(0, strlen($characters) - 1)];
			}
		$path_rand = $randomString;
		
		$sql_img_test= "SELECT * FROM $table_name WHERE $var_check='$path_rand' limit 1";
		$result_img = mysql_query($sql_img_test, $con) or die ("can not generate result " . mysql_error());
		$row_img = mysql_fetch_assoc($result_img); 
		if (!$row_img){
			
			$m=101;
		} else {
			$m++;
		}
		//end of new entry
		}
	}
	else {
	$sql_img_test= "SELECT * FROM $table_name WHERE $user='$id' && $limitor=$item limit 1";
	$result_img = mysql_query($sql_img_test, $con) or die ("can not generate result " . mysql_error());
	$row_img = mysql_fetch_assoc($result_img); 
	$path_rand=$row_img[$picture];
	echo "image updated";
	}

}



//image load path
$path= "$path$path_rand.jpg";
//echo $path;
// uploading file, and create database entry
move_uploaded_file($temp,$path); //places image in file location
} 
else {
	echo "Image not uploaded.";
}

?>

the database inserter:

<?php
$path="images/new_creatures/ ";
$table_name="creature_create";
$var_check= "picture";
$user = 'user';
//$id = '$id';
$limitor = 'creature_id';
$picture='picture';
if (isset($_GET['creature'])){
$item = $_GET['creature'];
}


if (
	isset($_POST['creature_name']) &&
	isset($_FILES['creature_image']) &&
	isset($_POST['def_rate']) &&
	isset($_POST['def_rate_range']) &&
	isset($_POST['att_rate']) &&
	isset($_POST['att_rate_range']) &&
	isset($_POST['def']) &&
	isset($_POST['def_range']) &&
	isset($_POST['att']) &&
	isset($_POST['att_range']) &&
	isset($_POST['health']) &&
	isset($_POST['health_range']) ){
	include "image_upload.php";
	$name = $_POST['creature_name'];
	//$image = $path_rand;
	$def_rate = $_POST['def_rate'];
	$def_rate_range=$_POST['def_rate_range'];
	$att_rate=$_POST['att_rate'];
	$att_rate_range=$_POST['att_rate_range'];
	$def=$_POST['def'];
	$def_range=$_POST['def_range'];
	$att=$_POST['att'];
	$att_range=$_POST['att_range'];
	$health=$_POST['health'];
	$health_range=$_POST['health_range'];
	
	
	if( empty($_GET['creture'])){
	
	echo "empty";
	$sql_new_creature="INSERT INTO creature_create
	(`user`, `name`, `def_rate`, `def_rate_dif`, `att_rate`, `att_rate_dif`, `def`, `def_dif`, `att`, `att_dif`, `health`, `health_dif`) VALUES 
	('$id', '$name', '$def_rate', '$def_rate_range', '$att_rate', '$att_rate_range',
	'$def', '$def_range', '$att', '$att_range', '$health', '$health_range');";
	//echo $sql_new_creature;
	$sql_creature = "SELECT * FROM `creature_create` WHERE user=$id LIMIT 1";
	$result_creature = mysql_query($sql_creature, $con) or die ("can not generate result " . mysql_error());
	$row_creature = mysql_fetch_assoc($result_creature);
	
	$path_rand=$row_creature['picture'];
	
	} else if( isset($_GET['creture'])){
	//	echo "set";
	$creature=$_GET['creture'];
	
	$sql_new_creature="
	UPDATE creature_create SET
	`name` = '$name',
	`def_rate` = '$def_rate',
	`def_rate_dif` = '$def_rate_range',
	`att_rate` = '$att_rate',
	`att_rate_dif` = '$att_rate_range',
	`def` = '$def',
	`def_dif` = '$def_range',
	`att` = '$att',
	`att_dif` = '$att_range',
	`health` = '$health',
	`health_dif` = '$health_range'
	WHERE `creature_id` = '$creature';
	";
	
	/*$sql_creature = "SELECT * FROM `creature_create` WHERE user=$id LIMIT 1";
	$result_creature = mysql_query($sql_creature, $con) or die ("can not generate result " . mysql_error());
	$row_creature = mysql_fetch_assoc($result_creature);
	*/
	$path_rand=$row_creature['picture'];
	
	//echo $sql_new_creature;
	}
	
	}
else { echo "error";
	$sql_creature = "SELECT * FROM `creature_create` WHERE user=$id LIMIT 1";
	$result_creature = mysql_query($sql_creature, $con) or die ("can not generate result " . mysql_error());
	$row_creature = mysql_fetch_assoc($result_creature);
	
	$path_rand=$row_creature['picture'];

}
$result = mysql_query($sql_new_creature, $con) or die ("can not generate result " . mysql_error());



          $def_rate_max=$def_rate+$def_rate_range;
          $att_rate_max=$att_rate+$att_rate_range;
          $def_max=$def+$def_range;
          $att_max=$att+$att_range;

echo"
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
	<tr>
		<td align='center' colspan='2'>
		<h1>$name</h1>
		<br>
		<img src='images/new_creatures/ $path_rand.jpg' width='200' height='200'>
		</td>
	</tr>
		<tr>
			<td>Attack Rate: $att_rate - $att_rate_max</td>
			<td>Defense Rate: $def_rate - $def_rate_max</td>
		</tr>
		<tr>
			<td>Attack: $att - $att_max</td>
			<td>Defense: $def - $def_max</td>
		</tr>
</table>
";

?>

It will upload/rewrite an image/content for user added content before a submission for a suggested addition to a game.

Edited by Q695
Link to comment
Share on other sites

1) A single equals-sign is ASSIGNMENT, it takes two to COMPARE:

 

if ($_FILES['creature_image']['error']=0){

 

2) It looks like you spelled the GET variable wrong:

 

if (isset($_GET['creature'])){
$item = $_GET['creature'];
}

...

	if( empty($_GET['creture'])){
	
	echo "empty";
	$sql_new_creature="INSERT INTO creature_create

 

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.