Jump to content

[SOLVED] $id++ (mysql_query)


mysterbx

Recommended Posts

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']."')");



}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.