why wont this work?

Status
Not open for further replies.

pisoj1

Active Member
141
2010
0
0
hey guys, trying to make a function anyway why wont this insert data into the table
Code:
  $Connection = mysql_open();
  $likes= array();
  foreach($likes as $like)
  {
  $insert3 = "insert into ProfileInterests " .
  	"values ('$id', $like, null)";
  $result3 = @ mysql_query ($insert3, $connection)
  	or showerror();
  mysql_close($connection)
  	or showerror();
  }

Without the loop it would add 1 line of the array...but not all of them...any idea what to do?
 
4 comments
Well, after looking at your code (looks different) to what I normally use;

Could try this;

Find:
PHP:
$Connection = mysql_open();

Replace:
PHP:
$connection = mysql_open();

Rest is lowercase.
 
If that's the entire code you're using, the array you're using is null; in your insert statement, you're missing the column name declarations before the values list; you're placing the mysql_close() function within your foreach() loop so after the first run, it would close your SQL connection. Also I'm not sure if mysql_open() is even a function, mysql_close() is however.

Here is how you would connect to your database. Replace the info with your own:

PHP:
<?php

$dbserver = 'yourserver';
$dbuser = 'username';
$dbname = 'database';
$dbpass = 'password';

$connect = mysql_connect($dbserver, $dbuser, $dbpass) or die(mysql_error());
$database = mysql_select_db($dbname) or die("Cannot select DB");

?>
You can put that into a file on your server called connect.php for instance. Now in all of your other pages, you can simply include this file and you'll be connected and ready to go. Here is your code posted above with just a test array with random info and the connect.php file included:

PHP:
<?php

include_once 'connect.php';

$likes = array("key1" => "value1", "key2" => "value2");

foreach($likes as $key) {
     mysql_query( "INSERT INTO ProfileInterests (id, like, null) VALUES ('$id', '$key', null)" ) or die(mysql_error());
}

mysql_close(); //You don't have to use this unless you have persistent connects

?>
 
You could be awesome, AdM.as and do it in one query (MySQL only I guess):

PHP:
$likes = array("key1" => "value1", "key2" => "value2");
$jmz = array();
foreach($likes as $li => $ke) $jmz[] = "('" . $li . "', '" . $ke . "', null)";
mysql_query("INSERT INTO ProfileInterests VALUES " . implode(",", $jmz));
 
Status
Not open for further replies.
Back
Top