-2

I've recently trying to add data into a database, (New to php), I've looked over to see where I've gone wrong, but can't find anything. The error is:

Unknown column 'FUMUKU' in 'field list'

Code:

$dbhost = 'localhost';
$dbuser = 'evocityi_admin';
$dbpass = 'password';
$database = 'evocityi_stocks';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $database);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$Dtime = "30/04/16";
$StockName = "FUMUKU";
$FUMUKUPrice = 1000;
$sql = "INSERT INTO stocks".
       "(Stock,Price, TimeD) ".
       "VALUES ".
       "('$StockName,$FUMUKUPrice, $DTime')";
mysql_select_db('evocityi_stocks');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>

SQL Database: https://gyazo.com/fc97b686cfea79ea773d1796e912551e

PaulB12345
  • 15
  • 5

6 Answers6

0

I managed to fix it using:

$sql = "INSERT INTO `stocks` (`Stock`,`Price`, `TimeD`) VALUES ('$StockName','$FUMUKUPrice', '".date('Y-m-d',strtotime($Dtime))."')";
The Codesee
  • 3,714
  • 5
  • 38
  • 78
PaulB12345
  • 15
  • 5
0
'$StockName,$FUMUKUPrice, $DTime'

You should surround every variable with quotes:

'$StockName' ,' $FUMUKUPrice' , '$DTime'

Just know that when blindly concatenating variables into a SQL query and not preparing statements for user input makes your code vulnerable to SQL injection. Use Prepared Statements instead. Also, use the mysqli_* functions, the mysql_* functions are deprecated.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

Try this query, you are not using qoutes properly on the variables due to this It through error.

$sql = "INSERT INTO stocks".
       "(Stock,Price, TimeD) ".
       "VALUES ".
       "('".$StockName."', '".$FUMUKUPrice."', '".$DTime."')";

To avoid deprecation and SQL Injection you should use PDO or mysqli.

Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40
0

You're using mysql_* functions, that's what's wrong.

Read the documentation and look into alternatives.

One such alternative may be:

$query = $pdoconnection->prepare("
    insert into `stocks`
    (`Stock`,`Price`,`TimeD`)
    values (?,?,?)
");
$query->execute([$StockName, $FUMUKUPrice, $Dtime]);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Use this It will helps you.

$sql = "INSERT INTO stocks(Stock,Price,TimeD) VALUES ('$StockName','$FUMUKUPrice', '".date('Y-m-d',strtotime($Dtime))."')";
Manish Silawat
  • 900
  • 5
  • 7
0

Try this

    $sql = ("INSERT INTO stocks (Stock,Price, TimeD)
                VALUES('$StockName', '$FUMUKUPrice', '$DTime')");
Venkatesh
  • 31
  • 7