Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP, MYsql connection problem to database.

Status
Not open for further replies.

VitoCorleone

Programmer
Jun 22, 2004
28
GB
hello everyone,

I have a sime error but cant figure out the solution.The following PHP script inserts 2 values into mySQL database i created using the POST method.

<?php

global $staffno;

global $fname;

// Database connection variables

$dbServer = "localhost";

$dbDatabase = "dreamhome";


$sConn = mysql_connect($dbServer, "", "")

or die("Couldn't connect to database server");



$dConn = mysql_select_db($dbDatabase, $sConn)

or die("Couldn't connect to database $dbDatabase");

$dbQuery = "INSERT INTO staff, fname VALUES ";

$dbQuery .= "(0, '$staffno', '$fname')";


mysql_query($dbQuery) or die("Couldn't add file to database");

echo "<h1>Data Uploaded</h1>";


?>

Here is the error i am getting which can be found in the code if the connection is unsucessful.

Couldn't connect to database dreamhome

The database has been created and so too has the table so why does it not connect? i have not created a user name or password for the database or its tables.
 
i'm not sure that you can do without a username. if you have left the installation in vanilla state then using a username of "root" should function.

I think there is a syntax error in your query too. normally the form of insert you are using is
Code:
insert into tablename (col1,col2, ...) values (val1, val2,...)
you are correct that you need to put quotes round the values.

for single inserts i prefer the syntax
Code:
insert into tablename set col1=val1, col2=val2 ...
as there is less room for errors and it is easier to debug.

you should always escape the values too, using mysql_escape_string of mysql_real_escape_string.

 
hi, yes i noticed that sytax error which has been changed to

$dbQuery = "INSERT INTO staff (staff_no, fname) VALUES";

$dbQuery = "('$staffno', '$fname')";
 
that won't work as you have omitted the concatenate operator (the dot) before the "=" in the second line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top