Page 1 of 1

Not connecting to Database

Posted: Wed Oct 27, 2010 9:13 am
by doug76
Hello,

I have been trying to solve this for what seems like ages.

I have a PHP program that "appears" to work. I am trying to add basic form informtion to a database. To the user they fill in the form and the program thanks them along with the information they have just entered. This alll works.

For some reason this does not get entered into the MySQL database.

I cannot see any problems with the code and cannot understand why it is not working

Any help would be much appreicated

code below
DP

Code: Select all

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Guitar wars - add you high score</title>
<link rel="stylesheet" type="text/css" href"style.css />
</head>
<body>
<h2>Guitar Wars - add you high score</h2>

<?php
session_start();
//Define the uplaod path and maximum file size constants
require_once('appvars.php');
require_once('connectvars5.php');

if (isset($_POST['submit'])) {

// Grab the score data from the POST
        $name = trim($_POST['name']); 
        $score = trim($_POST['score']);
        $screenshot = trim($_FILES['screenshot']['name']);

        if (!empty($name) && !empty($score) && !empty($screenshot)) {
        
        if ($_FILES['screenshot']['error'] == 0) {
//Move the file to the target upload folder
        $target = GW_UPLOADPATH . $screenshot;
        if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {

//Connect to the database
        $dbc = mysqli_connect('DB_host', 'DB_user', 'DB_password', 'DB_name');

//Write the data to the database
        $query = "INSERT into gwdb values (0, '$name', '$score', '$screenshot')";
        mysqli_query($dbc, $query);
 
//Confirm success with the user
echo '<p>Thanks for submitting your new high score!</p>';
echo '<strong>Name:</strong> ' . $name . ' <br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index5.php"><<Back to high scores</a></p>';

//Clear the score data to clear form
$name = "";
$score = "";
$screenshot ="";

mysqli_close($dbc);

}

else  {

echo '<p class="error">Sorry there was a problem uploading your screen shot image.</p>';
}
}

else {
        echo '<p class="error">The screen shot must be a GIF,JPEG or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
}
//Try to delete the temporary screen shot image file
@unlink($_FILES['screenshot']['tmp_name']);
}
else

{

echo '<p class="error">Please enter all of the information to add your high score.</p>';
}
}


?>  

<hr />

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	<input type="hidden" name="MAX_FILE_SIZE" value="150000000000000" />
	<label for="first_name">Name:</label><input type="text" id="name" name="name"
	value="<?php if (!empty($name)) echo $name; ?>" /><br />
	<label for="score">Score:</label><input type="text" id="score" name="score"
	value ="<?php if (!empty($score)) echo $score; ?>" /> <br />

	<label for="screenschot">Screen Shot:</label>
	<input type="file" id="screenshot" name="screenshot" /><br /><br />

	
<hr />

<input type="submit" value="Add" name="submit" />
</form>
</body>
</html>     

Re: Not connecting to Database

Posted: Wed Oct 27, 2010 10:16 am
by mikosiko
- Set error_reporting to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed.
- Use mysqli_error() to detect & control possibles errors in your mysqli_connect and mysqli_query.
- Debug (with echo at least) your code to be sure that you are reaching this line

Code: Select all

        if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
and beyond.

Re: Not connecting to Database

Posted: Wed Oct 27, 2010 10:23 am
by phppro62
Hi doug76
in line #30
$dbc = mysqli_connect('DB_host', 'DB_user', 'DB_password', 'DB_name');

you should change the DV-host to your server ( if you are working on your computer locally it often called 'localhost'
the DB_password your database password
and Db_name user name that can access the dbase

and also change line 33
$query = "INSERT into gwdb values (0, '$name', '$score', '$screenshot')";
mysqli_query($dbc, $query);

change gwdb with your table name NOT DBASE name

and then this will work.

Re: Not connecting to Database

Posted: Sat Oct 30, 2010 6:13 am
by doug76
Thank you! It is easy to read the wrong thing after reading so many times!