Jump to content

Help Needed


Danny620

Recommended Posts

Hello i have coded a script that checks the post values then updates db recoreds thing is.

 

i have this line of code if($name && $price && $ship && $paypal && $des && $cato_id !== ""){ do code }else{ do this } when the client enters 0 in the ship and price the do code does not get excuted how can i solve this problem thanks

 

<?php # Script 10.3 - upload_image.php
	  
	  // This page is for editing a product.
		require_once(MYSQL);;

// Check for a valid product ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {

$id = $_GET['id'];

// Make the query:
	$q = "SELECT name, price, ship, paypal, descr FROM products WHERE product_id='$id' LIMIT 1";		
	$r = mysqli_query ($dbc, $q);

	if(mysqli_affected_rows($dbc) == 1) {//Ran Ok ready to delete file

	$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

	}

} else { 
die('<p class="error">Sorry this page is not available or has been accessed in error</p>');
}

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

		require_once(MYSQL);

// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);

// Assume invalid values:
$name = $price = $ship = $paypal = $des = $cato_id = FALSE;

// Check for a product name:
if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['name'])) {
	$name = mysqli_real_escape_string ($dbc, $trimmed['name']);
} else {
	echo '<p class="error">Please enter a product name</p>';
}

// Check for a price
if (is_numeric($trimmed['price'])) {
	$price = mysqli_real_escape_string ($dbc, $trimmed['price']);
} else {
	echo '<p class="error">Please enter a price</p>';
}

// Check for a price
if (is_numeric($trimmed['shipping_cost'])) {
	$ship = mysqli_real_escape_string ($dbc, $trimmed['shipping_cost']);
} else {
	echo '<p class="error">Please enter a shipping cost</p>';
}

// Check for paypal code:
if ($_POST['paypal']) {
	$paypal = mysqli_real_escape_string ($dbc, $trimmed['paypal']);
} else {
	echo '<p class="error">Please enter the paypal code</p>';
}

// Check for product description:
if ($_POST['product_des']) {
	$des = mysqli_real_escape_string ($dbc, $trimmed['product_des']);
} else {
	echo '<p class="error">Please enter a product description</p>';
}

// Check for product description:
if ($_POST['cato_id']) {
	$cato_id = mysqli_real_escape_string ($dbc, $trimmed['cato_id']);
} else {
	echo '<p class="error">Please Select a category</p>';
}

if ($name && $price && $ship && $paypal && $des && $cato_id !== "") { // If everything's OK...

$q = "UPDATE products SET name='$name', price='$price', ship='$ship', paypal='$paypal', descr='$des', cato='$cato_id' WHERE product_id='$id'";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

			echo '<p class="success"><em>The Product Information has Been Updated</em></p>';

	}

	if($_POST['oldimage'] == 0){

// Check for an uploaded file:
if (isset($_FILES['upload'])) {

	// Validate the type. Should be JPEG or PNG.
	$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
	if (in_array($_FILES['upload']['type'], $allowed)) {

	function findexts ($filename) 
	 { 
		$filename = strtolower($filename) ; 
	 $exts = split("[/\\.]", $filename) ; 
		$n = count($exts)-1; 
		$exts = $exts[$n]; 
	 return $exts; 
		} 

		//This applies the function to our file  
		$ext = findexts ($_FILES['upload']['name']);

	 //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
			$timestamp = time();

		//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
			$default_name = $timestamp.".";

		//This assigns the subdirectory you want to save into... make sure it exists!
			$target = TARGET;
		$image = "/photos/";

	//This combines the directory, the random file name, and the extension
		$target = $target . $default_name.$ext; 
		$image = $image . $default_name.$ext;

		// Move the file over.
		if (move_uploaded_file ($_FILES['upload']['tmp_name'], "$target")) {

		// Make the query:
	$q = "SELECT image FROM products WHERE product_id='$id' LIMIT 1";		
	$r = @mysqli_query ($dbc, $q);

	if(mysqli_affected_rows($dbc) == 1) {//Ran Ok ready to delete file

	$row = @mysqli_fetch_array ($r, MYSQLI_NUM);

	$q = "UPDATE products SET image='$image' WHERE product_id='$id'";	
	$r = @mysqli_query ($dbc, $q);

//Delete from file
$myFile = DELPATH . $row[0];

	if (file_exists($myFile)) {
		//Delete from file
			if(unlink($myFile)){ echo "<p class=\"success\">File: $row[0] has been successfully deleted</p>"; }
							   }

		echo '<p class="success"><em>The Product Image has Been Changed</em></p>';

		}

		} // End of move... IF.

	} else { // Invalid type.
		echo '<p class="error">Please upload a JPEG or PNG image.</p>';
	}

// Check for an error:

if ($_FILES['upload']['error'] > 0) {
	echo '<p class="error">The file could not be uploaded because: <strong>';

	// Print a message based upon the error.
	switch ($_FILES['upload']['error']) {
		case 1:
			print 'The file exceeds the upload_max_filesize setting in php.ini.';
			break;
		case 2:
			print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
			break;
		case 3:
			print 'The file was only partially uploaded.';
			break;
		case 4:
			print 'No file was uploaded.';
			break;
		case 6:
			print 'No temporary folder was available.';
			break;
		case 7:
			print 'Unable to write to the disk.';
			break;
		case 8:
			print 'File upload stopped.';
			break;
		default:
			print 'A system error occurred.';
			break;
	} // End of switch.

	print '</strong></p>';

} // End of error IF.

// Delete the file if it still exists:
if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
	unlink ($_FILES['upload']['tmp_name']);
}

} // End of isset($_FILES['upload']) IF.

}

}

Link to comment
Share on other sites

The line:

if($name && $price && $ship && $paypal && $des && $cato_id !== ""){ do code }else{ do this }

 

Means

IF

$name doesn't equal 0 or false

AND

$price doesn't equal 0 or false

AND

$ship doesn't equal 0 or false

AND

$paypal doesn't equal 0 or false

AND

$des doesn't equal 0 or false

AND

$cato_id doesn't equal ""

THEN

do  code

ELSE

Do this

 

 

You need to tell PHP EXACTLY what you are doing. Your code should probably look like:

if(!empty($name) && !empty($price) && !empty($ship) && !empty($paypal) && !empty($des) && !empty($cato_id) {
do code
} else {
do this
}

 

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.