0

This isn't anything overly complicated (I assume), I'm just not sure of the syntax for doing what I want. I'm trying to insert a value from an array into the database. Below achieves what I want to do, however I was wondering if it could be reformatted to the code below that.

Current code which does what I want:

$name = explode(" ",$fullName);
$firstName = $name[0];  
$lastName = $name[count($name) - 1];
mysql_query("INSERT INTO `person` VALUES(NULL, '$firstName', '$lastName',0)"));

What I want to know is if it can be formatted like this:

$name = explode(" ",$fullName);
mysql_query("INSERT INTO `person` VALUES(NULL, '$name[0]', '$name[count($name) - 1]',0)"));

I tried this earlier a few different ways and got an error, is it just an issue with syntax or is it something a bit deeper?

Oh, and I should add that the only time I actually got the insert to run fully, I ended up with Array[0] and Array[2] - 1 in the first_name and last_name columns respectively.

Thanks all, hopefully I've been clear enough. Need any more info, let me know.

7
  • $name = explode(" ",$fullName); mysql_query("INSERT INTO 'person' VALUES(NULL, '$name[0]', '$name[count($name) - 1]'); This code fetched an error?? What was the error? Formatting looks fine. Commented Apr 21, 2013 at 14:14
  • @RubyLovely Am I being stupid or is that not what I already did? Commented Apr 21, 2013 at 14:16
  • 1
    mysql_query("INSERT INTO person VALUES(NULL, '$name[0]', '".($name[count($name) - 1])."')"); more like this .. Commented Apr 21, 2013 at 14:16
  • @dbf Yeah just noticed that, must've typoed. Corrected it (I hadn't typoed in my actual PHP). Commented Apr 21, 2013 at 14:17
  • 1
    @Zackehh9lives hmm, didn't correct any typo, look at the difference again ;) Commented Apr 21, 2013 at 14:19

3 Answers 3

1

You may try this:

mysql_query("INSERT INTO `person` VALUES(NULL, '$name[0]', '".$name[count($name) - 1]."' ");
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't use chat speak on Stack Overflow. Take the time to write "you" instead of "u".
1

Try this:

$name = explode(" ",$fullName); 
$query="INSERT INTO person VALUES(NULL,'". $name[0]."','".$name[count($name) - 1]."'";
mysql_query($query);

Comments

0

Try this:

mysql_query("INSERT INTO `person` VALUES(NULL, '{$name[0]}', '{$name[count($name) - 1]}',0)");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.