probably something stupid as my MySQL PHP knowledge is so rusty.. ive cleaned up all the errors i could and still i cannot write to my DB. Can anyone shed some light on what may be wrong, as its not even printing an error out anymore. Thanks:
<?php
$conn=mysql_connect("127.0.0.1","root","","pe_results");
if (isset($_POST['Name'])) {
$Name = $_POST['Name'];
}
if (isset($_POST['Short'])) {
$Short = $_POST['Short'];
}
if (isset($_POST['Med'])) {
$Med = $_POST['Med'];
}
if (isset($_POST['Long'])) {
$Long = $_POST['Long'];
}
if (isset($_POST['VLong'])) {
$VLong = $_POST['VLong'];
}
if (isset($_POST['Extreme'])) {
$Extreme = $_POST['Extreme'];
}
if (isset($_POST['LJump'])) {
$LJump = $_POST['LJump'];
}
if (isset($_POST['HJump'])) {
$HJump = $_POST['HJump'];
}
if (isset($_POST['Shotputt'])) {
$Shotputt = $_POST['Shotputt'];
}
if (isset($_POST['Discuss'])) {
$Discuss = $_POST['Discuss'];
}
if (isset($_POST['Javelin'])) {
$Javelin = $_POST['Javelin'];
}
if (isset($_POST['Date'])) {
$Date = $_POST['Date'];
}
if (isset($_POST['Year'])) {
$Year = $_POST['Year'];
}
$sql="INSERT INTO results_main (Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year)
VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";
$result = mysql_query($sql,$conn);
if($result){
echo"<br/>Everythings been saved";
echo "<BR>";
echo "<a href='index.html'>Back to the main page</a>";
}
else {
echo 'Fatal Error, you information has not been saved';
}
// close connection
mysql_close($conn);
?>
Here is the update:
The query is: INSERT INTO results_main (Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year) VALUES ('Chris Davern', '10:00', '20:00', '40:00', '80:00', '15:00', '100m', '10m', '15m', '50m', '400m', '2013-03-27', '9')
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 'Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year) ' at line 1
That means you aren't selecting a database. Here is how I used to connect with mysql_ functions:
$host = "localhost";
$databasename = "pe_results";
$databaseusername = "root";
$databasepassword = "password";
$conn = mysql_connect("$host", "$databaseusername", "$databasepassword");
mysql_select_db("$databasename", $conn);
$sql="INSERT INTO results_main (`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";
$result = mysql_query($sql);
Also please notice that updated the column names because the LONG column name is a mysql keyword so it needs the to recognize it as a column name instead of a keyword.
Then you can execute your mysql_query. However, like we mentioned earlier. The mysql_ functions are deprecated and should no longer be used.
Reading the php doc, your 4th argument to mysql_connect
should be a bollean, I guess it's your DB's name, so you never select any database. Use :
mysql_select_db("pe_results");
this code may have print this error. :
$result = mysql_query($sql,$conn) or die (mysql_error ());
You didn't select a database when creating the connection so you can specify it like this:
mysql_select_db("Put your Database Name Here");
The word LONG is a reserved word in MySQL... Try wrapping everything in backticks:
$sql="INSERT INTO `results_main` (`Name`, `Short`, `Med`, `Long`, `VLong`, `Extreme`, `LJump`, `HJump`, `Shotputt`, `Discuss`, `Javelin`, `Date`, `Year`)
VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";
Instead of
$sql="INSERT INTO results_main (Name, Short, Med, Long, VLong, Extreme, LJump, HJump, Shotputt, Discuss, Javelin, Date, Year)
VALUES ('$Name', '$Short', '$Med', '$Long', '$VLong', '$Extreme', '$LJump', '$HJump', '$Shotputt', '$Discuss', '$Javelin', '$Date', '$Year')";