3

In simple terms I have a form which has three identical entry fields. The names are different; however, when posted they have the same structure just different name prefix (ie three systems have different name prefixes: they would be windowstitle, mactitle, linuxtitle etc).

Currently I have a process that will only work one namesake out ie windowstitle (if the form is filled out, of course)

The code looks something like this:

<?php
$title = $_POST['windowstitle'];
//validate info or redirect
if ($title != "" ) {
    $title = mysql_real_escape_string($title);
    $sql = "insert into newwindows (title) values ('$title');
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
?>

Also the form block looks something like this

<form action="newuserprocess.php" method="post" enctype="multipart/form-data">
<div class="form">
    <h3>Windows</h3>

    <!-- title of system name -->
    <p><label for="windowstitle"> edition of system </lable></p>
    <input type="text" name="windowstitle"  size=20 /><br />
    </div>
    <div class="form">
    <h3>Mac</h3>

    <!-- title of system name -->
    <p><label for="mactitle"> edition of system </lable></p>
    <input type="text" name="mactitle"  size=20 /><br />
    </div>
<p><input type="submit" id="submit" class="bigbutton" value="Upload" /></p>
</form>

However, that leaves other forms left out with the only difference being the db I wanted entered and the post value prefix different.

So I came up with what I thought was a clever solution:

<?php
$arr = array('windows', 'mac', 'linux');
foreach ($arr as &$value) {
    $title = $_POST['$valuetitle'];
    //validate info 
    if ($title != "" ) {
        $title = mysql_real_escape_string($title);
        $sql = "insert into new$value (title) values ('$title');
        $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
    }
?>

However, this does not work. I know partly why; because '' makes the variable appear as is, thus my $_Post will always come back as $value. Another reason is the same with my new$value database name. What is the proper format for this? How do I make this work?

4
  • Are you missing a quote on $sql = "insert into newwindows (title) values ('$title'); ? Commented Aug 22, 2012 at 1:20
  • $_POST[$value . "title"] - use the variable directly, don't quote it. Commented Aug 22, 2012 at 1:21
  • what are you trying to do exactly? this seems way more complicated than it needs to be Commented Aug 22, 2012 at 1:22
  • To be honest its for a small cms im attempting to build up from the ground up. The process has other functions such as uploading images and other text fields. Ive included the essentials so it can be applied to the bigger picture Commented Aug 22, 2012 at 1:33

3 Answers 3

2

you probably want

$title = $_POST[$value . 'title'];

and

$sql = "insert into new$value (title) values ('$title')";
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @databyss that solves that issue! my only other question atm is sql would it be the same thing? eg 'code' $sql = "insert into new". $value . "(title) values('$title')"; 'code'
@user1452407 Avoid code like that as you could fall foul of some sql injection.
your $sql statement is missing the " at the end of it. Also you don't need the ; inside the sql statement so $sql = "insert into new$value (title) values ('$title')"; should work.
@JamesPoulson I know that. However I have included the mysql_real_escape_string function to prevent injection.
Glad to be of assistance, although it really is advised to switch to mysqli_ or PDO.
|
1

Another reason is the same with my new$value database name. My question is what is the proper format for this?

I'd surround $value in brackets {$value} for clarity. Your format works but could be clearer. See some tests: http://ideone.com/A2kWU

Also, if you are not changing the values in array $arr then you should just use

foreach ($arr as $value) { //...

to prevent accidental changes. In this case it won't be a big deal, though, since you're just using the array once.

Comments

0

Edit your code like:

<?php
     $arr = array('windows', 'mac', 'linux');
     foreach ($arr as $value) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.