Hi everybody
I need a help. codes below are written by someone else. I have a mysql database, html form to login to this database:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>1</title>
</head>
<body>
<h4>Please provide correct username and password:</h4>
<form enctype="multipart/form-data" action="login.php" method="post">
Login: <input name="usrlogin" type="text"><br>
Password: <input name="usrpasswd" size="15" maxlength="14" type="password"><br>
  <input value="Sign in" type="submit"> </form>
</body>
</html>

a small php script login.php including next form:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>upload_form</title>
</head>
<body>
<h4>Log on to database: </h4>
<?php 
 {
 //This gets all the other information from the form 
 $name=$_POST['usrlogin']; 
 $pass=$_POST['usrpasswd']; 
if ( !name OR !pass ) { 
echo'you didn\'t fell all fields required ..'; 
} else { 
// Connects to your Database 
 mysql_connect("mysql.mserver.ex", $name, $pass) or die(mysql_error()) ; 
 mysql_select_db("mydb") or die(mysql_error()) ; 
}
 }
 ?> 
<form enctype="multipart/form-data" action="add.php" method="post">
Name of picture: <input name="PictureName" type="text"><br>
Description: <input name="PictureDesc" type="text"><br>
Path: <input name="PictPath" type="text"><br>
Photo: <input name="photo" type="file"><br>
<input value="Add" type="submit"> </form>
</body>
</html>

this works ok
next add.php script to put informafion to database:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>upload_form</title>

</head>
<body>
<h4>uploading...</h4>
<?php 
 
 //This is the directory where images will be saved 
 $target = "picts/"; 
 $target = $target . basename( $_FILES['photo']['name']); 
 
 //This gets all the other information from the form 
 $name=$_POST['PictureName']; 
 $email=$_POST['PicturePath']; 
 $phone=$_POST['PictureDesc']; 
 $pic=($_FILES['photo']['name']); 
 
 
 //Writes the information to the database 
 mysql_query("INSERT INTO `mydb`.`picture` (`PictureID`, `PictureName`, `PicturePath`, `PictureDesc`, `Unused`) VALUES (NULL, '$name', '$email', 'phone', '');") ; 
 
 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 
 
 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 echo "Last inserted record has id %d\n", mysql_insert_id();
 } 
 else { 
 
 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?> 
</body></html>

after I submit form from login.php I got error I'm not connected to database. What I do wrong, could anybody analise and help please?

Recommended Answers

All 3 Replies

You will need to have the mysql_connect statement in every script that you wish to connect to the database, this does not transfer between scripts.

Alternatively, have a separate file which connects to the database and then use a require or include statement to use it on any page you need to.

Member Avatar for rajarajan2017
mysql_connect("mysql.mserver.ex", $name, $pass) or die(mysql_error()) ;

Should have this statement in every php databse query file or else globally write a file and include within your all files.

thanks very much for fast replay, I thought about this but question is how can I transfer $name, $pass values between scripts?

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.