0

Ok, I'm new to php and MYSQL aswell. I have a couple programs actually that are getting this error and I don't see what could be causing this at all.

Here's the situation. I have a job interview page. The user enters the information from the candidate and submits the interview. Everything works find the FIRST time around. Database is created, table is created, ID is given to the candidate, everything works good.

When I go to enter a second interview I get this error:
Unable to execute the query.

Error code 1007: Can't create database 'jobinterview'; database exists

Here's the code that creates the database, tables etc.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Job Interview</title>
<meta http-equiv="content-type"
	content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
/* ==== Code to ensure interviewer fills out all required areas === */
if (empty($_GET['first_name']) ||
	empty($_GET['last_name']) ||
	empty($_GET['position']) ||
	empty($_GET['cand_first']) ||
	empty($_GET['cand_last']) ||
	empty($_GET['cand_ID']) ||
	empty($_GET['comments']))
		die("<p>You must enter a value in every field for the interview! Click your 
		browser's Back button to return to the Interview form.</p>");

if (($_GET['month'] == "Month") ||
	($_GET['date'] == "Day") ||
	 ($_GET['year'] == "Year"))
	die ("<p>You must enter the full date in the drop down menus! Click
	     your browser's Back button to return to the Interview form.</p>");
	
if ($_GET['recommendation'] == "--- --- ---")
	die ("<p>You must enter a recommendation value in the drop down menu! Click
	     your browser's Back button to return to the Interview form.</p>");
	
if (($_GET['intellect'] == "--- --- ---") ||
	($_GET['communication'] == "--- --- ---") ||
	($_GET['business'] == "--- --- ---") ||
	($_GET['computer'] == "--- --- ---"))
	die ("<p>You must enter values for all of the Candidate's skills/presentation 
	     from the drop down menues! Click your browser's Back button to return to 
		 the Interview form.</p>");
	
/* ==== Code to connect to the database ==== */
$DBConnect = @mysqli_connect("localhost", "user", "password")
	Or die("<p>Unable to connect to the database server.</p>"
	. "<p>Error code " . mysqli_connect_errno()
	. ": " . mysqli_connect_error()) . "</p>";
	
/* ==== Code to create hit_counter database if it does not already exist ==== */
$DBName = "jobinterview";
if (!@mysqli_select_db($DBConnect, $DBname)) {
	$SQLstring = "CREATE DATABASE $DBName";
	$QueryResult = @mysqli_query($DBConnect, $SQLstring)
		Or die("<p>Unable to execute the query.</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
	echo "<p>Successfully created the database</p>";
	mysqli_select_db($DBConnect, $DBName);
}

/* ==== Code to create the table and give each candidate a candID === */
$TableName = "candidates";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if (!$QueryResult) {
		$SQLstring = "CREATE TABLE $TableName (candID SMALLINT
		NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(40), last_name VARCHAR(40),
		position VARCHAR(40), month VARCHAR(9), date VARCHAR(2), year VARCHAR(4),
		cand_first VARCHAR(40), cand_last VARCHAR(40), cand_ID VARCHAR(40), recommendation VARCHAR(20),
		comments VARCHAR(350), intellect VARCHAR(20), communication VARCHAR(20), business VARCHAR(20),
		computer VARCHAR(20))";
		$QueryResult = @mysqli_query($DBConnect, $SQLstring)
		Or die("<p>Unable to create the table.</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
	echo "<p>Successfully created the candidates table</p>";
}

/* ==== Adds the candidate to the database and closes the connection ==== */
$Int_Last = addslashes($_GET['first_name']);
$Int_First = addslashes($_GET['last_name']);
$Int_Position = addslashes($_GET['position']);
$Month = addslashes($_GET['month']);
$Day = addslashes($_GET['date']);
$Year = addslashes($_GET['year']);
$Cand_First = addslashes($_GET['cand_first']);
$Cand_Last = addslashes($_GET['cand_last']);
$Cand_ID = addslashes($_GET['cand_ID']);
$Recommend = addslashes($_GET['recommendation']);
$Comments = addslashes($_GET['comments']);
$Intellect = addslashes($_GET['intellect']);
$Communication = addslashes($_GET['communication']);
$Business = addslashes($_GET['business']);
$Computer = addslashes($_GET['computer']);
$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$Int_Last',
	'$Int_First', '$Int_Position', '$Month', '$Day', '$Year', '$Cand_First', '$Cand_Last',
	'$Cand_ID', '$Recommend', '$Comments', '$Intellect', '$Communication', '$Business', '$Computer')";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
	Or die("<p>Unable to execute the query.</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<strong>Interview Saved</strong>";
$CandID = mysqli_insert_id($DBConnect);
echo "<p>This candidate has been assigned an ID of $CandID.</p>";
mysqli_close($DBConnect);
?>
<hr>
<p><a href="JobInterview.html">Return to Job Interview Page</a></p>
</body>
</html>

I know it may seem a bit messy towards the end, still working on getting it tightened up and buttoned up a bit, but I'm at my wits end with this error and have looked through book after book, and searched the web and can't find a solution. Thanks for the help.

I'm currently using WAMP and have ran it through phpMyAdmin as well as trying to run it through SQLyog, same results. If that helps at all. Also, replaced my user name and password with "user" and "password" just in case.

Edited by SMHouston: n/a

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by SMHouston
0

Try this:

/* ==== Code to create hit_counter database if it does not already exist ==== */
$DBName = "jobinterview";
$SQLstring = "CREATE DATABASE IF NT EXISTS $DBName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
		OR die("<p>Unable to execute the query.</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
mysqli_select_db($DBConnect, $DBName);

Edited by genevish: n/a

0

Get a new error there.

Unable to execute the query.

Error code 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NT EXISTS jobinterview' at line 1

0

Ok, that worked. Thank you very much.
Are you able to explain why the other code wasn't working correctly?

The book I'm going through to learn this has the code I had in every single program they use as an example, and every single one of them won't work throwing up the 1007 error, and needless to say it's frustrating trying to learn from a book that is wrong.

0

Also, when I put the 2nd , 3rd interview etc in, shouldn't the "successfully created database" echo statement not be showing?

I tried putting and if..else statement in there but even with that it keeps doing it.

0

Gonna mark this as solved. I figured out what was going on, and again sometimes even the smallest of things can slip by. I rewrote that code literally 20 times and not once did I pick up what I was doing.

In this line:

if (!@mysqli_select_db($DBConnect, $DBname)) {

for $DBName I have a (n) instead of an (N). Unreal but it works now. Gonna mark it as saved and thanks again for the help. I just can't believe the amount of time I rewrote that code and made the same exact mistake that many times and didn't catch it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.