JREAM Posted February 20, 2009 Share Posted February 20, 2009 Hey, basically Im trying to make this loop act like the statements below this box. But I keep getting an undefined index so the Array Values aren't reading through I think. I tried a foreach loop, but my logic must be messed up on getting these arrays right, could anyone help me! So each item is like: $title = $_POST['title']; $slug= $_POST['slug']; etc .. $items = 'title, slug, breadcrumb, keywords, description, content, parent_id, position'; // Make the Array $values = explode(",", $items); // Loop Through Each Value for ($i = 0; $i < count($values); $i++) { echo $values[$i] . '=' . $_POST[$values[$i]]; } This one works perfectly, Im testing this and the above on the same page // This Works but I don't want to use it $title = mysql_real_escape_string($_POST['title']); $slug = mysql_real_escape_string($_POST['slug']); $breadcrumb = mysql_real_escape_string($_POST['breadcrumb']); $keywords = mysql_real_escape_string($_POST['keywords']); $description = mysql_real_escape_string($_POST['description']); $content = mysql_real_escape_string($_POST['content']); $parent_id = mysql_real_escape_string($_POST['parent_id']); $position = mysql_real_escape_string($_POST['position']); Link to comment https://forums.phpfreaks.com/topic/146087-array-troubles-name-_postname/ Share on other sites More sharing options...
redarrow Posted February 20, 2009 Share Posted February 20, 2009 Why the hell, You want to use the code like that is seems bad programming pratice. If you wanted to re use the varables, your need to use the array varable then the array name that a wast off time mate. use a function Link to comment https://forums.phpfreaks.com/topic/146087-array-troubles-name-_postname/#findComment-766921 Share on other sites More sharing options...
JonnoTheDev Posted February 20, 2009 Share Posted February 20, 2009 Waste of memory space using variables for each item in the $_POST, $_GET array. Use the data within the array foreach($_POST as $key => $val) { $_POST[$key] = mysql_real_escape_string($_POST[$key]); } mysql_query("INSERT INTO table SET name='".$_POST['name']."'"); Link to comment https://forums.phpfreaks.com/topic/146087-array-troubles-name-_postname/#findComment-766934 Share on other sites More sharing options...
JREAM Posted February 20, 2009 Author Share Posted February 20, 2009 Hey But I wanted the names also to be generated from the array like this so it generates the table ROW based on my function array list: INSERT INTO table SET $array[$name]='".$_POST[$array[$val]."'"); Because then I can re-use the same thing for my other forms for creating a page. I don't know how to retrieve the POST form name Link to comment https://forums.phpfreaks.com/topic/146087-array-troubles-name-_postname/#findComment-766940 Share on other sites More sharing options...
JonnoTheDev Posted February 20, 2009 Share Posted February 20, 2009 This is bad practice Link to comment https://forums.phpfreaks.com/topic/146087-array-troubles-name-_postname/#findComment-766944 Share on other sites More sharing options...
JREAM Posted February 20, 2009 Author Share Posted February 20, 2009 why? Link to comment https://forums.phpfreaks.com/topic/146087-array-troubles-name-_postname/#findComment-766945 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.