unidox Posted May 30, 2008 Share Posted May 30, 2008 Well I have an array that I turn into variables, and I need to insert it into sql. I was thinking of using a count function to count the array, but the problem is how would I insert it into the sql, I was thinking using a counter var, and a while statement. Would that work? Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/ Share on other sites More sharing options...
BillyBoB Posted May 30, 2008 Share Posted May 30, 2008 Or you could eliminate the process of turning them into vars. <?php foreach($array as $value) { //insert the value into the database. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553833 Share on other sites More sharing options...
wildteen88 Posted May 30, 2008 Share Posted May 30, 2008 Post your current code. I'm not quite understanding your question seems you may be over complicating things. Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553834 Share on other sites More sharing options...
DarkWater Posted May 30, 2008 Share Posted May 30, 2008 Please elaborate a bit and post some code. It's probably not as complicated as you're making it out to be. Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553835 Share on other sites More sharing options...
unidox Posted May 30, 2008 Author Share Posted May 30, 2008 Here: function add($array, $link, $log) { foreach ($array as $v) { if (preg_match("/password/i", $v)) { eval("$" . $v . " = " . pass($_POST[$v]) . ";"); if ($password == "") { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_4"); exit(); } elseif ($password_check == "") { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_5"); exit(); } else { if ($password != $password_check) { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_6"); exit(); } } } elseif (preg_match("/url/i", $v)) { eval("$" . $v . " = " . url($_POST[$v]) . ";"); if ($url == "") { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_7"); exit(); } } elseif (preg_match("/level/i", $v)) { if ($this->check == true) { eval("$" . $v . " = " . escape_data($_POST[$v]) . ";"); if ($level == 1) { $level = $this->chklvl("Users"); } if (!is_numeric($level)) { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_8"); exit(); } elseif ($level == "") { $level = $this->chklvl("Users"); } $q = mysql_query("SELECT * FROM `pcp_groups` WHERE `level` = '$level'") or die(mysql_error()); $r = mysql_fetch_array($q); $group = $r['name']; } else { $level = $this->chklvl("Users"); } } elseif (preg_match("/active/i", $v)) { eval("$" . $v . " = " . escape_data($_POST[$v]) . ";"); if (!is_numeric($active)) { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_9"); exit(); } elseif ($active == "") { $active = 2; } } else { eval("$" . $v . " = " . escape_data($_POST[$v]) . ";"); if (preg_match("/_required/i", $v)) { if ("$" . $v . "" == "") { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_10&field=" . $v . ""); exit(); } } } } if (($username != "") || ($email != "")) { $q = mysql_query("SELECT * FROM `pcp_users` WHERE `username` = '$username' || `email` = '$email'"); $r = mysql_fetch_array($q); if (($r['username'] == $username_required)) { // If there is already a member in the db header("Location: index.php?p=users&s=add_user&e=6"); // Redirects to error "Sorry, that username/email is already in use, please use forget password to retrieve your account.". exit(); } elseif (($r['email'] == $email_required)) { // If there is already a member in the db header("Location: index.php?p=users&s=add_user&e=7"); // Redirects to error "Sorry, that username/email is already in use, please use forget password to retrieve your account.". } } $this->create_log("Added " . $log . ": \"" . $username . "\"", $user_name); } I just need to insert it into sql now. Which I am trying to figure out. Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553838 Share on other sites More sharing options...
wildteen88 Posted May 30, 2008 Share Posted May 30, 2008 How is your array formatted? Post the output of print_r Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553839 Share on other sites More sharing options...
DarkWater Posted May 30, 2008 Share Posted May 30, 2008 Insert what into SQL now? >_> Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553840 Share on other sites More sharing options...
unidox Posted May 30, 2008 Author Share Posted May 30, 2008 Notice how I set each string in the array into a variable. I am trying to insert each data into sql. Like: mysql_query("INSERT INTO `" . $this->table . "` ($var) VALUES ('$var')") or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/108049-while-in-a-var/#findComment-553843 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.