Reputation: 55
So I am trying to make this website that takes in the values that are given to it by a text document and pout them into a mySQL database. The code I am running in my php doesn't give me any syntax errors, but the values aren't added to the database tables.
$upload = new mysqli('localhost', 'uMoviesRoot', $_POST['password1']);
if (mysqli_connect_errno()) {
echo "There as an error.";
}
else {
mysql_select_db("localhost");
$file= fopen($_FILES['Upload']['tmp_name'], 'r');
while(! feof($file)){
$line = fgetcsv($file, 999);
if ($line[0] == "movie") {
mysql_query("INSERT INTO movies (movie, year) VALUES ($line[1], $line[2])");
$movieCount++;
$lastMovie = $line[1];
}
Just some background, I have created the tables in mySQL (using MySQL workbench) and made a schema named movies. There are tables named actors(2 columns), directed_by (2 columns), directors(1 column), movies(2 columns), and performed_in (3 columns). I only put one of these additions in the code just to make it shorter (since all of the ifs do the same thing).
Is this a problem with my PHP code?
Upvotes: 0
Views: 60
Reputation: 2234
You should mysql_select_db("movies");
, localhost
is your server address and not the database name.
UPDATE (not testet but this should work):
$upload = new mysqli('localhost', 'uMoviesRoot', $_POST['password1']);
if (mysqli_connect_errno()) {
echo "There as an error.";
} else {
mysql_select_db("movies");
$file= fopen($_FILES['Upload']['tmp_name'], 'r');
while(! feof($file)){
$line = fgetcsv($file, 999);
if ($line[0] == "movie") {
mysql_query("INSERT INTO movies (movie, year) VALUES ('$line[1]', '$line[2]')");
$movieCount++;
$lastMovie = $line[1];
}
}
}
You could also output mysql_error to see the errors.
If you have values from user input, you should have a look at Prepared Statements to avoid SQL injection, etc.
Upvotes: 1