normower
normower

Reputation: 137

implode function returning an additional value

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.

Upvotes: 0

Views: 462

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 72991

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())

Upvotes: 4

Related Questions