0

so I have:

$input=array($fname,$lname);

I then use:

$valueString = join(",",$values);
    echo $valueString;

if I had input $fname = a and $lname = f I then receive:

a,fa,f

Not sure why I get the 'fa' value and how to edit my code so I get just a,f

apologies for the poor question, though it would be simpler

this is the function;

 function insert($coloumn, $values, $table)
{

    $valueString = join(",",$values);
    echo $valueString;
    $insert = "INSERT INTO $table ($coloumn)
        VALUES ($valueString)";
    //$query = $this->dbLocalhost->query($insert)
    //or die("could not insert:".mysql_error());

    if($this->dbLocalhost->query($insert))
    {
    header('Location: blah.php');
    }

}

and the this the page for the inputs:

   <?php
session_start();
require_once("database.php");
$db = new Database();
$table = "tables";
$coloumn = "firstname, lastname";
if(isset($_POST['Add']))
{

    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];



    $input=array($fname,$lname);

    $db->emptyCheck($coloumn, $input, $table);



}
?>
<html>
    <head>
        <title>Add new Record</title>
    </head>
    <body>
        <br><br><br><br><br>
        <div id ="formAlign" align="center">
        <form action="" method ="post">
            <label for="firstname"> First Name:</label>
            <input type ="text" name="firstname">
            <label for="lastname"> Last Name:</label>
            <input type="text" name="lastname">

            <input type="submit" name="Add">
        </form>
        </div>
    </body>
</html>

To clarify

$db->emptyCheck($coloumn, $input, $table);

leads to:

$this->insert($coloumn, $values, $table);

being called.

5
  • what is $values? Are you sure it's $valueString = join(",",$values);? or $valueString = join(",",$input); Commented Nov 15, 2012 at 13:07
  • Your code cannot do so, unless you run it twice. Commented Nov 15, 2012 at 13:08
  • I'm pretty sure you just get two a,f because you're outputting it twice. Please show a complete code sample that reproduces this problem. Commented Nov 15, 2012 at 13:08
  • amended the question with more code, sorry about that. Commented Nov 15, 2012 at 13:14
  • That's still not a complete code sample we could check. var_dump($values) right before you join them, var_dump any other values you're outputting (not echo) so you can actually distinguish between different output. If you still see the same problem, let us know. Commented Nov 15, 2012 at 13:20

1 Answer 1

4

implode() works fine. You are outputting the result of implode() twice.

'a,f' . 'a,f' = 'a,fa,f'

Note: I encourage using the root function and not the alias. (e.g. implode() vs. join())

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i noticed that the function was being called twice and I change join to implode.

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.