mysterbx Posted February 7, 2008 Share Posted February 7, 2008 Hello, I was browsing thru my admin.php file... I saw some realy stupid things like: mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title1']."', '".$_POST['type1']."', '".$_POST['image1']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title2']."', '".$_POST['type2']."', '".$_POST['image2']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title3']."', '".$_POST['type3']."', '".$_POST['image3']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title4']."', '".$_POST['type4']."', '".$_POST['image4']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title5']."', '".$_POST['type5']."', '".$_POST['image5']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title6']."', '".$_POST['type6']."', '".$_POST['image6']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title7']."', '".$_POST['type7']."', '".$_POST['image7']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title8']."', '".$_POST['type8']."', '".$_POST['image8']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title9']."', '".$_POST['type9']."', '".$_POST['image9']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title10']."', '".$_POST['type10']."', '".$_POST['image10']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title11']."', '".$_POST['type11']."', '".$_POST['image11']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title12']."', '".$_POST['type12']."', '".$_POST['image12']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title13']."', '".$_POST['type13']."', '".$_POST['image13']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title14']."', '".$_POST['type14']."', '".$_POST['image14']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title15']."', '".$_POST['type15']."', '".$_POST['image15']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title16']."', '".$_POST['type16']."', '".$_POST['image16']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title17']."', '".$_POST['type17']."', '".$_POST['image17']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title18']."', '".$_POST['type18']."', '".$_POST['image18']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title19']."', '".$_POST['type19']."', '".$_POST['image19']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title20']."', '".$_POST['type20']."', '".$_POST['image20']."')"); mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title21']."', '".$_POST['type21']."', '".$_POST['image21']."')"); ... Is it possible to replace it with: $id++ mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title$id']."', '".$_POST['type$id']."', '".$_POST['image$id']."')"); something like this... (max id must be 27) Quote Link to comment Share on other sites More sharing options...
craygo Posted February 7, 2008 Share Posted February 7, 2008 yes you would use a for statement <?php for($id=1; $id<=27; $id++){ mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title$id']."', '".$_POST['type$id']."', '".$_POST['image$id']."')"); } ?> Ray Quote Link to comment Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 edit: nevermind Don't seem to understand what you mean, but from what I read I think you need to do this: <?php for( $id=1; $id<=27; $id++ ){ mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('".$_POST['title$id']."', '".$_POST['type$id']."', '".$_POST['image$id']."')"); } ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 Beware variable substitution inside single quote (') strings doesn't work. So i think a better method perhaps would be (borrowed from craygo's post) for( $id=1; $id<=27; $id++ ){ mysql_query("INSERT INTO `ads` (title, type, image) VALUES ('{$_POST['title'.$id]}', '{$_POST['type'.$id]}', '{$_POST['image'.$id]}')"); } I find where ".$var." is concerned you can replace it with {$var} which saves you of having to remember to come back in from the double quote (") escaping. Quote Link to comment Share on other sites More sharing options...
mysterbx Posted February 7, 2008 Author Share Posted February 7, 2008 I was just going to post a reply about it Thanks for the fast help everyone Quote Link to comment 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.