1

this code calls the values entered into a form and enters them into a database (or at least it's supposed to) every time the page loads it gives "undefined index" messages, and I am struggling to determine why.

Any help that can be offered to me is greatly appreciated!

<?php

$dbc=mysql_connect('localhost', 'user', '');
mysql_select_db('database', $dbc);



$sqlInsertString = "INSERT INTO band_information (Name, Photo, Bio, City, State, Zipcode, Genre, Link)
            VALUES ({$_POST['bandname']}, {$_FILES['bandphoto']['name']}, {$_POST['bandbio']}, {$_POST['bandcity']},
                    {$_POST['bandstate']}, {$_POST['bandzipcode']}, {$_POST['bandgenre']},{$_POST['bandlink']});";

if($_SERVER['REQUEST_METHOD']=='POST'){
    if(move_uploaded_file($_FILES['bandphoto']['tmp_name'], "C:\\HTML\\mgertenbach\\BAND\\photos\\{$_FILES['bandphoto']['name']}") && $mysql_query($sqlinsertString, $dbc)){
        print '<p>Thanks for submitting your band!</p>';
    } else {
        print '<p>Could not submit band because: <br/>' .
        mysql_error($dbc) . '</p>';
    }
}   
Rob M.
  • 35,491
  • 6
  • 51
  • 50
user3081307
  • 1,211
  • 2
  • 10
  • 8
  • Like you mention above, this is a page to enter a form and insert into a database, so when you load this page at first or before submitting the form, it will still insert into a database but since there is no value in the form but variable is not defined. you should do a check if isset($_POST){ //save into database } – iCezz Dec 09 '13 at 06:11
  • You need to check for $_FILES being empty, too. Is your form using `enctype="multipart/form-data"`? – Damien Pirsy Dec 09 '13 at 06:12
  • Does that SQL actually work? Your values aren't quoted... – Rob M. Dec 09 '13 at 06:13

1 Answers1

1

Since you are getting values from your <form> , you need to first check whether they are set or not.

You should make use of the isset construct for that. Like this

if(!isset($_POST['bandname'])) // And whichever variables you get from your <form>
{
echo "Band Name was not Provided";
exit;
}
else
{
//.... do your CRUD operations here
}

Secondly, you are using mysql_* functions which will be deprecated soon. Switch to MySQli or PDO instead.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126