I have something that looks like this. $t='yes'; for($i=0;$i<count($stuff);$i++) { echo $t=$make; echo $tt=$stuffl[$i]; } PHP: What I am trying to do make the same column equal the same variable. But when I try the code above all I get is array to show up in the mysql database instead of what equals $t. Any pointers or suggestions would be appreciated.
$stuff= a bunch of numbers I am getting that to fill in right. Its just repeating the of the $t inside the for {} I tried adding the [$i] but then I get array that shows up inside every column. Instead of the word yes $t='yes'; $stuff=numbers etc... $make='yes'; for($i=0;$i<count($stuff);$i++) { echo $t=$make; echo $tt=$stuff[$i]; $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')"; $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error()); } PHP:
Is count($stuff) not supposed to be count($stuffl)? Your weird variable/table/cell naming makes debugging hard as we don't know what these things are supposed to represent. Again, the query is outside the loop, so it only runs once, using the last values in your array. Please post the contents of $stuff. Is it a multidimensional array? What does "echo $tt;" in the loop output?
Thanks.. I did a quick markup. I have corrected the example code. My ultimate goal is to get the mysql table to look like this hi stuff yes 5 yes 8 yes 9 yes 10 yes 11 yes 12 Now with the code above I am getting array 5 array 8 array 9 array 10 array 11 array 12 All I am trying to do is get my vaibale $t='yes'; into my column.. But I have to repeat the variable thats what I am having trouble with. I can get it to go into the first row like this. yes 5 array 8 array 9 array 10 array 11 array 12 array 8 array 9 array 10 array 11 array 12
$t='yes'; $stuff=numbers etc... $make='yes'; for($i=0;$i<count($stuff);$i++) { $t=$make; // you might as well not bother with this one! $tt=$stuff[$i]; $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')"; $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error()); } PHP:
I found out how to do this a different way, however huggystudios I did use it with part of your answer. What I did was just run another foreach inside the for() Thank you very much for everyone's help. $t='yes'; $stuff=numbers etc... $make='yes'; for($i=0;$i<count($stuff);$i++) { foreach(') as $make) { echo $t=$make; } echo $tt=$stuff[$i]; $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')"; $r = mysqli_query ($dbconnect, $q) or die ("Update query failed : " . mysql_error()); } PHP:
I'm sorry, but your code still doesn't make any sense. First of all, that you get an array into the db, makes no sense - nowhere are you assigning an array. You're having two variables doing the same thing - $t = 'yes'; means $t is 'yes', and $make = 'yes'; means $make is 'yes'. You're doing a for-loop on $stuff, and since you're declaring both $t and $make outside the loop, they should stay the same regardless - hence, doing the for-loop without the foreach should yield the result you're after. Hence, the following code should work just fine: $t = 'yes'; $stuff = array(1,5,9,17,29); //example array for ($i = 0; $i < count($stuff); $i++) { $tt = $stuff[$i]; $q = "INSERT INTO something (`hi`, `stuff`) VALUES ('$t', '$tt')"; mysqli_query($dbconnect, $q) or die ("Update query failed : " . mysql_error()); } PHP: That should do exactly what you're trying to accomplish, given the examples you provided.
This is also a stellar example of when prepared queries would be VASTLY superior, since you can prepare once and then bind/execute many times. I'd also ditch the stupid array indexing for loop for a foreach since you aren't actually doing anything with the index... and lose that $t variable since that doesn't actually seem to be doing anything. * note * the following uses php 5.4+ "javascript style" array declaration. If it throws an error, update your PHP to something modern... I'm also assuming $dbconnect is a mysqli object. $stuff = [1,5,9,17,29]; //example array $statement = $dbconnect->prepare(' INSERT INTO something ( hi, stuff ) VALUES ( ?, ? ) '); foreach ($stuff as $value) { $statement->bindParam('ss', 'yes', $value); $statement->execute(); } Code (markup): A LOT simpler... and one of the entire reasons we're supposed to be using mysqli or PDO instead of the old mysql_ functions... and if you turn off prepare emulation, it'll run faster since the query is actually sent to the engine, then for each iteration you're only sending the new data.