0

I've just started learning php and I'm having trouble finding the problem when I get it to connect to the database and run an 'INSERT' query. I think the problem is in my connection.php file.

insert.php:

<?PHP
echo "into";
include("connection.php");
echo "connected";
$table = blog;
$sql = "INSERT INTO $table SET
Title = '$_POST[title]',
Description = '$_POST[description]',
Content = '$_POST[content]'";
$query = @mysql_query($sql);

echo "complete";

echo $_POST[title];
echo $_POST[description];
echo $_POST[content]; 
?>

connection.php:

<?php
echo "connection file reached";
$conn = mysql_connect(localhost, user, pass);
mysql_select_db(test, $conn);
echo "connected";
?>

Any advice on my code, or how I could solve this problem would be much appreciated.

EDIT: I'm thinking that it never gets to the connection.php file because it shows the "into" at the top of insert.php but not the "connection file reached" at the top of connection.php. Is that call correct to that file?

Thanks

13
  • 3
    You should use mysqli or PDO to prevent SQL-Injections. Commented Jul 5, 2014 at 13:57
  • Its only on a local server so that shouldn't be too much of a problem will it? Commented Jul 5, 2014 at 13:58
  • $table is not defined. Commented Jul 5, 2014 at 13:58
  • set error_reporting(E_ALL); and ini_set('display_errors', 1); Commented Jul 5, 2014 at 13:59
  • get the error <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> Commented Jul 5, 2014 at 13:59

2 Answers 2

1

Readup on php constants, variables and strings!

define('foo','bar'); // foo == 'bar';
$foo = 'bar'; // $foo == 'bar'

Please use mysqli_* as mysql_* is depreciated.

$conn = mysql_connect(localhost, user, pass);

probably should be

$conn = mysql_connect('localhost', 'user', 'pass');
Sign up to request clarification or add additional context in comments.

3 Comments

That worked, thanks. It went through and said connected etc but the db is still empty, any ideas on this?
I would install phpmyadmin on your setup xamp or whatever your using, and start by copying in the result of echo $sql it will help you debug your query
i have Sequel Pro running and can see the database
0

you have written

@mysql_query($sql)

as well as a lot other problem it's structure is also wrong the problem might be from version of your language as recommendation of php you should not use mysql any more use mysqli or PDO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.