Help - Search - Members - Calendar
Full Version: Why Does This Not Work?
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
TFE
Hi,
Here is the script. It is not connecting to the database. The config file has all the details of db name, user, password etc. I don't know why this is not working. Any help greatly appreciated. I am desperate to find a solution. Thanks for your patience. mellow.gif
<BODY>
<?php
require 'config.php';

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$emailaddress = $_POST['emailaddress'];
$webtitle = $_POST['webtitle'];
$weburl = $_POST['weburl'];
$webdescription = $_POST['webdescription'];
$dateofentry = $_POST['dateofentry'];

// Establish a MySQL connection and select our database using values contained in config.php.
mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);

mysql_query('SELECT * FROM `Relatedlinks`');


/*
mysql_query('INSERT INTO `Relatedlinks` (`firstname`, `lastname`, `webemail`, `weburl`, `webtitle`, `webdescription`, `date`) VALUES (\'' . $firstname . ', ' . $lastname . ', ' . $emailaddress . ', ' . $weburl . ', ' . $webtitle . ', ' . $webdescription . ', ' . $dateofentry . ')') or die(mysql_error());
*/

?>
</BODY>


swordz
Apart from password, what mysql data are you using? That's where the problem is.

swordz
TFE
Actually it is connecting with the DB ok. It keeps coming back with parse errors because of the query string catenation errors. How to put together mysql_query('stuff') when i have to plugin the variables and do WHERE LIKE etc. A bit complicated that. wacko.gif If some one can show me some easy way of doing it, i will very grateful.


QUOTE(swordz @ Aug 12 2009, 07:07 AM) *
Apart from password, what mysql data are you using? That's where the problem is.

swordz
swordz
Using your commented out query as an example:

mysql_query("INSERT INTO `Relatedlinks` (`firstname`, `lastname`, `webemail`, `weburl`, `webtitle`, `webdescription`, `date`) VALUES ('" . $firstname . "', '" . $lastname . "', '" . $emailaddress . '", "' . $weburl . "', '" . $webtitle . "', '" . $webdescription . "', '" . $dateofentry . ")") or die(mysql_error());

Take note as to which are single and which are double quotes. Also, you should be using mysql_real_escape_string() to prevent SQL injection. Otherwise, that should hopefully work. It'd be helpful to see the exact error it gives, and the exact query you ran.

swordz
TFE
swords, thanks very much for your response. I had downloaded a manual from the internet and found out about the mysql_real_escape_string() which ofcourse made life a little easier. After roughing it with nested loops and string catenations, I have finally got it working like a breeze. smile.gif


QUOTE(swordz @ Aug 12 2009, 10:08 AM) *
Using your commented out query as an example:

mysql_query("INSERT INTO `Relatedlinks` (`firstname`, `lastname`, `webemail`, `weburl`, `webtitle`, `webdescription`, `date`) VALUES ('" . $firstname . "', '" . $lastname . "', '" . $emailaddress . '", "' . $weburl . "', '" . $webtitle . "', '" . $webdescription . "', '" . $dateofentry . ")") or die(mysql_error());

Take note as to which are single and which are double quotes. Also, you should be using mysql_real_escape_string() to prevent SQL injection. Otherwise, that should hopefully work. It'd be helpful to see the exact error it gives, and the exact query you ran.

swordz
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.