Jump to content

Recommended Posts

hello, i am hoping someone can help, i have multiple post results that need to go one by one into a table

 

heres the form i have:

	echo '<h2>Update Variation Values</h2>';
	echo '<div id ="content_form">';
	echo '<form method="post" action="">';
	echo '<input type="hidden" name="option_id" value="' . $option_id . '">';
	echo '<input type="hidden" name="variation_id" value="' . $variation_id . '">';
	echo '<input type="hidden" name="product_name" value="' . $product_name . '">';
	$sql = "SELECT * FROM store_option$variation_id WHERE product = '$product_name'";
	$res = mysql_query($sql) or die(mysql_error());
	while ($row = mysql_fetch_assoc($res)) {

		echo '<div id="content_title"><label for="title[]">Title:</label></div>';
		echo '<div id="content_box"><input name="title[]" type="text" id="title" value="' . $row['title'] . '" /></div>';
		echo '<div id="content_title"><label for="price[]">Price:</label></div>';
		echo '<div id="content_box"><input name="price[]" type="text" id="price[]" value="' . $row['price'] . '" /></div>';
	}
	echo '<div id="content_body"><input type="submit" name="post_update_variation_value" value="Save Values"></div></form>';
	echo '</div>';
}

heres my current code

	echo '<pre>',print_r($_POST,true);

foreach($_POST['title'] as $key => $value){
if($value !=''){

	$sql = "UPDATE store_option$variation_id SET title = '$value', price = '$price' WHERE product = '$product_name'";
	$res = mysql_query($sql) or die(mysql_error());
} else {
	echo "Option $key is empty<br />";
}
}

its currently just doing the last result and filling up the database with that result, many thanks all.

 

why do you have a string in an array loop :confused:

 

 

i read you could use the (array) type cast inside the foreach and it should be fine not sure if this is true or not to convert a string to array inside a foreach loop.

 

normally if it was a string you would be getting "invalid argument foreach" error.

 

also might change != to !==

 

 

if i use this code, i get this message: Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\jubileeleather\site\admin\_class\store.php on line 654

 

	$option_id = mysql_real_escape_string($_POST['option_id']);
	$variation_id = mysql_real_escape_string($_POST['variation_id']);
	$product_name = mysql_real_escape_string($_POST['product_name']);

	foreach($_POST['title'] as $key1 => $value1){

		foreach($_POST['price'] as $key2 => $value2){

			foreach($_POST['option_value_id'] as $key3 => $value3){

				$sql = "UPDATE store_option$variation_id SET title = '$value1', price = '$value2' WHERE id = '$value3'";
				$res = mysql_query($sql) or die(mysql_error());
			}
		}
	}
	echo '<script>window.location="dashboard.php?page=store_variation"</script>';
}

thanks for your reply, i have tried this, but only the first field is changed in the database.

	function post_update_variation_value() {

	$option_id = mysql_real_escape_string($_POST['option_id']);
	$variation_id = mysql_real_escape_string($_POST['variation_id']);
	$product_name = mysql_real_escape_string($_POST['product_name']);

	foreach ($_POST['title'] as $key => $value) {
  		$title = $value;
  		$price = $_POST['price'][$key];

  		// execute your query
	$sql = "UPDATE store_option$variation_id SET title = '$title', price = '$price' WHERE id = '$key'";
	$res = mysql_query($sql) or die(mysql_error());
	}
	echo '<script>window.location="dashboard.php?page=store_variation"</script>';
}
}

yea realised now, tried adding this to the code now all fine, thanks so much for your help :)

 

  $price = $_POST['price'][$key];

$id = $_POST['option_value_id'][$key];

 

  // execute your query

$sql = "UPDATE store_option$variation_id SET title = '$title', price = '$price' WHERE id = '$id'";

$res = mysql_query($sql) or die(mysql_error());

I would use the $key when making your form, that way you know things are going to be held together as they should be:

 

echo '<div id="content_title"><label for="title[' . $row['id'] . ']">Title:</label></div>';
echo '<div id="content_box"><input name="title[' . $row['id'] . ']" type="text" id="title[' . $row['id'] . ']" value="' . $row['title'] . '" /></div>';
echo '<div id="content_title"><label for="price[' . $row['id'] . ']">Price:</label></div>';
echo '<div id="content_box"><input name="price[' . $row['id'] . ']" type="text" id="price[' . $row['id'] . ']" value="' . $row['price'] . '" /></div>';

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.