I am new to PHP/MySQL and currently working on a project to learn it a little better. I have a bit of PHP code and it has a problem that I'm having a little trouble figuring out.

Basically, it is taking session data and writing it to a MySQL database. Here is the entire code:

<?php

$con = mysql_connect("localhost","my_username","mypassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("myserver_imaging", $con);

mysql_query ("INSERT INTO imaging (os,MAC,Model,AntiVirus,Browser,Email,Connectivity,Sound,Ports) VALUES ('".$_SESSION['imaging2']."','".$_SESSION['imaging3']."','".$_SESSION['imaging4']."','".$_SESSION['antivirus']."','".$_SESSION['browser']."','".$_SESSION['email']."','".$_SESSION['connectivity']."','".$_SESSION['sound']."','".$_SESSION['ports']."')");
OR die("Could not update: ".mysql_error());

mysql_close($con);
?>

Now here's the problem. When I remove line 12 (the OR die statement), the data gets written to the database, but it errors out. I'm using Joomla 2.5 with the Sourcerer plug-in to insert PHP into my pages, and the error messages point directly to a couple modules in my template.

When I add the statement back in, the page redirects where it is supposed to, but the data doesn't get written to the database.

I know this question would best be directed to those using Joomla, but I'm also curious to know if there is a problem with my syntax in line 12. This isn't a huge deal since it is only a project, but I would still like to know if the problem is with my syntax.

Any suggestions or comments would be very much appreciated! Thanks for checking into it with me.

Remove the semicolin from the end of line 11. So lines 11 and 12 should be all one line of code as such:

mysql_query ("INSERT INTO imaging (os,MAC,Model,AntiVirus,Browser,Email,Connectivity,Sound,Ports) VALUES ('".$_SESSION['imaging2']."','".$_SESSION['imaging3']."','".$_SESSION['imaging4']."','".$_SESSION['antivirus']."','".$_SESSION['browser']."','".$_SESSION['email']."','".$_SESSION['connectivity']."','".$_SESSION['sound']."','".$_SESSION['ports']."')") OR die("Could not update: ".mysql_error());
commented: Thanks for the suggestion on cleaning up my syntax! +0

Hi,

let me test it on my joomla, but try dschuett's suggestion first. It should work...

UPDATE: Sorry, when I editied my response the array sample got deleted. Here it is again.

$thisArray = array('item1','item2','item3','item4')

$_SESSION['items'] = $thisArray;

## to access the array, you just have to start session
## for example,
session_start();

## item1 to 4 
$_SESSION['items'][0];
$_SESSION['items'][1];
$_SESSION['items'][2];
$_SESSION['items'][3];
commented: Thanks for the help! My apologies for taking so long to respond. +0

Thanks for the suggestions. I changed to code to what dschuett had stated, and I get error messages, but the data still writes to the database. I think what is happening here is a conflict with the template I am using. The error messages state the following:

Warning: Invalid argument supplied for foreach() in /myserver/public_html/demo/libraries/gantry/core/gantrytemplate.class.php on line 537

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /myserver/public_html/demo/libraries/gantry/core/gantrytemplate.class.php on line 508

Warning: Invalid argument supplied for foreach() in /myserver/public_html/demo/modules/mod_roknavmenu/lib/RokNavMenu.php on line 72

Fatal error: Call to a member function getDefaults() on a non-object in /myserver/public_html/demo/modules/mod_roknavmenu/lib/librokmenu/RokMenu.php on line 64

I'm using a template from RocketTheme, and I think these errors are conflicting with something else in their code as well. Perhaps I will rework the code into an array as you suggested, veedeo, and see if it makes a difference.

Thanks for the suggestions everyone. I appreciate it.

This appears to be a problem with my template and Joomla, not necessarily a problem with my code. I will still definitely try to get my code organized into an array, however. Always willing to learn more. :)

Meanwhile, I scoot over to RocketTheme's website to see what they suggest. Thanks again everyone for your help.

Good luck, I am looking at my joomla in my localhost. I have the oldest version way back in the late 2010. So, I really have to update it before I can take a look at their classes.

Its just probably just something that needs to be extended... in their database connection class. It is good to just go for extending the responsible class, this way you can write it specific to your needs..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.